题解:CF1257D Yet Another Monster Killing Problem
很容易注意到一天要打最多的怪。 \
接着考虑在一天中当前打到第
- 对怪物, 若存在英雄
i ,使得s_i \geq r-l+1 且p_i \geq \max_{i=l..r}{a_i} ,则可行。(充要)
到这里其实可以做了呢,我们把英雄按
- 要得到
O(n) 算法,我们还得动点脑子。
既然可以二分答案,那能不能增量式?因为查询不依赖
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
int T, n, m;
int a[N], p[N], s[N], bst[N];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> T;
while (T -- ) {
cin >> n;
for (int i = 1; i <= n; i ++ ) bst[i] = 0, cin >> a[i];
cin >> m;
for (int i = 1; i <= m; i ++ ) cin >> p[i] >> s[i], bst[s[i]] = max(bst[s[i]], p[i]);
for (int i = n - 1; i; i -- ) bst[i] = max(bst[i], bst[i + 1]);
int res = 0;
for (int r = 1, l = 1; l <= n; res ++ , l = r) {
if (bst[1] < a[r]) { res = -1; break; }
int maxv = a[r];
while (r <= n && maxv <= bst[r - l + 1]) maxv = max(maxv, a[ ++ r]);
}
cout << res << endl;
}
return 0;
}