Submission #1054611

#TimeUsernameProblemLanguageResultExecution timeMemory
1054611cot7302 Martian DNA (BOI18_dna)C++17
100 / 100
18 ms4700 KiB
#include <bits/stdc++.h>
#define ALL(X) begin(X), end(X)
using namespace std;
using i64 = long long;

template <class T>
using vec = vector<T>;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, k, R; cin >> n >> k >> R;
    vec<int> D(n);
    for (auto& x : D) cin >> x;
    vec<int> cnt(k);
    vec<int> need(k);
    while (R--) {
        int b, q; cin >> b >> q;
        need[b] = q;
    }
    int ans = 1e9;
    for (int i = 0, r = 0, ok = 1; i < n; i++) {
        if (i == 0) {
            for (int b = 0; b < k; b++) {
                while (r < n && cnt[b] < need[b])
                    cnt[D[r++]] += 1;
                ok &= cnt[b] >= need[b];
            }
        }
        else {
            int b = D[i - 1];
            cnt[b] -= 1;
            while (r < n && cnt[b] < need[b])
                cnt[D[r++]] += 1;
            ok &= cnt[b] >= need[b];
        }
        if (!ok) break;
        ans = min(ans, r - i);
    }
    if (ans == 1e9) cout << "impossible\n";
    else cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...