题目描述

You are given an integer array of length n.

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] for some value x and length k.

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] the following arrays are subsequences: [3], [5,3,1,2,4], [5,1,4], but the array [1,3] is not.

题目大意

给你n个数的序列,求出最长的连续上升子序列(每个元素之间只差\(1\)),并输出在原序列中的位置。

40分做法

非常容易想到的暴力,我们开\(200000\)个\(vector\),每次在已有的数组中找是否有一个数组的最后一个元素是当前数-1,如果有那么就插入到长度最长的一个,如果没有,那么就新开一个数组来存储新的数列。

40分代码

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define pb push_back
  4. using namespace std;
  5. struct node{int x,p;};
  6. vector<node>ans[200005];//其实可以用vector套vector
  7. int cnt=0,n,len[200005],ret=0,res=0;
  8. LL a[200005];
  9. LL r(){LL x=0,w=0;char ch=0;while(!isdigit(ch))w|=ch=='-',ch=getchar();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return w?-x:x;}
  10. int main(){
  11. scanf("%d",&n);
  12. for(int i=1;i<=n;i++)a[i]=r();
  13. for(int i=1;i<=n;i++){
  14. int pos=-1;
  15. for(int j=1;j<=cnt;j++)if(ans[j][ans[j].size()-1].x==a[i]-1&&(pos==-1||ans[pos].size()<=ans[j].size()))pos=j;//找到已有的数组,在插入
  16. if(pos==-1)ans[++cnt].pb((node){a[i],i}); else ans[pos].pb((node){a[i],i});//如果没有合法的数组就新开一个。
  17. }
  18. for(int i=1;i<=cnt;i++)if(ret<ans[i].size())ret=ans[i].size(),res=i;
  19. printf("%d\n",ret); for(int i=0;i<ans[res].size();i++)printf("%d ",ans[res][i].p);
  20. return 0;
  21. }

100分做法

其实是非常简单的DP问题,我们用\(F[i]\)表示以\(i\)为结尾的最长的序列,那么转移方程就是:\(F[i]=F[i-1]+1\)。

但是这里的\(F\)数组的下标非常大,那么我们就用\(map\)来映射就可以了。

100分代码

  1. #include<bits/stdc++.h>
  2. #define inf 0x3f3f3f3f
  3. #define N 200005
  4. using namespace std;
  5. int a[N],n;
  6. map<int,int>f;
  7. int r(){int w=0,x=0;char ch=0;while(!isdigit(ch))w|=ch=='-',ch=getchar();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return w?-x:x;}
  8. int main(){
  9. int cas=r();n=r();
  10. int x=0,Max=0;
  11. for(int i=1;i<=n;i++){
  12. a[i]=r();f[a[i]]=f[a[i]-1]+1;
  13. if(f[a[i]]>Max)Max=f[a[i]],x=a[i];
  14. }
  15. int t=x-Max+1;printf("%d\n",Max);
  16. for(int i=1;i<=n;i++)if(a[i]==t){printf("%d ",i);t++;}
  17. return 0;
  18. }

[CF977F]Consecutive Subsequence的更多相关文章

  1. Consecutive Subsequence CodeForces - 977F(dp)

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

  2. codeforce 977 F. Consecutive Subsequence

    F. Consecutive Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  3. 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 ...

  4. Consecutive Subsequence (DP+map)

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

  5. 最大连续子序列和问题(Maximum Consecutive Subsequence Sum)

    该算法的定义是:给出一个int序列,元素有正有负,找出其中的最大连续子序列的和. 例如:-2,11,-4,13,-5-2,:最大和为20(11,-4, 13). 怎么考虑这个问题呢? 要充分利用,连续 ...

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

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

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

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

  8. CF 977 F. Consecutive Subsequence

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

  9. 【Codeforces 977F】Consecutive Subsequence

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

随机推荐

  1. Oracle Profile 配置文件

    Profile是用户的配置文件,它是密码限制,资源限制的命名集合.利用profile 可以对数据库用户进行基本的资源管理,密码管理. 1 创建profile 的语法 create profile pr ...

  2. day 7-19 Mysql索引原理与查询优化

    一,介绍 1.什么是索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语 ...

  3. day 7-18 mysql case when语句

    概述: sql语句中的case语句与高级语言中的switch语句,是标准sql的语法,适用于一个条件判断有多种值的情况下分别执行不同的操作. 首先,让我们看一下CASE的语法.在一般的SELECT中, ...

  4. Git发生SSL certificate problem: certificate ha错误的解决方法

    这两天,不知道为什么,用Git提交代码到服务器时,总出现SSL certificate problem: unable to get local issuer certificate while ac ...

  5. 多IP地址--笔记

    多IP 地址特性使虚拟用户可以在一个load generator上运行且被识别为多个IP地址 1 虚拟IP是同一个generator上的多个IP,这种分配过程由controller自动来进行 2 对于 ...

  6. dreamweavercs 和dreamweaver cc的區別

    https://zhidao.baidu.com/question/1541178469432885667.html

  7. jvm相关参数

    官方地址:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html#BEHHGFAE 一.查看jvm运行参数 1.查看 ...

  8. python之类和__init__

    构建一个商品类,__init__函数类似于构造方法,self类似于this import random class Goods: def __init__(self, name, price): se ...

  9. JS--操作DOM树

    <ul id="ul1"> <li id="li1">111</li> <li id="li2"& ...

  10. Lodop打印较大的超出纸张的图片

    ADD_PRINT_IMAGE打印图片时,如果一个图片过大,超出纸张,默认超出部分是不显示的,也不会分页.最近遇到有人利用ADD_PRINT_URL打印图片,说图片自动分了多页,因为这个方法一般是用来 ...