我们假设最终的序列中最大的一项为 \(a_x\) ,贪心可得,为了使 \(ans\) 尽量大,我们在操作中不应该对包含 \(x\) 的区间进行操作,而其它区间都应该进行操作
枚举 \(x\) 即可,时间复杂度 \(O(n^2+nm)\)
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 306;
int n, m, a[N], l[N], r[N], c[N];
vector<int> ans[N];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) scanf("%d %d", &l[i], &r[i]);
int num = 0, now;
for (int x = 1; x <= n; x++) {
memset(c, 0, sizeof(c));
for (int i = 1; i <= m; i++) {
if (l[i] <= x && r[i] >= x) continue;
--c[l[i]];
++c[r[i]+1];
ans[x].push_back(i);
}
int s = 0, t = 0, k;
for (int i = 1; i <= n; i++) {
s += c[i];
if (!t || a[i] + s < k) k = a[t=i] + s;
}
if (!num || a[x] - k > now) now = a[num=x] - k;
}
cout << now << endl << ans[num].size() << endl;
for (unsigned int i = 0; i < ans[num].size(); i++)
printf("%d ", ans[num][i]);
puts("");
return 0;
}