题目链接:LibreOJ 2606 借教室
题目大意:
题解:
因为订单编号从(1)到(m),且须遵循先后顺序,所以可以用二分答案。
验证答案时,把(1)到(x)号订单每天借教室的总和统计出来,用差分数组存储。
超过最大可借教室数量时,(x)号订单借不到教室。
#include <cstring>
#include <iostream>
using namespace std;
int n, m, ans = 0;
int a[1000005], res[1000005];
int d[1000005], s[1000005], t[1000005];
bool judge(int x) {
int sum = 0;
memset(res, 0, sizeof(res));
for (int i = 1; i <= x; ++i) {
res[s[i]] += d[i];
res[t[i] + 1] -= d[i];
}
for (int i = 1; i <= n; i++) {
sum += res[i];
if (a[i] < sum) {
return false;
}
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
cin >> d[i] >> s[i] >> t[i];
}
int l = 1, r = m;
while (l <= r) {
int mid = l + r >> 1;
if (!judge(mid)) {
ans = mid;
r = mid - 1;
} else
l = mid + 1;
}
if (!ans) {
cout << ans;
} else {
cout << -1 << endl << ans;
}
return 0;
}