codeforces 190D Non-Secret Cypher(two pointers)
来源:互联网 发布:和我信造粉软件 编辑:程序博客网 时间:2024/05/29 10:58
题目链接
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).
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 ai (1 ≤ ai ≤ 109) — elements of the array.
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 %I64dspecifier.
4 21 2 1 2
3
5 31 2 1 1 3
2
3 11 1 1
6
子数列为数列a中下标从L到R连续的一段数字,其中1<=L<=R<=n。
若一个子数列中相同的数字个数大于等于K个,则满足条件。
题解:我们可以用two pointer的方法。维护两个指针,左指针L,右指针R。表示当前的区间为[L,R]。如果当前区间满足条件,则L后移一位,否则R后移一位。我们可以用map或者hash统计区间中每个数字的个数。初始的时候L=R=1。在维护的过程中我们可以O(1)判断出一个区间是否满足条件。
如何统计答案的个数?
当我们求得一个满足条件的区间[L,R]以后,所有包含这个区间的区间都满足条件。但是对于每个区间答案直接加上L*(n-R+1)是不对的,为了避免算重的情况。我们在two pointer的时候记录当前区间的前一个满足条件的区间 [preL,preR]。答案加上(L-preL)*(n-R+1)就对了。
代码如下:
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<string>#include<queue>#include<stack>#include<map>#include<set>#include<stdlib.h>#include<vector>#define inff 0x3fffffff#define nn 410000#define mod 1000000007typedef long long LL;const LL inf64=inff*(LL)inff;using namespace std;int n,k;int a[nn];map<int,int>ma;int main(){ int i; while(scanf("%d%d",&n,&k)!=EOF) { for(i=1;i<=n;i++) { scanf("%d",&a[i]); } ma.clear(); int pl; pl=0; int l,r; l=1,r=0; bool ok=false; LL ans=0; while(r<=n) { if(ok) { ans+=LL(n-r+1)*(LL)(l-pl); pl=l; if(ma[a[l]]==k) ok=false; ma[a[l]]--; l++; } else { r++; if(r<=n) { if(ma.count(a[r])==0) ma[a[r]]=1; else ma[a[r]]++; if(ma[a[r]]==k) { ok=true; } else ok=false; } } } printf("%I64d\n",ans); } return 0;}
- codeforces 190D Non-Secret Cypher(two pointers)
- Codeforces Round #120 (Div. 2) / 190D Non-Secret Cypher (计数&two pointers)
- Codeforces 190D Non-Secret Cypher
- CodeForces Round #120 (190D) - Non-Secret Cypher
- codeforces 190D Non-Secret Cypher 双指针
- Codeforces 190D Non-Secret Cypher【思维+RMQ+二分】
- D. Non-Secret Cypher
- Codeforces Round #120(190D) Non-Secret Cypher(尺取法)
- CF 190D Non-Secret Cypher
- CF 190D. Non-Secret Cypher
- cf 120 c,d,e STL Non-Secret Cypher Counter Attack
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays(二分 | two pointers)
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays(二分 | two pointers)
- Codeforces Round #345 (Div. 2) D. Image Preview __ two pointers and pretreat
- Sum( two pointers )
- D. R2D2 and Droid Army(two pointers)
- Educational Codeforces Round 11 C (Two Pointers)
- codeforces 590D Top Secret Task(dp)
- JAVA设计模式(十)——享元模式
- Linux学习笔记一:Ubuntu 12.04中文输入法的安装
- 文件的读写和上锁
- ORACLE 11G ADG的STATSPACK性能报表配置方法
- IOS开发中用@interface声明局部变量ivar和用@property声明属性的区别
- codeforces 190D Non-Secret Cypher(two pointers)
- 进度条
- 汇编基础(1)
- POI 18
- android背景选择器selector用法
- 自适应的布局:使用输入法时底部Button被顶上来
- 整maven+ssm出现的错误 java.lang.UnsupportedClassVersionError: .... : Unsupported major.minor version 51.0
- 45个实用的JavaScript技巧、窍门和最佳实践
- 葡京娱乐场足球免费推荐交流群227231326