XidianOJ 1175: count
题目描述
There is a sequence with n integers, your task is finding there are how many consecutive subsequences have two characters:
1.The length of the subsequence is k.
2.Every number in this subsequence is different.
输入
The input contains multiple test cases.
The first line of each case contains two integers n and k (1≤k≤n≤200000). Then second line contains n positive integers a[i](1≤a[i]≤1000000000).
输出
For each case ,print the number of consecutive subsequences in the only line.
样例输入
10 3 1 2 3 4 3 2 1 1 2 2
样例输出
4
动态规划
设dp[i]是以第i个数字结尾的满足不重复数字的最长长度,则dp[i] = dp[i-1] + 1(a[i]未出现过),dp[i] = i - m[a[i]] (a[i]出现过,使用map容器储存上个a[i]的下标)
#include <cstdio> #include <cctype> #include <iostream> #include <cstring> #include <algorithm> #include <string> #include <vector> #include <map> #include <set> #include <cmath> using namespace std; typedef long long LL;LL n,k; LL a[200005]; LL dp[200005] = {0}; map<LL,LL> m;int main(){//freopen("test.in","r",stdin);while (scanf("%lld %lld",&n,&k) != EOF){m.clear();memset(dp,0,sizeof(dp));LL i,total = 0;for (i=1;i<=n;i++){scanf("%lld",&a[i]);}dp[1] = 1; m[a[1]] = 1;for (i=2;i<=n;i++){if (m[a[i]] == 0){dp[i] = dp[i-1]+1; m[a[i]] = i;}else {dp[i] = i - m[a[i]];// printf("%lld %lld\n",i,dp[i]); m.clear();for (int j=i,k=0;k<dp[i];k++){m[a[j-k]] = j-k;}if (i == 5){//printf("%lld\n",m[a[2]]); }}}//printf("%lld = k\n",k);for (i=k;i<=n;i++){if (dp[i] >= k) {// printf("%lld\n",i);total ++;}}printf("%lld\n",total);}return 0; }
View Code
转载于:https://www.cnblogs.com/ToTOrz/p/6735997.html
XidianOJ 1175: count相关推荐
- 【bzoj 1833】【codevs 1359】 [ZJOI2010]count 数字计数(数位dp)
1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 2774 Solved: 1230 [Submit ...
- LeetCode刷题记录6——696. Count Binary Substrings(easy)
LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...
- c++关于map的find和count的使用
使用count,返回的是被查找元素的个数.如果有,返回1:否则,返回0.注意,map中不存在相同元素,所以返回值只能是1或0. 使用find,返回的是被查找元素的位置,没有则返回map.end(). ...
- 递归/归并:count of smaller numbers求逆序数
已知数组nums,求新数组count,count[i]代表了在nums[i]右侧且比 nums[i]小的元素个数. 例如: nums = [5, 2, 6, 1], count = [2, 1, 1, ...
- bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Submit: 7669 Solved: 1894 [Sub ...
- windows7下解决caffe check failed registry.count(type) == 1(0 vs. 1) unknown layer type问题
在Windows7下调用vs2013生成的Caffe静态库时经常会提示Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer t ...
- 1093 Count PAT‘s
这题出现在"活用递推"专题下面,所谓递推就是这一步的结果和上一步的结果有直接联系.对于本题来说,从左到右,记到当前位置,一共出现的P的个数,如果当前位置是P,则个数就是上一位的加1 ...
- Mybatis中Oracle和Mysql的Count字段问题
Mybatis中Oracle和Mysql的Count字段问题 我们在进行项目开发时经常会碰到查询总数的问题,所以我们直接是用select count(1) from table来进行查询.那么在Myb ...
- LINQ to SQL语句之 Count/Sum/Min/Max/Avg
Count/Sum/Min/Max/Avg操作符 适用场景:统计数据吧,比如统计一些数据的个数,求和,最小值,最大值,平均数. Count 说明:返回集合中的元素个数,返回INT类型:不延迟.生成SQ ...
最新文章
- poj 1845 Sumdiv (算数基本定理+逆元)
- What to call your Academic Event
- Base64编码解码原理
- C语言 system
- 基于嵌入式系统的gnash最小库依赖关系
- Win7 开启远程桌面
- word鼠标右下角有一个小方块_word
- 菠萝粉的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
- C语言错误类型中英文对照表
- RocketMq配置rocketmq-console控制台管理账号密码
- 毕业寄语 | 关于毕业季的温柔文案
- 厦大教授计算机专业,厦门大学计算机系李翠华教授的荣休座谈会
- Unity Shader LOD详解
- 如何在opensolaris2008.05清除root密码
- HDU1799-组合公式变形
- C语言初步-顺序结构-已知三边求三角形面积-海伦公式
- Ubuntu 18.04 vscode安装网易云音乐插件
- C语言输出教学日历表
- 第16章 第一个信徒
- fork后父子进程共享资源
热门文章
- 域服务器内置用户组说明
- 使用 ODP.NET 访问 Oracle(.net如何访问Oracle)详解
- 粒子效果的开始和结束颜色属性
- 海量数据处理简要说明(一)
- python socket 实现的简单http服务器
- 一步一步手绘Spring DI运行时序图(Spring 自动装配之依赖注入)
- 蓝桥杯 ALGO-77 算法训练 斜率计算
- 【iOS开发】理解 IBOutlet 和 IBAction
- 1038. 统计同成绩学生(20)-PAT乙级真题
- android51版本小游戏,世界游戏大全51游戏下载-世界游戏大全51预约 安卓版v1.0.0-PC6手游网...