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>
template <class T>
using Vec = std::vector<T>;
int main() {
int N, K, R;
std::cin >> N >> K >> R;
Vec<int> A(N);
for (auto& x: A) {
std::cin >> x;
}
Vec<Vec<int>> pos(K);
for (int i = 0; i < N; ++i) {
pos[A[i]].push_back(i);
}
Vec<int> need(K);
while (R--) {
int b;
std::cin >> b;
std::cin >> need[b];
}
int right = 0;
for (int i = 0; i < K; ++i) {
if (need[i] == 0) {
continue;
}
if (need[i] > (int) pos[i].size()) {
std::cout << "impossible\n";
return 0;
}
right = std::max(right, pos[i][need[i] - 1]);
}
int ans = right + 1;
for (int left = 1; left < N; ++left) {
const auto x = A[left - 1];
if (need[x] > 0) {
need[x] += 1;
if (need[x] > (int) pos[x].size()) {
break;
}
right = std::max(right, pos[x][need[x] - 1]);
}
ans = std::min(ans, right - left + 1);
}
std::cout << ans << '\n';
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... |