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 maxn = 2e5 + 5 ;
int n, k, r, freq[maxn], v[maxn], qtd[maxn] ;
bool check(int x){
// cout << x << ":\n" ;
for(int i = 0 ; i <= k ; i++) freq[i] = 0 ;
int ct = 0 ;
for(int i = 1 ; i <= x ; i++){
freq[v[i]]++ ;
if(freq[v[i]] == qtd[v[i]]) ct++ ;
}
if(r == ct) return 1 ;
for(int i = x + 1 ; i <= n ; i++){
if(freq[v[i-x]] == qtd[v[i-x]]) ct-- ;
freq[v[i-x]]-- ; freq[v[i]]++ ;
if(freq[v[i]] == qtd[v[i]]) ct++ ;
if(r == ct) return 1 ;
}
if(r == ct) return 1 ;
return 0 ;
}
int main(){
ios_base::sync_with_stdio(false) ; cin.tie(NULL) ;
cin >> n >> k >> r ;
for(int i = 1 ; i <= n ; i++) cin >> v[i] ;
for(int i = 1 ; i <= r ; i++){
int v, f ; cin >> v >> f ;
qtd[v] = f ;
}
int ini = 1, fim = n, mid, best = -1 ;
while(ini <= fim){
mid = (ini + fim)>>1 ;
if(check(mid)) best = mid, fim = mid - 1 ;
else ini = mid + 1 ;
}
if(best == -1) cout << "impossible\n" ;
else cout << best << "\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... |