Codeforces 190D Non-Secret Cypher

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 numberm, the enemies use an array of integers a. The number of its subarrays, in which there are at least k equal numbers, equals m. The numberk 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 numberk, find the number of subarrays of the array of numbersa, 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 thei-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 n,k (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 integersai (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 thecin, cout streams or the%I64d specifier.

Examples
Input
4 2
1 2 1 2
Output
3
Input
5 3
1 2 1 1 3
Output
2
Input
3 1
1 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,求有数字至少重复出现k次的连续子数组的个数。

解题思路:

    十分明显应该用尺取法(有人叫做双指针),从头往尾扫,如果当前区间不满足条件尾指针向后移,如果满足结果加上尾指针到数组尾的距离加一,表示包含这个子数组的还没有统计的子数组的个数。

    具体实现时和我常用的尺取写法不太一样。

    注意这题的结果会炸int。

附AC代码:

[cpp] view plain copy
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #define LL long long  
  7. using namespace std;  
  8.   
  9. const int maxn=400000+3;  
  10. int n,k,a[maxn];  
  11. LL ans;  
  12. map<int,int> save;//统计各个数字出现的次数  
  13.   
  14. int main()  
  15. {  
  16.     scanf("%d%d",&n,&k);  
  17.     for(int i=0;i
  18.         scanf("%d",&a[i]);  
  19.     int s=0,e=-1;//s是头指针,e是尾指针  
  20.     bool flag=false;//标记区间此时是否满足条件  
  21.     while(e
  22.     {  
  23.         if(flag)  
  24.         {  
  25.             ans+=n-e;  
  26.             if(save[a[s]]==k)  
  27.                 flag=false;  
  28.             --save[a[s++]];  
  29.         }  
  30.         else  
  31.         {  
  32.             ++save[a[++e]];  
  33.             if(save[a[e]]==k)  
  34.                 flag=true;  
  35.         }  
  36.     }  
  37.     printf("%I64d\n",ans);  
  38.       
  39.     return 0;  



另一重方法

思路:


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


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


时间复杂度O(nlogn);


Ac代码:

[cpp]   view plain  copy
  1. #include  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. using namespace std;  
  7. int a[450000];  
  8. vector<int>mp[450000];  
  9. int num[450000];  
  10. int nex[450000];  
  11. int minn[400005][42];  
  12. int n,k;  
  13. void ST()  
  14. {  
  15.     int len=floor(log10(double(n))/log10(double(2)));  
  16.     for(int j=1;j<=len;j++)  
  17.     {  
  18.         for(int i=1;i<=n+1-(1<
  19.         {  
  20.             minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);  
  21.         }  
  22.     }  
  23. }  
  24. int getminn(int a,int b)  
  25. {  
  26.     int len= floor(log10(double(b-a+1))/log10(double(2)));  
  27.     return min(minn[a][len], minn[b-(1<
  28. }  
  29. int main()  
  30. {  
  31.     while(~scanf("%d%d",&n,&k))  
  32.     {  
  33.         int cnt=0;  
  34.         map<int,int>s;  
  35.         for(int i=1;i<=n;i++)mp[i].clear();  
  36.         for(int i=1;i<=n;i++)  
  37.         {  
  38.             scanf("%d",&a[i]);  
  39.             if(s[a[i]]==0)s[a[i]]=++cnt;  
  40.             a[i]=s[a[i]];  
  41.             mp[a[i]].push_back(i);  
  42.             num[i]=mp[a[i]].size()-1;  
  43.         }  
  44.         for(int i=1;i<=n;i++)  
  45.         {  
  46.             nex[i]=0x3f3f3f3f;  
  47.             if(mp[a[i]].size()>num[i]+k-1)  
  48.             {  
  49.                 nex[i]=mp[a[i]][num[i]+k-1];  
  50.             }  
  51.         }  
  52.         for(int i=1;i<=n;i++)minn[i][0]=nex[i];  
  53.         ST();  
  54.         long long int output=0;  
  55.         for(int i=1;i<=n;i++)  
  56.         {  
  57.             int pos=-1;  
  58.             int l=i;  
  59.             int r=n;  
  60.             while(r-l>=0)  
  61.             {  
  62.                 int mid=(l+r)/2;  
  63.                 if(getminn(i,mid)<=mid)  
  64.                 {  
  65.                     pos=mid;  
  66.                     r=mid-1;  
  67.                 }  
  68.                 else l=mid+1;  
  69.             }  
  70.             if(pos==-1)continue;  
  71.             output+=(n-pos+1);  
  72.         }  
  73.         printf("%lld\n",output);  
  74.     }  


你可能感兴趣的:(Codeforces 190D Non-Secret Cypher)