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>
#define N 200050
using namespace std;
int n, r, k, qtd[N], v[N], T[N], ans = 2000000000;
int esq, dir;
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k>>r;
for(int i = 1; i <= n; i++) cin>>v[i];
for(int i = 0; i < r; i++)
{
int x, t;
cin>>x>>t;
qtd[x] = t;
}
int ini = 1, fim = n, mid, best = -1;
while(fim >= ini)
{
bool nop = 0;
mid = (ini + fim)/2;
memset(T, 0, sizeof T);
for(int i = 1; i <= mid; i++) T[v[i]] ++;
for(int i = 0; i < k; i++)
if(T[i] < qtd[i]) nop = 1;
if(nop) ini = mid + 1;
else fim = mid - 1, best = mid;
}
if(best == -1)
{
cout<<"impossible\n";
return 0;
}
esq = 1, dir = best, ans = best;
memset(T, 0, sizeof T);
for(int i = 1; i <= best; i++) T[v[i]] ++;
for(; dir <= n; dir++)
{
while(T[v[esq]] - 1 >= qtd[v[esq]] && esq <= dir) T[v[esq]] --, esq ++;
ans = min(ans, (dir - esq + 1));
T[v[dir + 1]] ++;
}
if(ans < 2000000000 && best != -1)cout<<ans<<'\n';
else cout<<"impossible\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... |