Codeforces 732D Exams

链接

传送门

题意

\(n\)天,\(m\)个科目,给出每天可以考试的科目和考过一个科目之前需要复习的天数,输出最少多少天可以考过所有科目。

思路

二分完成的天数,然后每个科目,如果有多个日期可以参与考试,选后面的日期考试,不会比选前面的更差。然后用扫描线检验一遍中途有没有复习时间不够的就好啦。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 100010;

int n, m;
int d[maxn], a[maxn], tmp[maxn];
int vis[maxn], vis_clock;

bool check(int x) {
++vis_clock;
int cnt = 0;
for (int i = x; i > 0; --i) {
if (d[i] != 0 && vis[d[i]] != vis_clock) {
tmp[i] = -a[d[i]];
vis[d[i]] = vis_clock;
++cnt;
} else {
tmp[i] = 1;
}
}
if (cnt != m) {
return false;
}
int sum = 0;
for (int i = 1; i <= x; ++i) {
sum += tmp[i];
if (sum < 0) {
return false;
}
}
return true;
}

int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &d[i]);
}
for (int i = 1; i <= m; ++i) {
scanf("%d", &a[i]);
}
int L = m, R = n, ans = inf;
while (L <= R) {
int M = L + (R - L) / 2;
if (check(M)) {
R = M - 1;
ans = min(ans, M);
} else {
L = M + 1;
}
}
printf("%d\n", ans == inf ? -1 : ans);
return 0;
}