codeforce 977 F. Consecutive Subsequence
2 seconds
256 megabytes
standard input
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.
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.
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.
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
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的更多相关文章
- CF 977 F. Consecutive Subsequence
题意: 第一场div3, 求的是一个序列中最长连续(a,a+1,a+2...)子序列. 分析: 设一个DP[i] 表示 序列以i结尾的最长长度, 一开始都设为0. 那么如果这个数是a, 他的最长长度就 ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (DP)
题意:给你一个长度为\(n\)的序列,求一个最长的\({x,x+1,x+2,.....,x+k-1}\)的序列,输出它的长度以及每个数在原序列的位置. 题解:因为这题有个限定条件,最长序列是公差为\( ...
- Codeforces 977F - Consecutive Subsequence - [map优化DP]
题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...
- Consecutive Subsequence CodeForces - 977F(dp)
Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...
- [CF977F]Consecutive Subsequence
题目描述 You are given an integer array of length n. You have to choose some subsequence of this array o ...
- 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 ...
- Consecutive Subsequence (DP+map)
You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...
- 【Codeforces 977F】Consecutive Subsequence
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设f[i]表示i作为序列的最后一个数字,最长的连续序列的长度. 用f[i]和f[i-1]+1来转移即可 [代码] import java.io ...
随机推荐
- Treflection02_getMethods()_getMethod()
1. package reflectionZ; import java.lang.reflect.Constructor; import java.lang.reflect.Method; impor ...
- C++ 函数后面的const
一个函数 AcGePoint3dstartPoint() const; const放在后面跟前面有区别么 ==> 准确的说const是修饰this指向的对象的 譬如,我们定义了 classA{ ...
- yii2:如果获取config/web.php配置的值?
return [ 'version' => '1.0.1', 'category-map' => [ 1 => '样式1', 2 => '样式2', 3 => '样式3' ...
- sublime 常见问题
问题一: There Are No Packages Available For Installation 原因:官方提供的Package Control不能用.将官方的那个Package Contr ...
- Ajax基础(五)--封装库
jQuery ajax请求的基本语法: 一.封装为对象: ajax.txt代码: {"id":"102","username":" ...
- 【Python】UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3
问题如下: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3 解决方法: 程序开始加上下面两句 sys ...
- 修改element内部样式的两种方式
第一种: 全局样式修改: 但这种方式有点不好的地方,这样会影响别的组件,比如修改elementUI的树结构的样式,这样改的话会影响到别的树组件: 第二种方式: 在要修改的组件内修改树结构样式 比如改这 ...
- EL表达式 分割字符串 ,forEach定次循环
后台取出来的是字符串 以 a,b,c, 的形式 前台要将字符串中的“,”去掉 ,并forEach重新拼接 list.labelsName不用加${} <c:set value=" ...
- Chrome 开发者控制台使用技巧
Chrome 有内置的开发者工具.它拥有丰富的特性,比如元素(Elements).网络(Network)和安全(Security).今天,我们主要关注一下 JavaScript 控制台. 当我最初写代 ...
- keras&tensorflow+分布式训练︱实现简易视频内容问答框架
内容来源:Keras 之父讲解 Keras:几行代码就能在分布式环境训练模型 把 Keras API 直接整合入 TensorFlow 项目中,这样能与你的已有工作流无缝结合.至此,Keras 成为了 ...