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 endl '\n'
using namespace std;
const int MAXN = 2e5 + 10;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int n, k, r;
int a[MAXN], cnt[MAXN], need[MAXN];
int total_need = 0;
void read()
{
cin >> n >> k >> r;
for (int i = 1; i <= n; ++ i)
{
cin >> a[i];
}
int x, wx;
for (int i = 1; i <= r; ++ i)
{
cin >> x >> wx;
total_need += wx;
need[x] = wx;
}
}
void solve()
{
int j = 0;
int were_needed = 0;
for (int i = 1; i <= n && !j; ++ i)
{
if(cnt[a[i]] < need[a[i]])
{
were_needed ++;
}
cnt[a[i]] ++;
if(were_needed == total_need)
j = i;
}
if(were_needed != total_need)
{
cout << "impossible" << endl;
return;
}
int ans = j;
for (int i = 2; i <= n; ++ i)
{
cnt[a[i-1]] --;
if(cnt[a[i-1]] < need[a[i-1]])
{
int ok = 0;
for (int jj = j+1; jj <= n && !ok; ++ jj)
{
cnt[a[jj]] ++;
if(a[jj] == a[i-1])
{
ok = 1;
j = jj;
}
}
if(!ok)break;
}
ans = min(ans, j - i + 1);
}
cout << ans << endl;
}
int main()
{
speed();
read();
solve();
return 0;
}
# | 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... |