#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, dic, r;
cin >> n >> dic >> r;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = n + 1;
vector<int> p(r + 1);
vector<int> k(r + 1);
vector<int> have(n + 1, 0);
for(int i = 1; i <= r; i++) {
cin >> p[i] >> k[i];
have[p[i]] = 1;
}
vector<int> pos[n + 1];
vector<int> some(n + 1, 1e9);
for(int i = 1; i <= n; i++) {
pos[a[i]].push_back(i);
}
for(int i = 1; i <= r; i++) {
some[p[i]] = pos[p[i]].size() - 1;
}
vector<int> c(n + 1, n + 1);
set<pair<int,int>> s; // {i + k - 1, a[i]};
vector<int> cal(n + 1, 0);
for(int i = 1; i <= r; i++) {
cal[p[i]] = pos[p[i]].back() + k[i] - 1;
}
vector<int> W(n + 1, 0);
for(int i = 1; i <= r; i++) {
W[p[i]] = k[i];
}
for(int i = n; i >= 1; i--) {
if(have[a[i]]) {
int X = cal[a[i]];
int Y = a[i];
if(i != n) {
auto it = s.lower_bound({X, Y});
if(it != s.end()) {
if((*it).first == X && (*it).second == Y) {
s.erase(it);
}
}
}
if(some[a[i]] + W[a[i]] - 1 >= pos[a[i]].size()) {
}
else {
s.insert({pos[a[i]][some[a[i]] + W[a[i]] - 1], a[i]});
}
cal[a[i]] = pos[a[i]][some[a[i]] + W[a[i]] - 1];
some[a[i]]--;
if(s.size() < r) {
continue;
}
int d = (*--s.end()).first;
if(d > n || s.size() < r) {
}
else {
ans = min(ans, d - i + 1);
}
}
}
if(ans == n + 1) {
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... |