This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |