Colorful Candies 题解 [at_abc210 C map离散化 滑动窗口]
题目:Colorful Candies
地址:at_abc210_c
解析
参考源码
#include<iostream>
#include<string>
#include<map>
using namespace std;
const int MAXN = 3e5;
map<int,int> m;
int nums[MAXN+10];
int c[MAXN+10];
int main()
{
int N,K;
cin>>N>>K;
nums[N-K+1] = 0;
int ns = 0;
int maxx = 0;
for(int i=1;i<=N;i++){
cin>>c[i];
if(i<=K){
if(m.count(c[i])==0){
m[c[i]]=1;
ns++;
}
else{
m[c[i]]++;
}
}
}
maxx = ns;
for(int i=K+1;i<=N;i++){
m[c[i-K]]--;
if(m[c[i-K]]==0){
ns--;
m.erase(c[i-K]);
}
if(m.count(c[i])==0){
m[c[i]]=1;
ns++;
if(ns>maxx) maxx = ns;
}
else{
m[c[i]]++;
}
}
cout<<maxx<<endl;
return 0;
}
|