Submission #622297

#TimeUsernameProblemLanguageResultExecution timeMemory
622297Urvuk3 Martian DNA (BOI18_dna)C++17
100 / 100
33 ms4496 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
ll INF=1e9,LINF=1e18,MOD=1e9+7;
#define fi first
#define se second
#define pii pair<int,int>
#define mid ((l+r)/2)
#define sz(a) (int((a).size()))
#define all(a) a.begin(),a.end()
#define endl "\n"
#define PRINT(x) cerr<<#x<<'-'<<x<<endl<<flush;
#define pb push_back
#define PRINTvec(x) { cerr<<#x<<"-"; for(auto h:x) cerr<<h<<" "; cerr<<endl; }

void Solve(){
    int N,K,R; cin>>N>>K>>R;
    vector<int> D(N+1);
    for(int i=1;i<=N;i++) cin>>D[i];
    vector<int> treba(K,0);
    for(int i=1;i<=R;i++){
        int x,y; cin>>x>>y;
        treba[x]=y;
    }
    vector<int> kolko(K,0);
    int i=1,j=1; kolko[D[1]]++;
    int aktivnih_dovoljno=0;
    for(int i=0;i<K;i++){
        if(kolko[i]>=treba[i]) aktivnih_dovoljno++;
    }
    int res=INF;
    while(i<=j){
        if(aktivnih_dovoljno==K){
            res=min(res,j-i+1);
            int levi=D[i];
            i++;
            if(i>j) break;
            int staro_kolko=kolko[levi],novo_kolko=--kolko[levi];
            if(staro_kolko>=treba[levi] && novo_kolko<treba[levi]){
                aktivnih_dovoljno--;
            }
        }
        else if(j==N){
            int levi=D[i];
            i++;
            if(i>j) break;
            int staro_kolko=kolko[levi],novo_kolko=--kolko[levi];
            if(staro_kolko>=treba[levi] && novo_kolko<treba[levi]){
                aktivnih_dovoljno--;
            }
        }
        else{
            j++;
            if(i>j) break;
            int desni=D[j];
            int staro_kolko=kolko[desni],novo_kolko=++kolko[desni];
            if(staro_kolko<treba[desni] && novo_kolko>=treba[desni]){
                aktivnih_dovoljno++;
            }
        }
    }
    cout<<(res!=INF ? to_string(res) : "impossible")<<endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t; t=1;
    //cin>>t;
    while(t--){
        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...