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 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 (stderr)
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]]){
# | 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... |