[CF977F]Consecutive Subsequence

题目描述
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分代码
#include<bits/stdc++.h>
#define LL long long
#define pb push_back
using namespace std;
struct node{int x,p;};
vector<node>ans[200005];//其实可以用vector套vector
int cnt=0,n,len[200005],ret=0,res=0;
LL a[200005];
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;}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)a[i]=r();
for(int i=1;i<=n;i++){
int pos=-1;
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;//找到已有的数组,在插入
if(pos==-1)ans[++cnt].pb((node){a[i],i}); else ans[pos].pb((node){a[i],i});//如果没有合法的数组就新开一个。
}
for(int i=1;i<=cnt;i++)if(ret<ans[i].size())ret=ans[i].size(),res=i;
printf("%d\n",ret); for(int i=0;i<ans[res].size();i++)printf("%d ",ans[res][i].p);
return 0;
}
100分做法
其实是非常简单的DP问题,我们用\(F[i]\)表示以\(i\)为结尾的最长的序列,那么转移方程就是:\(F[i]=F[i-1]+1\)。
但是这里的\(F\)数组的下标非常大,那么我们就用\(map\)来映射就可以了。
100分代码
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define N 200005
using namespace std;
int a[N],n;
map<int,int>f;
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;}
int main(){
int cas=r();n=r();
int x=0,Max=0;
for(int i=1;i<=n;i++){
a[i]=r();f[a[i]]=f[a[i]-1]+1;
if(f[a[i]]>Max)Max=f[a[i]],x=a[i];
}
int t=x-Max+1;printf("%d\n",Max);
for(int i=1;i<=n;i++)if(a[i]==t){printf("%d ",i);t++;}
return 0;
}
[CF977F]Consecutive Subsequence的更多相关文章
- Consecutive Subsequence CodeForces - 977F(dp)
Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...
- codeforce 977 F. Consecutive Subsequence
F. Consecutive Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 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 ...
- 最大连续子序列和问题(Maximum Consecutive Subsequence Sum)
该算法的定义是:给出一个int序列,元素有正有负,找出其中的最大连续子序列的和. 例如:-2,11,-4,13,-5-2,:最大和为20(11,-4, 13). 怎么考虑这个问题呢? 要充分利用,连续 ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
- Codeforces 977F - Consecutive Subsequence - [map优化DP]
题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...
- CF 977 F. Consecutive Subsequence
题意: 第一场div3, 求的是一个序列中最长连续(a,a+1,a+2...)子序列. 分析: 设一个DP[i] 表示 序列以i结尾的最长长度, 一开始都设为0. 那么如果这个数是a, 他的最长长度就 ...
- 【Codeforces 977F】Consecutive Subsequence
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设f[i]表示i作为序列的最后一个数字,最长的连续序列的长度. 用f[i]和f[i-1]+1来转移即可 [代码] import java.io ...
随机推荐
- 【学习总结】Git学习-参考廖雪峰老师教程五-远程仓库
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- select2 简单解析
<select name="supplierId" class="customsBrokerSel select2 absOpacity select2-hidde ...
- 转《service worker在移动端H5项目的应用》
1. PWA和Service Worker的关系 PWA (Progressive Web Apps) 不是一项技术,也不是一个框架,我们可以把她理解为一种模式,一种通过应用一些技术将 Web App ...
- python爬虫之多线程、多进程、GIL锁
背景: 我们知道多线程要比多进程效率更高,因为线程存在于进程之内,打开一个进程的话,首先需要开辟内存空间,占用内存空间比线程大.这样想也不怪,比如一个进程用10MB,开10个进程就得100MB的内存空 ...
- k8s HPA自动收缩
HPA自动收缩 autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量 #创建一个replicaset测试 [r ...
- mysql数据库的备份和还原的总结
mysql数据库的备份和还原的总结 (来自一运维同事的总结) 1. 备份方式: 热备:数据库在线进行备份,不影响读和写的在线备份方式! 温备:数据库在线进行备份,对表备份时先锁定写操作,仅可以执行读操 ...
- nginx 卸载后重新安装/etc/nginx配置文件没了,cannot open /etc/nginx/nginx.conf (No such file or directory)
sudo apt-get --purge remove nginx-common sudo apt-get --purge remove nginx* sudo apt-get autoremove ...
- 关于 flask 实现数据库迁移以后 如何根据创建的模型类添加新的表?
在此之前 我们先说一下常规的flask运用第三方扩展来实现数据库的迁移的三个步骤以及每步的目的. 数据库的迁移的三个步骤:(cd 到run.py所在路径) python run.py db init ...
- Lodop生成文档式模版
Lodop模版有两种方法,一种是传统的JS语句,可以用JS方法里的eval来执行,一种是文档式模版,是特殊格式的base64码,此篇博文介绍文档式模版的生成方法.两种模版都可以存入一下地方进行调用,比 ...
- nginx rewrite重写
通过官方文档可以看到,rewrite的作用上下文是 server location,可以写在 server里面 亦或location里面; 命令: if (条件) {} 条件判断 set #设置 ...