#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) {
req_done--;
if (nxt[i] <= j && 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 |
83 ms |
134984 KB |
Output is correct |
2 |
Incorrect |
79 ms |
134860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
79 ms |
135044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
98 ms |
138504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
135 ms |
139032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |