제출 #397387

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