D. Non-Secret Cypher

原创 2013年12月01日 21:33:01
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 leastk 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 kequal 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.

Sample test(s)
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).

思路: two pointer, 两个指针指向头,然后一个往右走,直到找到符合条件的序列,然后左指针后移,直到不符合条件在进行后移

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <map>
using namespace std;

long long n, k, num[555555];
long long l = 0, r = 0;
long long ans = 0;
map<int, int> save;

int main() {
    scanf("%lld%lld", &n, &k);
    for (int i = 0; i < n; i ++)
	cin >> num[i];
    save[num[0]] ++;
    if (k == 1) {
	cout << n * (n + 1) / 2 << endl;
	return 0;
    }
    while (r != n - 1) {
	r ++;
	save[num[r]] ++;
	if (save[num[r]] >= k) {
	    while (save[num[r]] >= k) {
		save[num[l]] --;
		l ++;
		ans += n - r;
	    }
	}
    }
    cout << ans << endl;
    return 0;
}


相关文章推荐

CF 190D Non-Secret Cypher

题目链接:http://codeforces.com/problemset/problem/190/D 题目大意:求一个数列中有多少子数列其中某个数出现k次以上. 题目思路:枚举每个数,求...
  • gotoac
  • gotoac
  • 2012年05月21日 16:08
  • 576

CodeForces Round #120 (190D) - Non-Secret Cypher

这场比赛前四题还是很好做的~~水完这四道看standings..发现比赛时出4道就能进前25了...囧....      本题我用了一个类似单调队列的东西...      将数列中每个点的位置和a...
  • kk303
  • kk303
  • 2012年05月20日 19:47
  • 818

Codeforces #590D: Top Secret Task 题解

这题显然的是dp 设计状态dp[u][i][j]为在前i个数中进行操作,swap次数不超过j次,前i个数的和最小是多少 对于第u个元素: 如果不将它放进前i个:dp[u][i][j]=dp[u-1][...

Codeforces 590D Top Secret Task

CF 590D题目大意:给出一个长度为N的序列,同时你有S次机会交换任意两个相邻位置的数,希望前K个数的和最小。错误思路。。 很容易想到状态为:f[i][j]:f \left[i\right]\lef...

【codeforces 757D】Felicity's Big Secret Revealed

【题目链接】:http://codeforces.com/problemset/problem/757/D【题意】 给你一个01串; 让你分割这个01串; 要求2切..n+1切; 对于每一...

Box2D: 错误variable length array of non-POD element type 'b2Vec2'

Variable length of non-POD element type 'b2Vec2'!!怎么会报出这个错误呢? 真有点儿搞不懂了,刚进入ios开发的时候用的是一个朋友的mac笔记本 ...
  • yuanya
  • yuanya
  • 2014年08月18日 15:37
  • 993

Unity3D: Non-rectangular GUI buttons – Part 1

Unity3D: Non-rectangular GUI buttons – Part 1 Posted by DimasTheDriver | Filed under Featured, ...

cypher-refcard-3.3.pdf

  • 2017年10月31日 07:12
  • 1.97MB
  • 下载

cypher-refcard-3.1

  • 2017年07月21日 14:24
  • 274KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:D. Non-Secret Cypher
举报原因:
原因补充:

(最多只允许输入30个字)