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;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
const int maxn = 2e5 + 5;
int req[maxn];
signed main(void){
fastio;
int n, k, r;
cin>>n>>k>>r;
vector<int> a(n);
for(int i = 0; i < n; i++) cin>>a[i];
for(int i = 0; i < r; i++){
int b, t;
cin>>b>>t;
req[b] = t;
}
int L = 0, R = n + 1;
while(L + 1 < R){
int m = (L + R) / 2;
int ok = k - r;
vector<int> cur(k);
for(int i = 0; i < m; i++){
cur[a[i]] ++;
if(cur[a[i]] == req[a[i]]) ok++;
}
if(ok == k){
R = m;
continue;
}
int pass = 0;
for(int i = m; i < n; i++){
cur[a[i]] ++;
if(cur[a[i]] == req[a[i]]) ok++;
cur[a[i - m]] --;
if(cur[a[i - m]] == req[a[i - m]] - 1) ok--;
if(ok == k) pass = 1;
}
if(pass) R = m;
else L = m;
}
if(R == n + 1) cout<<"impossible\n";
else cout<<R<<"\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... |