
今天模拟赛考了这道题,连样例1都没过。
代码也条过了,还是没用,老师让我自己条。我手玩都可以过样例,但是写出来就不对。
请各位大佬帮我看一下哪里出问题了,实在条不出来了。
my code:
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
struct grater {
	int x;
	friend bool operator < (const grater &a, const grater &b) {
		return a.x > b.x;
	}
};
int n, k, a[201];
priority_queue<int> q1, clr;
priority_queue<grater> q2;	//区间里要和外面交换的数 
int main() {
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	int ans = -1e9;
	for (int i = 1; i <= n; i++)
		for (int j = i; j <= n; j++) {
			q1 = clr;	//clear
			for (int l = 1; l <= n; l++)
				if (l >= i && l <= j)
					q2.push({a[i]});
				else
					q1.push(a[i]);
			int p = k;
			while (p-- && !q1.empty()) {
				int x = q1.top(); q1.pop();
				int y = q2.top().x;
				if (y >= x)
					break;
				q2.pop();
				q2.push({x});
			}
			int cnt = 0;
			while (!q2.empty()) {
				cnt += q2.top().x;
				q2.pop();
			}
			ans = max(ans, cnt);
		}
	printf("%d", ans);
}