Submission #396726

#TimeUsernameProblemLanguageResultExecution timeMemory
396726danielcm585 Martian DNA (BOI18_dna)C++14
40 / 100
2091 ms10948 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second

typedef long long ll;
typedef pair<int,int> ii;

int n, k, r;
vector<int> a;
map<int,int> need, cnt;

bool can(int mid) {
    cnt.clear();
    int full = 0;
    for (int i = 1; i <= n; i++) {
        cnt[a[i]]++;
        if (need.count(a[i]) && cnt[a[i]] == need[a[i]]) full++;
        if (i-mid >= 1) {
            cnt[a[i-mid]]--;
            if (need.count(a[i-mid]) && cnt[a[i-mid]] == need[a[i-mid]]-1) full--;
        }
        if (i >= mid && full >= r) return 1;
    }
    return 0;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> k >> r;
    a = vector<int>(n+2);
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= r; i++) {
        int b, q; cin >> b >> q;
        need[b] = q;
    }
    int ans = -1;
    for (int l = 1, r = n; l <= r; ) {
        int mid = (l+r)/2;
        // cout << mid << " -> " << can(mid) << '\n';
        if (can(mid)) {
            ans = mid;
            r = mid-1;
        }
        else l = mid+1;
    }
    if (ans == -1) cout << "impossible\n";
    else 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...