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;
using ll = long long;
const int inf = 1e9;
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, k, m, ans = inf;
cin >> n >> k >> m;
vector<int> a(n), need(k), have(k);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) {
int x, c;
cin >> x >> c;
need[x] = c;
}
int cnt = k - m;
for (int l = 0, r = 0; r < n; r++) {
if (have[a[r]] >= need[a[r]]) cnt--;
have[a[r]]++;
if (have[a[r]] >= need[a[r]]) cnt++;
if (cnt != k) continue;
while (cnt == k) {
if (have[a[l]] >= need[a[l]]) cnt--;
have[a[l]]--;
if (have[a[l]] >= need[a[l]]) cnt++;
l++;
}
ans = min(ans, r - l + 2);
}
if (ans == inf) cout << "impossible";
else cout << ans;
}
# | 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... |