B2 - TV Subscriptions (Hard Version)

Topic connection: https://codeforces.com/contest/1247/problem/B2

Solution: double pointer ,, a head, a tail, into the head, tail exit, a first recording start 1 to k, and record the number of each number appears, and then into the tail in the head, is determined sequentially.

#include<bits/stdc++.h>
using namespace std;
const int N=2E5+7;
map<int ,int >mp;
int arr[N];
void solve(){
    mp.clear();
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
    int ans=0;
    for(int i=1;i<=k;i++){
        if(mp[arr[i]]==0) ans++;
        mp[arr[i]]++;
    }
    int sum=ans;
    for(int j=k+1,i=1;j<=n;j++,i++){
        if(mp[arr[i]]==1) ans--;
        mp[arr[i]]--;
        
        if(mp[arr[j]]==0) ans++;
        mp[arr[j]]++;
        sum=min(ans,sum);    
    }
    cout<<sum<<endl;
}
int main(){
    int t;
    cin>>t;
    while(t--) solve();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Accepting/p/11759305.html