CF190 D. Non-Secret Cypher
Question
给定一个长度为
求满足
中有至少
个数字相同的子区间的数量。
Solution
遇到这种和
有关的子区间数量问题,可以采取尺取法,尺取法每个边界判断一组,复杂度为
即
,我们在尺取边界的同时,计入当前边界所能增加的贡献值即可。
当
固定时,每次加上
,因为
在
时都满足。
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll maxn = 1e6 + 5;
const int N = 4e5 +5;
int n,k,a[N];
map<int,int> M;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
int L=1,R=0;
bool ok=false;
ll ans=0;
while(R<=n){
if(ok){
ans+=n-R+1;
if(--M[a[L++]]==k-1) ok=false;
}else{
if(++M[a[++R]]==k) ok=true;
}
}
cout<<ans<<'\n';
return 0;
}