#include <bits/stdc++.h>
using namespace std;
const int N = 200'007;
int n, s, r, d[N], b[N], q[N];
int req[N], nxt_req[N];
int nxt[N], cur_req[N];
queue<int> last[N];
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n >> s >> r;
for (int i = 0; i < n; i++) {
cin >> d[i];
cur_req[i] = -1;
}
for (int i = 0; i < r; i++) {
cin >> b[i] >> q[i];
req[b[i]] = q[i];
}
for (int i = n - 1; i >= 0; i--) if (req[d[i]] > 0) {
if ((int) last[d[i]].size() > 0) {
nxt[i] = last[d[i]].back();
}
else {
nxt[i] = -1;
}
last[d[i]].push(i);
if ((int) last[d[i]].size() > req[d[i]]) {
last[d[i]].pop();
}
if ((int) last[d[i]].size() < req[d[i]]) {
nxt_req[i] = -1;
}
else {
nxt_req[i] = last[d[i]].front();
}
}
int ans = INT_MAX, req_done = 0;
for (int i = 0, j = -1, k = -1; i < n; i++) {
while (j + 1 < n && req_done < r) {
j++;
if (req[d[j]] > 0 && cur_req[d[j]] == -1 && nxt_req[j] != -1) {
cur_req[d[j]] = j;
k = max(k, nxt_req[j]);
req_done++;
}
}
if (req_done == r) {
ans = min(ans, k - i + 1);
}
else {
break;
}
if (req[d[i]] > 0) {
cur_req[d[i]] = -1;
req_done--;
if (nxt[i] <= j && nxt[i] != -1 && nxt_req[nxt[i]] != -1) {
cur_req[d[i]] = nxt[i];
k = max(k, nxt_req[nxt[i]]);
req_done++;
}
}
}
if (ans == INT_MAX) {
cout << "impossible\n";
}
else {
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
134972 KB |
Output is correct |
2 |
Correct |
79 ms |
134868 KB |
Output is correct |
3 |
Correct |
80 ms |
134984 KB |
Output is correct |
4 |
Correct |
79 ms |
135040 KB |
Output is correct |
5 |
Correct |
84 ms |
134984 KB |
Output is correct |
6 |
Correct |
73 ms |
134884 KB |
Output is correct |
7 |
Correct |
81 ms |
134972 KB |
Output is correct |
8 |
Correct |
77 ms |
134860 KB |
Output is correct |
9 |
Correct |
76 ms |
134860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
135092 KB |
Output is correct |
2 |
Correct |
80 ms |
135028 KB |
Output is correct |
3 |
Correct |
80 ms |
135056 KB |
Output is correct |
4 |
Correct |
82 ms |
134964 KB |
Output is correct |
5 |
Correct |
81 ms |
135044 KB |
Output is correct |
6 |
Correct |
85 ms |
134960 KB |
Output is correct |
7 |
Correct |
80 ms |
134872 KB |
Output is correct |
8 |
Correct |
76 ms |
134880 KB |
Output is correct |
9 |
Correct |
80 ms |
134884 KB |
Output is correct |
10 |
Correct |
77 ms |
134980 KB |
Output is correct |
11 |
Correct |
82 ms |
134916 KB |
Output is correct |
12 |
Correct |
97 ms |
134948 KB |
Output is correct |
13 |
Correct |
78 ms |
134952 KB |
Output is correct |
14 |
Correct |
92 ms |
134988 KB |
Output is correct |
15 |
Correct |
88 ms |
134964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
138388 KB |
Output is correct |
2 |
Correct |
98 ms |
138236 KB |
Output is correct |
3 |
Correct |
98 ms |
138016 KB |
Output is correct |
4 |
Correct |
107 ms |
138292 KB |
Output is correct |
5 |
Correct |
106 ms |
136676 KB |
Output is correct |
6 |
Correct |
100 ms |
139048 KB |
Output is correct |
7 |
Correct |
99 ms |
138000 KB |
Output is correct |
8 |
Correct |
97 ms |
136640 KB |
Output is correct |
9 |
Correct |
94 ms |
137532 KB |
Output is correct |
10 |
Correct |
123 ms |
138488 KB |
Output is correct |
11 |
Correct |
78 ms |
134988 KB |
Output is correct |
12 |
Correct |
78 ms |
135056 KB |
Output is correct |
13 |
Correct |
76 ms |
135060 KB |
Output is correct |
14 |
Correct |
77 ms |
135040 KB |
Output is correct |
15 |
Correct |
75 ms |
135016 KB |
Output is correct |
16 |
Correct |
81 ms |
135044 KB |
Output is correct |
17 |
Correct |
81 ms |
134952 KB |
Output is correct |
18 |
Correct |
78 ms |
134896 KB |
Output is correct |
19 |
Correct |
78 ms |
134932 KB |
Output is correct |
20 |
Correct |
77 ms |
134972 KB |
Output is correct |
21 |
Correct |
80 ms |
134860 KB |
Output is correct |
22 |
Correct |
78 ms |
134980 KB |
Output is correct |
23 |
Correct |
77 ms |
134984 KB |
Output is correct |
24 |
Correct |
79 ms |
134988 KB |
Output is correct |
25 |
Correct |
79 ms |
134984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
138956 KB |
Output is correct |
2 |
Correct |
118 ms |
138744 KB |
Output is correct |
3 |
Correct |
106 ms |
138428 KB |
Output is correct |
4 |
Correct |
92 ms |
138136 KB |
Output is correct |
5 |
Correct |
124 ms |
139084 KB |
Output is correct |
6 |
Correct |
123 ms |
139472 KB |
Output is correct |
7 |
Correct |
97 ms |
138180 KB |
Output is correct |
8 |
Correct |
109 ms |
138212 KB |
Output is correct |
9 |
Correct |
97 ms |
138404 KB |
Output is correct |
10 |
Correct |
97 ms |
138308 KB |
Output is correct |
11 |
Correct |
97 ms |
138044 KB |
Output is correct |
12 |
Correct |
99 ms |
138344 KB |
Output is correct |
13 |
Correct |
92 ms |
136696 KB |
Output is correct |
14 |
Correct |
98 ms |
138828 KB |
Output is correct |
15 |
Correct |
96 ms |
138056 KB |
Output is correct |
16 |
Correct |
94 ms |
136680 KB |
Output is correct |
17 |
Correct |
90 ms |
137552 KB |
Output is correct |
18 |
Correct |
98 ms |
138344 KB |
Output is correct |
19 |
Correct |
78 ms |
134976 KB |
Output is correct |
20 |
Correct |
77 ms |
134992 KB |
Output is correct |
21 |
Correct |
77 ms |
135096 KB |
Output is correct |
22 |
Correct |
80 ms |
135104 KB |
Output is correct |
23 |
Correct |
76 ms |
135000 KB |
Output is correct |
24 |
Correct |
77 ms |
135048 KB |
Output is correct |
25 |
Correct |
80 ms |
134936 KB |
Output is correct |
26 |
Correct |
77 ms |
134940 KB |
Output is correct |
27 |
Correct |
79 ms |
134932 KB |
Output is correct |
28 |
Correct |
76 ms |
134936 KB |
Output is correct |
29 |
Correct |
77 ms |
134984 KB |
Output is correct |
30 |
Correct |
79 ms |
134984 KB |
Output is correct |
31 |
Correct |
76 ms |
134860 KB |
Output is correct |
32 |
Correct |
78 ms |
134960 KB |
Output is correct |
33 |
Correct |
81 ms |
135016 KB |
Output is correct |