제출 #862437

#제출 시각아이디문제언어결과실행 시간메모리
862437The_Samurai Martian DNA (BOI18_dna)C++17
100 / 100
26 ms4700 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...