Submission #915866

#TimeUsernameProblemLanguageResultExecution timeMemory
915866n3rm1n Martian DNA (BOI18_dna)C++17
100 / 100
24 ms4940 KiB
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = 2e5 + 10;
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int n, k, r;
int a[MAXN], cnt[MAXN], need[MAXN];
int total_need = 0;
void read()
{
    cin >> n >> k >> r;
    for (int i = 1; i <= n; ++ i)
    {
        cin >> a[i];
    }
    int x, wx;
    for (int i = 1; i <= r; ++ i)
    {
        cin >> x >> wx;
        total_need += wx;
        need[x] = wx;
    }
}
void solve()
{
    int j = 0;
    int were_needed = 0;
    for (int i = 1; i <= n && !j; ++ i)
    {
        if(cnt[a[i]] < need[a[i]])
        {
            were_needed ++;
        }
        cnt[a[i]] ++;
        if(were_needed == total_need)
            j = i;
    }
    if(were_needed != total_need)
    {
        cout << "impossible" << endl;
        return;
    }
    int ans = j;
    for (int i = 2; i <= n; ++ i)
    {
        cnt[a[i-1]] --;
        if(cnt[a[i-1]] < need[a[i-1]])
        {

            int ok = 0;
            for (int jj = j+1; jj <= n && !ok; ++ jj)
            {
                cnt[a[jj]] ++;
                if(a[jj] == a[i-1])
                {
                    ok = 1;
                    j = jj;
                }
            }
            if(!ok)break;
        }
        ans = min(ans, j - i + 1);
    }
    cout << ans << endl;
}
int main()
{
    speed();

    read();
    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...