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 int long long
#define sp << " " <<
#define nl << "\n"
int n, k, r, a[1<<18], b[1<<18];
bool possible(int x){
int cnt[r], curr = 0;
for(int i=0; i<r; ++i) cnt[i] = b[i];
for(int i=0; i<n; ++i){
if(i-x>=0 and a[i-x]>=0) curr -= ++cnt[a[i-x]]==1;
if(a[i]>=0) curr += --cnt[a[i]]==0;
if(curr==r) return true;
}
return false;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k >> r;
for(int i=0; i<n; ++i) cin >> a[i];
vector<int> req(k, 0), id(k, -1);
for(int i=0, inp; i<r; ++i){
cin >> inp >> req[inp];
}
int currID = 0;
for(int i=0; i<k; ++i){
if(req[i]) b[currID] = req[i], id[i] = currID++;
}
for(int i=0; i<n; ++i){
if(req[a[i]]) a[i] = id[a[i]];
else a[i] = -1;
}
if(!possible(n)){
cout << "impossible" nl;
return 0;
}
int low = 1, high = n;
while(low<high){
int mid = (low+high)/2;
if(possible(mid)) high = mid;
else low = mid+1;
}
cout << low nl;
}
# | 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... |