F. Consecutive Subsequence
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
input

Copy
7
3 3 4 7 5 6 8
output

Copy
4
2 3 5 6
input

Copy
6
1 3 5 2 4 6
output

Copy
2
1 4
input

Copy
4
10 9 8 7
output

Copy
1
1
input

Copy
9
6 7 8 3 4 5 9 10 11
output

Copy
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]

题意:给你一个数组找出最长的递增子序列的长度以及下标位置。

例如第一组样例:7

3 3 4 7 5 6 8

最长的子序列为3 4 5 6,长度为4。

下标为1 3 5 6或2 3 5 6

题解:用map进行动态规划,mp[i]表示以i数字开头的最长的递增子序列的长度,即有转移方程mp[i]=max(mp[i],mp[i-1]+1)

最后找出map里面子序列长度最长的数字i,倒着输出就行了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf=0x3f3f3f3f; map<int ,int >dp;
int t[];
stack<int>s;
int main()
{
int n;
scanf("%d",&n);
int ans=-inf;
int maxx;
for(int i=;i<=n;i++)
{
scanf("%d",&t[i]);
dp[t[i]]=dp[t[i]-]+;
if(dp[t[i]]>ans)
{
ans=dp[t[i]];
maxx=t[i];
}
}
printf("%d\n",ans);
for(int i=n;i>=;i--)
{
if(t[i]==maxx)
{
s.push(i);
maxx--;
}
}
for(int i=;i<ans;i++)
{
if(i!=)printf(" ");
printf("%d",s.top());
s.pop();
}
printf("\n");
}

codeforce 977 F. Consecutive Subsequence的更多相关文章

  1. CF 977 F. Consecutive Subsequence

    题意: 第一场div3, 求的是一个序列中最长连续(a,a+1,a+2...)子序列. 分析: 设一个DP[i] 表示 序列以i结尾的最长长度, 一开始都设为0. 那么如果这个数是a, 他的最长长度就 ...

  2. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)

    题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...

  3. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (DP)

    题意:给你一个长度为\(n\)的序列,求一个最长的\({x,x+1,x+2,.....,x+k-1}\)的序列,输出它的长度以及每个数在原序列的位置. 题解:因为这题有个限定条件,最长序列是公差为\( ...

  4. Codeforces 977F - Consecutive Subsequence - [map优化DP]

    题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...

  5. Consecutive Subsequence CodeForces - 977F(dp)

    Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...

  6. [CF977F]Consecutive Subsequence

    题目描述 You are given an integer array of length n. You have to choose some subsequence of this array o ...

  7. Consecutive Subsequence CodeForces - 977F (map优化DP)·

    You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...

  8. Consecutive Subsequence (DP+map)

    You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...

  9. 【Codeforces 977F】Consecutive Subsequence

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 设f[i]表示i作为序列的最后一个数字,最长的连续序列的长度. 用f[i]和f[i-1]+1来转移即可 [代码] import java.io ...

随机推荐

  1. 子tab里面新增tab(top.jQuery)

    top.jQuery应用. var jq = top.jQuery; if (jq("#centre").tabs("exists", "预览页面&q ...

  2. Scala学习手记1 - 快速体验

    又重新开始了scala的学习,因为中断了太长时间,所以这次还得从零开始.学习的过程就记录在这个博客上了. 这次学习的教程是<scala程序设计 java虚拟机多核编程实战>,我在多看上买了 ...

  3. Java socket - 使用代理服务器

    为什么使用代理服务器不需要多说了. 使用Proxy Java提供了Proxy类实现使用代理进行通信. Proxy类的构造器Proxy(Proxy.Type type, SocketAddress sa ...

  4. 用WebClient在异步下载或上传时每次只进行一个任务 C#

    当在每次上传或者下载的时候,我只想进行一个任务的,我用的是WebClient类,但是我又不想用同步的方法UploadFile.DownloadFile,因为WebClient这个类的同步方法没有Upl ...

  5. 八 web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用

    使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener()初始化IPinstall_opener()将代理IP设置成全 ...

  6. cassandra框架模型之一——Colum排序,分区策略 Token,Partitioner bloom-filter,HASH

    转自:http://asyty.iteye.com/blog/1202072 一.Cassandra框架二.Cassandra数据模型 Colum / Colum Family, SuperColum ...

  7. Sturts2 三种开发模式 (转)

    1.实现与 Servlet API的交互struts2中提供了Map类型的request.session与application,可以从ActionContext对象中获得.ActionContext ...

  8. zoj 3212 K-Nice(构造)

    K-Nice Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge This is a super simple pr ...

  9. 如何在100万文字的文章中 200ms内 快速提取 替换 上万个关键字

    关键点: 关键字 组合 成一棵 hash 树  ( 有属性就直接移动指针到下一个字对象, 没有属性就创建子对象, 再移动指针;  第二次循环在子对象看有没这个属性 ) 探测针       标记结束   ...

  10. Flask--信号 blinker

    Flask--信号 blinker Flask框架中的信号基于blinker,可以让开发者在flask请求过程中 定制一些用户行为执行. 在请求前后,模板渲染前后,上下文前后,异常 的时候 安装: p ...