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 ll long long
#define pb push_back
#define fi first
#define se second
const int mod=998244353, maxn=2e5+5;
int n, k, r, a[maxn], tipe, butuh[maxn], ki, ka, mid, res, unik, cukup, cnt[maxn], x;
bool penting[maxn], bisa;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> k >> r;
for(int i=1; i<=n; i++) cin >> a[i];
for(int i=1; i<=r; i++) {
cin >> tipe >> x;
penting[tipe]=true;
butuh[tipe]=x;
}
ki=1, ka=n, res=-1;
while(ki<=ka) {
mid=(ki+ka)/2, bisa=false, unik=cukup=0;
for(int i=0; i<k; i++) cnt[i]=0;
for(int i=1; i<=n; i++) {
if(i>mid) {
cnt[a[i-mid]]--;
if(!cnt[a[i-mid]]) unik--;
if((cnt[a[i-mid]]==(butuh[a[i-mid]]-1))&&penting[a[i-mid]]) cukup--;
}
cnt[a[i]]++;
if(cnt[a[i]]==1) unik++;
if((cnt[a[i]]==butuh[a[i]])&&penting[a[i]]) cukup++;
if(unik==k&&cukup==r) bisa=true;
}
if(bisa) {
res=mid;
ka=mid-1;
} else ki=mid+1;
}
if(res==-1) cout << "impossible";
else cout << res;
cout << '\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... |