Codeforces Round #596 (Div. 2)B2. TV Subscriptions(Hard Version)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/w_udixixi/article/details/102772371

Codeforces Round #596 (Div. 2)B2. TV Subscriptions(Hard Version)

Topic: http://codeforces.com/contest/1247/problem/B2

The meaning of problems : given a sequence of numbers K can be selected, the selected digital marker, d seeking to have consecutive numbers marked in the original sequence of the smallest K

Practice : the length of the recording interval d, the number of different digital presence. Found that the length of the interval is certain, we can continue past the recurrence

AC Code :

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#include<set>
#define rep(x,a,b) for(int x=a;x<=b;x++)
using namespace std;
const int maxn=2e6+7;
int a[maxn];
int num[maxn];
int main()
{
    int t,n,k,d;
    scanf("%d",&t);
    while(t--)
    {
        int ans=0,maxx=0;
        scanf("%d%d%d",&n,&k,&d);
        rep(i,1,n){scanf("%d",&a[i]);num[a[i]]=0;}
        for (int i=1;i<=d;i++)
        {
            if (num[a[i]]==0)ans++;
            num[a[i]]++;
        }
        int minn=ans;
        for (int i=d+1;i<=n;i++)
        {
            if (a[i]==a[i-d])continue;
            else
            {
                //区间右端点
                if (num[a[i]]==0) ans++;
                num[a[i]]++;
                //区间左端点
                if (num[a[i-d]]==1) ans--;
                num[a[i-d]]--;
            }
            minn=min(minn,ans);
        }
        cout<<minn<<endl;
    }
}

Guess you like

Origin blog.csdn.net/w_udixixi/article/details/102772371