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;
#define F first
#define S second
#define PB push_back
using pii = pair<int, int>;
const int MXN = 2e5+5;
int n, k, r;
int A[MXN], req[MXN], tot[MXN], cnt[MXN];
int satisfied = 0;
int main() {
cin >> n >> k >> r;
for(int i = 0; i < n; i++) cin >> A[i];
for(int i = 0; i < n; i++) tot[A[i]]++;
for(int i = 0; i < r; i++) {
int a, b; cin >> a >> b;
req[a] = b;
if(tot[a] < b) {
cout << "impossible\n";
return 0;
}
}
auto add = [](int i) {
cnt[A[i]]++;
if(cnt[A[i]] == req[A[i]]) satisfied++;
};
auto del = [](int i) {
if(cnt[A[i]] == req[A[i]]) satisfied--;
cnt[A[i]]--;
};
auto ok = []() {
return satisfied == r;
};
int lo = 1, hi = n;
while(lo < hi) {
int m = (lo+hi)/2;
satisfied = 0;
fill(cnt, cnt+k, 0);
int i;
for(i = 0; i < m; i++) add(i);
bool f = false;
if(ok()) f = true;
for(; i < n; i++) {
add(i), del(i-m);
if(ok()) f = true;
}
if(f) {
hi = m;
}
else {
lo = m+1;
}
}
cout << lo << "\n";
return 0;
}
# | 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... |