Consecutive Subsequence (DP+map)
You are given an integer array of length nn.
You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to [x,x+1,…,x+k−1][x,x+1,…,x+k−1] for some value xx and length kk.
Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array [5,3,1,2,4][5,3,1,2,4] the following arrays are subsequences: [3][3], [5,3,1,2,4][5,3,1,2,4], [5,1,4][5,1,4], but the array [1,3][1,3] is not.
Input
The first line of the input containing integer number nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the array. The second line of the input containing nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the array itself.
Output
On the first line print kk — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.
On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.
Examples
- 7
3 3 4 7 5 6 8
- 4
2 3 5 6
- 6
1 3 5 2 4 6
- 2
1 4
- 4
10 9 8 7
- 1
1
- 9
6 7 8 3 4 5 9 10 11
- 6
1 2 3 7 8 9
Note
All valid answers for the first example (as sequences of indices):
- [1,3,5,6][1,3,5,6]
- [2,3,5,6][2,3,5,6]
All valid answers for the second example:
- [1,4][1,4]
- [2,5][2,5]
- [3,6][3,6]
All valid answers for the third example:
- [1][1]
- [2][2]
- [3][3]
- [4][4]
All valid answers for the fourth example:
- [1,2,3,7,8,9]
题目大意:
先输入n,然后输入n个数,然后找最长递增子串,输出各个元素的下标。
- #include <bits/stdc++.h>
- using namespace std;
- int a[];
- map <int,int> ma;
- int main()
- {
- int n;
- cin>>n;
- for(int i=,x;i<=n;i++)
- cin>>x,a[i]=x,ma[x]=ma[x-]+;
- int pos,maxx=;
- for(map <int,int>::iterator it=ma.begin();it!=ma.end();it++)
- {
- if(it->second>maxx)
- maxx=it->second,pos=it->first;
- }
- cout<<maxx<<'\n';
- for(int i=;i<=n;i++)
- if(a[i]==pos-maxx+)
- cout<<i<<' ',pos++;
- cout<<'\n';
- return ;
- }
Consecutive Subsequence (DP+map)的更多相关文章
- 【UVa】Palindromic Subsequence(dp+字典序)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=465&page=s ...
- CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数)
The sequence of integers a1,a2,…,aka1,a2,…,ak is called a good array if a1=k−1a1=k−1 and a1>0a1&g ...
- POJ 2533 Longest Ordered Subsequence(dp LIS)
Language: Default Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- HDU1423 Greatest Common Increasing Subsequence (DP优化)
LIS和LCS的结合. 容易写出方程,复杂度是nm2,但我们可以去掉一层没有必要的枚举,用一个变量val记录前一阶段的最优解,这样优化成nm. 1<=k<j,j增加1,k的上界也增加1,就 ...
- 【Unity Shader】六、使用法线贴图(Normal Map)的Shader
学习资料: http://www.sikiedu.com/course/37/task/456/show# http://www.sikiedu.com/course/37/task/458/show ...
- 取数字(dp优化)
取数字(dp优化) 给定n个整数\(a_i\),你需要从中选取若干个数,使得它们的和是m的倍数.问有多少种方案.有多个询问,每次询问一个的m对应的答案. \(1\le n\le 200000,1\le ...
- 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)
洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...
- [Codeforces722E] Research Rover (dp+组合数学)
[Codeforces722E] Research Rover (dp+组合数学) 题面 给出一个N*M的方格阵,从(1,1)出发,到(N,M)结束,从(x,y)只能走到(x+1,y)或(x,y+1) ...
- Mybatis(万能map)
mybatis(万能map) 我们使用对象作为参数有一个缺点: 我们要在mapper.xml文件和测试中要把所有的字段都写出来,那么,假如一个对象有100个字段,那我们要把这些字段都写出来吗? 所以这 ...
随机推荐
- 绘制复杂的原理图元件库用于cadence(二)
绘制Xilinx XC7K325TFFG900 kintex-7 FPGA元件 1.在官网搜索“pin out”往下拉一下就能看见 2.点击进入选择相应型号 3.打开之后是类似txt格式的FFG900 ...
- java设计模式--建造模式
建造模式 建造模式属于对象创建型模式,建造模式的目的为将复杂对象的构建过程与其部件实现方式分离,使得同样的构建过程可以有不同的表示,同时相同的构建过程也能够适用于不同的部件实现方式. 建造模式的适用性 ...
- kafka安装和使用
kafka安装和启动 kafka的背景知识已经讲了很多了,让我们现在开始实践吧,假设你现在没有Kafka和ZooKeeper环境. Step 1: 下载代码 下载0.10.0.0版本并且解压它. &g ...
- 解决部分浏览器不能显示itext生成的PDF文件文本域内容问题
利用Itext可以实现pdf的高效动态生成,但在实践过程中遇到了一个问题: 即itext利用map中的值设置到pdf模板上建立的文本域中时:能成功生成,但是在部分浏览器上(360,QQ,等浏览器)无法 ...
- siege4压测脚本示例
agent="Siege 1.0"rcconfig="/opt/siege4.0/etc/siegerc"concurrent=$1repet=$2url=&q ...
- [习题]输入自己的生日(年/月/日)#2 -- 日历(Calendar)控件的时光跳跃,一次跳回五年、十年前?--TodaysDate属性、VisibleDate属性
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/06/10/calendar_visibledate_birthday_dropdow ...
- Django 表增加外键
1.创建临时表,并把原表的数据复制到临时表 先根据python manage syl article查看创建临时表 CREATE TABLE `article_article_temp` ( `id` ...
- 使用JDK自带的工具jstack找出造成运行程序死锁的原因
Java多线程编程也是Java面试中经常考察的内容.刚接触Java多线程编程的朋友们,可能会不慎写出一些会导致死锁(deadlock)的应用出来.如何分析造成Java多线程的原因呢?很多时候我们在怀疑 ...
- MySQL报错竞技赛
以下报错,我几乎没出过几个. ERROR 2 系统找不到文件: mysql-5.6.1X默认的配置文件是在C:\Program Files\MySQL\MySQL Server 5.6\my-defa ...
- 如何诊断 11.2 集群节点驱逐问题 (文档 ID 1674872.1)
适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.1 到 11.2.0.2 [发行版 11.2]本文档所含信息适用于所有平台 用途 这篇文档提 ...