제출 #442511

#제출 시각아이디문제언어결과실행 시간메모리
442511abc864197532 Martian DNA (BOI18_dna)C++17
100 / 100
56 ms4548 KiB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define test(args...) abc("[" + string(#args) + "]", args)
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cerr << *l << " \n"[++l == r];
}
const int mod = 1e9 + 7, N = 200000;

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, k;
    cin >> n >> m >> k;
    vector <int> a(n), need(m, 0);
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0, v, c; i < k; ++i) {
        cin >> v >> c;
        need[v] = c;
    }
    vector <int> cnt(m, 0);
    int ok = k, ans = n + 1;
    auto add = [&](int v) {
        cnt[v]++;
        if (cnt[v] == need[v]) ok--;
    };
    auto del = [&](int v) {
        if (cnt[v] == need[v]) ok++;
        cnt[v]--;
    };
    for (int i = 0, j = 0; i < n; ++i) {
        while (ok && j < n) {
            add(a[j++]);
        }
        if (!ok) ans = min(ans, j - i);
        del(a[i]);
    }
    if (ans == n + 1) cout << "impossible\n";
    else cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...