Codeforces 1165F2(二分内的check)
Posted alphawa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1165F2(二分内的check)相关的知识,希望对你有一定的参考价值。
要点
- 二分答案,内部喜闻乐见的拖延策略:对于某个打折玩具,就选最晚的打折时间买,答案并不会变劣,只是购买时间的平移。
- 注意最晚时间不是预处理的东西,是二分内部的、在mid以内的最晚时间。
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 2e5 + 5;
int n, m, k[maxn], d[maxn], t[maxn];
int Late[maxn], sum;
bool ok(int mid) {
for (int i = 1; i <= n; i++) {
Late[i] = 0;
}
vector<int> day[maxn];
for (int i = 1; i <= m; i++) {
if (d[i] <= mid)
Late[t[i]] = max(Late[t[i]], d[i]);
}
for (int i = 1; i <= n; i++) {
if (k[i] && Late[i])
day[Late[i]].push_back(i);
}
int s = sum, cur = 0;
for (int i = 1; i <= mid; i++) {
cur++;
if (i < maxn && day[i].size()) {
for (int t : day[i]) {
if (cur >= k[t]) {
cur -= k[t];
s -= k[t];
} else {
s -= cur;
cur = 0;
break;
}
}
}
}
return cur >= s * 2;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &k[i]), sum += k[i];
for (int i = 1; i <= m; i++) {
scanf("%d%d", &d[i], &t[i]);
}
int l = 1, r = 4e5 + 5, ans;
while (l <= r) {
int mid = (l + r) >> 1;
if (ok(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
return !printf("%d\n", ans);
}
以上是关于Codeforces 1165F2(二分内的check)的主要内容,如果未能解决你的问题,请参考以下文章
codeforces 713D D. Animals and Puzzle 二分+二维rmq
CodeForces 589F-Gourmet and Banquet-二分答案
Codeforces 889F Letters Removing(二分 + 线段树 || 树状数组)
Codeforces 1262E Arson In Berland Forest(二维前缀和+二维差分+二分)
Codeforces Round #747 (Div. 2) D. The Number of Imposters(并查集,二分图)