题意就是让你在N个数中找到D个连续的数,使这D个数中不同的数最小。
hard数据较大,优化到nlogn才能过。
具体怎么优化看代码吧
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 1e9;
const int N = 2e5+100;
const int M = 1e6+100;
int i,j;
int a[N],cnt[M];
int n,k,d;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(cnt, 0, sizeof cnt );
scanf("%d %d %d",&n,&k,&d);
for(i=1; i<=n; ++i)
scanf("%d",&a[i]);
int mmin,ans=0;
for(i=1; i<=d; ++i)
{
if(cnt[a[i]]==0)
++ans;
cnt[a[i]]++;
}
mmin=ans;
for(i=d+1; i<=n; i++)
{
if((--cnt[a[i-d]])==0)
--ans;
if((cnt[a[i]]++)==0)
++ans;
if(ans<mmin)
mmin=ans;
}
printf("%d\n",mmin);
}
return 0;
}
标签:cnt,const,Subscriptions,TV,596,typedef,long,int,include From: https://blog.51cto.com/u_15952369/6034931