Submission #1349155

#TimeUsernameProblemLanguageResultExecution timeMemory
1349155iamhereforfun Martian DNA (BOI18_dna)C++20
100 / 100
15 ms2776 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 2e5 + 5;
const int M = 1 << 5;
const int K = 19;
const int LG = 20;
const long long INF = 3e18;
const int C = 26;
const int B = 1000;
const int MOD = 1e9 + 7;

int n, k, q, a[N], cnt[N], need[N], ans, l, r, bad;

inline void solve()
{
    cin >> n >> k >> q;
    memset(need, -1, sizeof(need));
    bad = 0;
    for (int x = 1; x <= n; x++)
    {
        cin >> a[x];
    }
    ans = n + 1;
    for (int x = 0; x < q; x++)
    {
        int a, b;
        cin >> a >> b;
        need[a] = b;
    }
    bad = q;
    l = 1, r = 1;
    while (r <= n)
    {
        cnt[a[r]]++;
        if (need[a[r]] == cnt[a[r]])
        {
            bad--;
        }
        while (!bad)
        {
            ans = min(ans, r - l + 1);
            cnt[a[l]]--;
            if (need[a[l]] > cnt[a[l]])
                bad++;
            l++;
        }
        r++;
    }
    if(ans == n + 1) cout << "impossible";
    else cout << ans;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    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...