Codeforces 190D Non-Secret Cypher(尺取)

超、凢脫俗 2022-06-03 10:57 191阅读 0赞

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 k equal 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[ij] (1 ≤ i ≤ j ≤ n) of array a = (a1, a2, …, a**n) is an array, made from its consecutive elements, starting from the i-th one and ending with the j-th one: a[ij] = (a**i, a**i + 1, …, a**j).

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 integers a**i (1 ≤ a**i ≤ 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 cin, cout streams or the %I64d specifier.

Example

Input

  1. 4 2
  2. 1 2 1 2

Output

  1. 3

Input

  1. 5 3
  2. 1 2 1 1 3

Output

  1. 2

Input

  1. 3 1
  2. 1 1 1

Output

  1. 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).

题解:

之前做过这一类型的题,这次训练一下就a了,就是先找到一个符合条件的区间[l,r]记为尺子,然后用哈希记录下该区间各个值的情况,然后将尺子右移,对尺子两端的数据进行增减,判断是否满足k,不满足尺子右端伸长到满足为止,对于每一次符合条件的情况,答案ans应该加上n-r,累加下来就是答案

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<queue>
  5. #include<stack>
  6. #include<math.h>
  7. #include<vector>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<cmath>
  12. #include<string>
  13. #include<algorithm>
  14. #include<iostream>
  15. #include<stdio.h>
  16. using namespace std;
  17. #define ll long long
  18. map<int,int>M;
  19. int a[400005];
  20. int main()
  21. {
  22. int i,j,n,k,l,r,tag=0;
  23. ll ans;
  24. scanf("%d%d",&n,&k);
  25. for(i=0;i<n;i++)
  26. scanf("%d",&a[i]);
  27. l=0,r=0;
  28. while(r<n)
  29. {
  30. M[a[r]]++;
  31. if(M[a[r]]==k)
  32. {
  33. tag=1;
  34. break;
  35. }
  36. r++;
  37. }
  38. if(!tag)
  39. {
  40. printf("0\n");
  41. return 0;
  42. }
  43. tag=1;
  44. ans=n-r;
  45. while(r<n)
  46. {
  47. if(M[a[l]]==k)
  48. {
  49. tag=0;
  50. r++;
  51. }
  52. M[a[l]]--;
  53. l++;
  54. while(!tag&&r<n)
  55. {
  56. M[a[r]]++;
  57. if(M[a[r]]==k)
  58. {
  59. tag=1;
  60. break;
  61. }
  62. else
  63. r++;
  64. }
  65. if(!tag)
  66. break;
  67. ans+=(ll)(n-r);
  68. }
  69. printf("%I64d\n",ans);
  70. return 0;
  71. }

发表评论

表情:
评论列表 (有 0 条评论,191人围观)

还没有评论,来说两句吧...

相关阅读

    相关 法总结

    一般地,当我们要去枚举满足条件的区间权值的区间,可以用遍历双指针 l 和 r 的做法去枚举第一个满足条件的区间,然后去维护其他数据,这样的做法就是尺取法,它的复杂度为O(n)

    相关 法 模板

      题目描述 博览馆正在展出由世上最佳的 M 位画家所画的图画。 wangjy想到博览馆去看这几位大师的作品。 可是,那里的博览馆有一个很奇怪的规定,就是在购买门票

    相关 算法--

    尺取法 尺取法通常是指对数组保存一对下标(起点,终点),然后根据实际情况交替推进两个端点直到得出答案的方法,这种操作很像是尺取虫爬行的方式故得名。 我们先来看看POJ的

    相关 Pie————二分+练习

    给你n个蛋糕的半径,你有m个朋友,所以总共有m+1个人,现在要分蛋糕,要求每个人分到的大小都是一样的,且每个人的蛋糕都是从一块上切割下来的(不能是2个不同的蛋糕拼起来的),现在