[Codeforces 425A] Sereja and Swaps

时间:2023-03-10 02:27:44
[Codeforces 425A] Sereja and Swaps

[题目链接]

https://codeforces.com/contest/425/problem/A

[算法]

枚举最终序列的左端点和右端点 , 尝试用这段区间中小的数与区间外大的数交换

时间复杂度 : O(N^3logN)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = ;
const int inf = 2e9; int n , k;
int a[MAXN],b[MAXN],value[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} int main()
{ read(n); read(k);
for (int i = ; i <= n; i++) read(value[i]);
int ans = -inf;
for (int i = ; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
int la = , lb = , cnt = ;
for (int x = ; x <= n; x++)
{
if (x >= i && x <= j)
{
a[++la] = value[x];
cnt += value[x];
} else
b[++lb] = value[x];
}
sort(a + ,a + la + );
sort(b + ,b + lb + ,greater<int>());
for (int x = ; x <= min(la,min(lb,k)); x++)
{
if (b[x] - a[x] > )
cnt += b[x] - a[x];
else break;
}
ans = max(ans,cnt);
}
}
printf("%d\n",ans); return ; }