1 solutions
-
0
#include<bits/stdc++.h> using namespace std; const int N=3e5+10; int a[N]; map<int,int> h; int main() { int n,k; cin>>n>>k; int ans=0,cnt=0; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) { h[a[i]]++; //累加当前出现的数字的次数 if(h[a[i]]==1) cnt++; //第一次出现 int l=i-k; //计算应该出界的位置 if(l>0) //出界的位置合法 { h[a[l]]--; if(h[a[l]]==0) cnt--; //出界元素少了一个 } ans=max(ans,cnt); //更新答案 //cout<<i<<" "<<l<<" "<<cnt<<" "<<h[a[i]]<<endl; } cout<<ans; return 0; }
- 1
Information
- ID
- 2732
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 5
- Tags
- # Submissions
- 11
- Accepted
- 3
- Uploaded By