#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ins insert
#define pb push_back
#define endl '\n'
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
int nxt(){ int x;cin>>x; return x; }
const int mod = 1e9 +7, sze = 400 +23, inf = LLONG_MAX, LG = 20;
void fast(){
int n,k,rq;
cin>>n>>k>>rq;
map<int,vector<int>> var;
vector<int> mal(n+10,-23),arr(n+1,0);
generate(arr.begin()+1,arr.end(),nxt);
vector<int> lazim(n+10,0);
vector<int> last(n+10,0);
int goturmeli = rq;
while(rq--){
int b,q;
cin>>b>>q;
lazim[b]=q;
}
for(int i=1;i<=n;i++){
if(lazim[arr[i]]){
if(!var[arr[i]].empty()){
last[i]=var[arr[i]].back();
}
var[arr[i]].pb(i);
if(var[arr[i]].size()>=lazim[arr[i]]){
mal[i]=var[arr[i]][ var[arr[i]].size() - lazim[arr[i]] ];
}
}
}
int l =1;
int r = n;
int ans=-1;
while(l<=r){
int mid = (l+r)/2;
multiset<int> q;
int gotur = 0;
vector<int> got(n+23,0),sil(n+23,0);
for(int i =1;i<=mid;i++){
if(lazim[arr[i]]){
if(++got[arr[i]]==1){
++gotur;
}
if(last[i]){
sil[last[i]]=1;
q.erase(q.find( mal[last[i]] ));
}
q.ins(mal[i]);
}
}
if(gotur == goturmeli && (* q.begin()) >0 ){
r = mid-1;
ans = mid;
}
else{
// for(auto v:q){
// cout<<v<<" ";
// }
// cout<<endl;
bool flag=false;
for(int i = mid+1;i<=n;i++){
if(lazim[ arr[ i - mid] ] ){
if(--got[arr[i-mid]] == 0){
--gotur;
}
if(!sil[i-mid]){
q.erase( q.find(mal[ i -mid]));
}
}
if(lazim[arr[i]]){
if(last[i] > i -mid && (!sil[last[i]] ) ){
sil[last[i]]=1;
q.erase(mal[last[i]]);
}
if(++got[arr[i]] == 1){
++gotur;
}
q.ins(mal[i]);
}
if(gotur == goturmeli && (*(q.begin())) > i - mid){
flag=true;
break;
}
}
if(flag){
ans = mid;
r = mid-1;
}
else{
l = mid+1;
}
}
}
if(ans==-1){
putr("impossible");
}
putr(ans);
}
signed main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int tt = 1;
// cin>>tt;
while(tt--){
fast();
}
return 0;
}
Compilation message
dna.cpp: In function 'void fast()':
dna.cpp:34:34: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} [-Wsign-compare]
34 | if(var[arr[i]].size()>=lazim[arr[i]]){
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Execution timed out |
2072 ms |
336 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
3 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
4 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
508 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Execution timed out |
2063 ms |
336 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
11760 KB |
Output is correct |
2 |
Correct |
121 ms |
12168 KB |
Output is correct |
3 |
Correct |
82 ms |
11856 KB |
Output is correct |
4 |
Correct |
152 ms |
11820 KB |
Output is correct |
5 |
Correct |
55 ms |
10912 KB |
Output is correct |
6 |
Incorrect |
165 ms |
12256 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
609 ms |
25928 KB |
Output is correct |
2 |
Correct |
531 ms |
23624 KB |
Output is correct |
3 |
Correct |
188 ms |
15140 KB |
Output is correct |
4 |
Correct |
58 ms |
12304 KB |
Output is correct |
5 |
Execution timed out |
2078 ms |
23624 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |