이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 5;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, r, q;
    cin >> n >> r >> q;
    int dna[n];
    for(int i = 0; i < n; i++) {
        cin >> dna[i];
    }
    vector<int> need(r+1, INF), cnt(r+1, 0);
    for(int i = 0; i < q; i++) {
        int b;
        cin >> b;
        cin >> need[b];
    }
    int good = 0, ans = INF;
    set<int> begins, ends;
    vector<int> pos[r+1];
    for(int i = 0; i < n; i++) {
        int c = dna[i];
        cnt[c]++;
        pos[c].push_back(i);
        if (cnt[c] == need[c]) {
            good++;
            begins.insert(pos[c][0]);
            ends.insert(pos[c][need[c]-1]);
        }
        if (cnt[c] > need[c]) {
            begins.erase(pos[c][cnt[c]-need[c]-1]);
            ends.erase(pos[c][cnt[c]-2]);
            begins.insert(pos[c][cnt[c]-need[c]]);
            ends.insert(pos[c][cnt[c]-1]);
        }
        if (good == q) {
            ans = min(ans, *ends.rbegin() - *begins.begin() + 1);
        }
    }
    if (ans == INF) {
        cout << "impossible";
    } else {
        cout << ans << "\n";
    }
//    cout << good << "\n";
//    for(auto i : ends) {
//        cout << i << "\n";
//    }
    return 0;
}
//~ check for overflows
| # | 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... |