
滑動窗口、資料結構
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
using namespace std;
//declare
int n,k;
vector<int> v;
map<int,int> cnt;
//
signed main(){
fastio;
cin>>n>>k;
for(int i=0;i<n;i++){
int x; cin>>x;
v.push_back(x);
}
int L = 0, R = 0, now = 0, ans = 0;
while(R < n){
if(now <= k){
ans += (R-L);
if(cnt[v[R++]]++ == 0) now++;
}
else{
if(--cnt[v[L++]] == 0) now--;
}
}
while(L < R){
if(now <= k) ans++;
if(--cnt[v[L++]] == 0) now--;
}
cout<<ans<<"\n";
return 0;
}

發佈留言