Day8 - B - Non-Secret Cypher CodeForces - 190D
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[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 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.
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.
Examples
- 4 2
1 2 1 2
- 3
- 5 3
1 2 1 1 3
- 2
- 3 1
1 1 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题相似,一个指针每次递增一,另一个指针随条件改变不止一,一般是维护第一个指针的极值,然后计算数量
- const int maxm = 4e5+;
- int buf[maxm], n, k;
- map<int, int> vis;
- int main() {
- ios::sync_with_stdio(false), cin.tie();
- cin >> n >> k;
- for(int i = ; i < n; ++i)
- cin >> buf[i];
- LL ans = ;
- int l = , r = ;
- vis[buf[]]++;
- while(l + k - < n) {
- while(r < n) {
- if(vis[buf[r]] >= k) break;
- r++;
- vis[buf[r]]++;
- }
- if(r == n) break;
- ans += (LL)(n - r);
- vis[buf[l]]--;
- l++;
- }
- cout << ans << "\n";
- return ;
- }
Day8 - B - Non-Secret Cypher CodeForces - 190D的更多相关文章
- CodeForces 190D Non-Secret Cypher
双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- Day8 - A - Points on Line CodeForces - 251A
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. N ...
- [Codeforces] #603 (Div. 2) A-E题解
[Codeforces]1263A Sweet Problem [Codeforces]1263B PIN Code [Codeforces]1263C Everyone is a Winner! [ ...
- CodeForces 490C Hacking Cypher
Hacking Cypher Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- CodeForces 496B Secret Combination
Secret Combination Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- Codeforces Round #279 (Div. 2) C. Hacking Cypher 前缀+后缀
C. Hacking Cypher time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)D. Felicity's Big Secret Revealed
题目连接:http://codeforces.com/contest/757/problem/D D. Felicity's Big Secret Revealed time limit per te ...
- Codeforces Round #279 (Div. 2) C. Hacking Cypher (大数取余)
题目链接 C. Hacking Cyphertime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...
- 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination
题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...
随机推荐
- Python 中的类与对象 初认识
一:类的声明 1类的关键字: 从第一天第一个项目起我们就接触过关键字,比如False True is not None return for while elif else import等等,这是语言 ...
- selenium webdriver 实现百度贴吧自动签到
public static void main(String[] args) { //TestUtils.killProcess("javaw.exe"); TestUtils.k ...
- SpringMVC笔记三
课程安排: 第一天:springmvc的基础知识 什么是springmvc? springmvc框架原理(掌握) 前端控制器.处理器映射器.处理器适配器.视图解析器 springmvc入门程序 目的: ...
- Java基础知识笔记第三章:运算符表达式语句
算术运算符与表达式 操作符 描述 例子 + 加法 - 相加运算符两侧的值 A + B 等于 30 - 减法 - 左操作数减去右操作数 A – B 等于 -10 * 乘法 - 相乘操作符两侧的值 A * ...
- Java基础知识笔记第二章:基本数据类型与数组
标识符和关键字 标识符: 1:字母,数字,下划线,美元符号 2.不能以数字开头 3.标识符不能是:true false null(尽管true false null不是java的关键字 ...
- web-pc项目中index页面分析
先上HTML代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...
- 【PAT甲级】1006 Sign In and Sign Out (25 分)
题意: 给出学生人数M,输入M组学生ID,到机房的时间,离开机房的时间.输出最早到机房的学生的ID,空格,最后离开机房的学生的ID.(M大小未给出,就用了1e5) AAAAAccepted code: ...
- 61二叉搜索树的第k个结点
题目描述 给定一颗二叉搜索树,请找出其中的第k大的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. 思路:二叉搜索树的中序遍历是递增的序列,使用 ...
- Linux进程管理(一)
目录 Linux进程管理(一) 参考 pstree命令 pidof命令 pmap命令 pwdx命令 ps命令 nice调优 发送信号 Linux进程管理(一)
- Java 在Excel单元格中应用一种/多种字体样式
在Excel表格中,设置单元格字体样式时,可以对单元格内的所有字符应用同一样式,即获取指定单元,应用样式即可:另外也可以对单元格内的不同字符内容应用不同字体样式,即获取单元格中的字符位置,应用样式:本 ...