제출 #957413

#제출 시각아이디문제언어결과실행 시간메모리
957413vjudge1 Martian DNA (BOI18_dna)C++17
100 / 100
343 ms28308 KiB
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>

const ll N = 200069;
const ll mod = 1e9 + 7;

int tc;
ll n, k, r;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll i, j;
    
    cin >> n >> k >> r;
    
    ll a[n+5];
    for(i = 1; i <= n; i++)
    {
        cin >> a[i];
    }

    map<ll, ll> need, cur;
    for(i = 1; i <= r; i++)
    {
        ll x, y;
        cin >> x >> y;
        need[x] = y;
    }
    
    ll ans = 1e18, sz = max(0ll, k - r);
    j = 1;
    for(i = 1; i <= n; i++)
    {
        ++cur[a[i]];
        if(cur[a[i]] == need[a[i]])
        {
            ++sz;
        }
        while(j <= n and sz == k)
        {
            ans = min(ans, i - j + 1);
            --cur[a[j]];
            if(cur[a[j]] == need[a[j]] - 1) --sz;
            ++j;
        }
    }

    if(ans == 1e18)
    {
        cout << "impossible\n";
    }
    else cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...