Codeforces 190D Non-Secret Cypher【思维+RMQ+二分】

来源:互联网 发布:linux u盘启动 windows 编辑:程序博客网 时间:2024/05/16 14:34

D. Non-Secret Cypher
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their native land, the berlanders need to know exactly how many more flatland soldiers are left in the enemy's reserve. Fortunately, the scouts captured an enemy in the morning, who had a secret encrypted message with the information the berlanders needed so much.

The captured enemy had an array of positive integers. Berland intelligence have long been aware of the flatland code: to convey the message, which contained a number m, the enemies use an array of integers a. The number of its subarrays, in which there are at least kequal numbers, equals m. The number k has long been known in the Berland army so General Touristov has once again asked Corporal Vasya to perform a simple task: to decipher the flatlanders' message.

Help Vasya, given an array of integers a and number k, find the number of subarrays of the array of numbers a, which has at least k equal numbers.

Subarray a[i... j] (1 ≤ i ≤ j ≤ n) of array a = (a1, a2, ..., an) is an array, made from its consecutive elements, starting from the i-th one and ending with the j-th one: a[i... j] = (ai, ai + 1, ..., aj).

Input

The first line contains two space-separated integers nk (1 ≤ k ≤ n ≤ 4·105), showing how many numbers an array has and how many equal numbers the subarrays are required to have, correspondingly.

The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — elements of the array.

Output

Print the single number — the number of such subarrays of array a, that they have at least k equal integers.

Please do not use the %lld specifier to read or write 64-bit integers in С++. In is preferred to use the cincout streams or the %I64dspecifier.

Examples
input
4 21 2 1 2
output
3
input
5 31 2 1 1 3
output
2
input
3 11 1 1
output
6
Note

In the first sample are three subarrays, containing at least two equal numbers: (1,2,1), (2,1,2) and (1,2,1,2).

In the second sample are two subarrays, containing three equal numbers: (1,2,1,1,3) and (1,2,1,1).

In the third sample any subarray contains at least one 1 number. Overall they are 6: (1), (1), (1), (1,1), (1,1) and (1,1,1).


题目大意:


给出长度为N的一个序列,让我们统计有多少个满足条件的子区间,一个子区间需要满足的条件是:某个数字出现的次数大于等于k次。


思路:


①我们维护一个数组nex【i】,表示从当前位子开始,第k次出现a【i】这个数的位子。


②然后我们O(n)枚举起点,然后二分一个终点,对于当前枚举的区间【L,R】,如果其中存在一个位子i,使得nex【i】<=R,那么当前区间就一定包含某个数出现的次数大于等于k次,那么我们希望找到区间【L,R】内nex【i】的最小值,所以这里预处理出一个ST表即可。


时间复杂度O(nlogn);


Ac代码:

#include<stdio.h>#include<string.h>#include<math.h>#include<vector>#include<map>using namespace std;int a[450000];vector<int>mp[450000];int num[450000];int nex[450000];int minn[400005][42];int n,k;void ST(){    int len=floor(log10(double(n))/log10(double(2)));    for(int j=1;j<=len;j++)    {        for(int i=1;i<=n+1-(1<<j);i++)        {            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);        }    }}int getminn(int a,int b){    int len= floor(log10(double(b-a+1))/log10(double(2)));    return min(minn[a][len], minn[b-(1<<len)+1][len]);}int main(){    while(~scanf("%d%d",&n,&k))    {        int cnt=0;        map<int,int>s;        for(int i=1;i<=n;i++)mp[i].clear();        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            if(s[a[i]]==0)s[a[i]]=++cnt;            a[i]=s[a[i]];            mp[a[i]].push_back(i);            num[i]=mp[a[i]].size()-1;        }        for(int i=1;i<=n;i++)        {            nex[i]=0x3f3f3f3f;            if(mp[a[i]].size()>num[i]+k-1)            {                nex[i]=mp[a[i]][num[i]+k-1];            }        }        for(int i=1;i<=n;i++)minn[i][0]=nex[i];        ST();        long long int output=0;        for(int i=1;i<=n;i++)        {            int pos=-1;            int l=i;            int r=n;            while(r-l>=0)            {                int mid=(l+r)/2;                if(getminn(i,mid)<=mid)                {                    pos=mid;                    r=mid-1;                }                else l=mid+1;            }            if(pos==-1)continue;            output+=(n-pos+1);        }        printf("%lld\n",output);    }}