This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define maxn 200005
ll n,k,r;
ll a[maxn];
ordered_set v[maxn];
ll c[maxn];
ll b[maxn];
int main(){
daj_mi_malo_vremena
cin >> n >> k >> r;
for(ll i = 1;i<=n;i++) cin >> a[i];
for(ll i = 1;i<=n;i++) a[i]++;
for(ll i = 1;i<=n;i++) v[a[i]].insert(i);
for(ll i = 1;i<=r;i++){
ll x,y; cin >> x >> y;
x++;
b[i] = x;
c[x] = y;
}
ll tr = 0;
for(ll i = 1;i<=r;i++){
ll x = b[i];
if(sz(v[x])<c[x]){
cout<<"impossible"<<endl;
return 0;
}
tr = max(tr,*v[x].find_by_order(c[x]-1));
}
ll ans = llinf;
for(ll i = 1;i<=n;i++){
ans = min(tr-i+1,ans);
ll x = a[i];
if(c[x]==0) continue;
c[x]++;
if(sz(v[x])<c[x]) break;
tr = max(tr,*v[x].find_by_order(c[x]-1));
}
cout<<ans<<endl;
return 0;
}
/*
5 2 2
0 1 1 0 1
0 1
1 1
13 4 3
1 1 3 2 0 1 2 0 0 0 0 3 1
0 2
2 1
1 2
*/
# | 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... |