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代码:

  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <cstdio>  
  4. #include <cstring>  
  5. #include <map>  
  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<n;++i)  
  18.         scanf("%d",&a[i]);  
  19.     int s=0,e=-1;//s是头指针,e是尾指针  
  20.     bool flag=false;//标记区间此时是否满足条件  
  21.     while(e<n)  
  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<stdio.h>  
  2. #include<string.h>  
  3. #include<math.h>  
  4. #include<vector>  
  5. #include<map>  
  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<<j);i++)  
  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<<len)+1][len]);  
  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.     }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值