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;
signed main () {
int n, k, q;
cin >> n >> k >> q;
vector <int> a(n);
for(int i = 0; i < n; i ++) cin >> a[i];
int cnt[k];
memset(cnt, 0, sizeof(cnt));
while(q --) {
int val, sz;
cin >> val >> sz;
cnt[val] += sz;
}
int mn = 1e9, pref[k];
memset(pref, 0, sizeof(pref));
int l = 0, r = 0;
for(;r<n;r++){
pref[a[r]] ++;
for(int j = 0; j < k; j ++) {
if(cnt[j] <= pref[j]) continue;
goto end;
}
break;
end:;
}
if(r == n) {
cout << "impossible";
return 0;
}
mn = r-l+1;
for(;r < n;) {
while(1) {
if(l == n) {
break;
// goto end2;
}
pref[a[l]] --;
// cout << pref[a[l]] << " ";
if(pref[a[l]] < cnt[a[l]]) {
pref[a[l]] ++;
// l--;
break;
}
l ++;
}
// cout << l << " " << r << "\n";
mn = min(mn, r-l+1);
r++;
if(r == n) break;
pref[a[r]] ++;
}
cout << mn;
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... |