제출 #577059

#제출 시각아이디문제언어결과실행 시간메모리
577059Pietra Martian DNA (BOI18_dna)C++14
100 / 100
54 ms4480 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...