Sereja and Swaps CodeForces - 426C
题解:明显暴力是可以做的,枚举每段区间,区间内从小到大排序,区间外从大到小排序,然后用大的替换小的,更新最大值。
PS:方向想错了,代码写不出来。。。假设一次都不交换,求maxf(l, r),然后分别讨论要不要交换,然而,并没写出来。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<bitset> #include<vector> #include<queue> #include<map> #include<string> #include<stack> #define ll long long #define P pair<int, int> #define PP pair<int,pair<int, int>> #define pb push_back #define pp pop_back #define lson root << 1 #define INF (int)2e9 + 7 #define rson root << 1 | 1 #define LINF (unsigned long long int)1e18 #define mem(arry, in) memset(arry, in, sizeof(arry)) using namespace std; int n, k; int a[300], b[300], c[300]; bool cmp1(int x, int y) { return x < y; } bool cmp2(int x, int y) { return x > y; } int get(int l, int r) { vector<int> p, q; int res = 0; for(int i = l; i <= r; i++) q.pb(a[i]), res += a[i]; for(int i = 1; i <= n; i++) if(i < l || i > r) p.pb(a[i]); sort(q.begin(), q.end(), cmp1); sort(p.begin(), p.end(), cmp2); int len = min(r - l + 1, n - r + l - 1); for(int i = 1; i <= min(k, len); i++) { if(q[i - 1] < p[i - 1]) res = res - q[i - 1] + p[i - 1]; } return res; } int main() { cin >> n >> k; for(int i = 1; i <= n; i++) cin >> a[i]; int ans = -INF; for(int i = 1; i <= n; i++) { for(int j = i; j <= n; j++) { ans = max(ans, get(i, j)); } } cout << ans << endl; return 0; }