Consecutive Subsequence CodeForces - 977F(dp)
Consecutive Subsequence CodeForces - 977F
题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列)
解题思路:
状态:把dp[i]定义为数列末尾为 i 的最大连续数列的长度值
状态转移方程:dp[i] = dp[i-1] + 1
再由最大连续数列最后一个值从后往前倒推出前面的所有值,到不是连续时停止
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = + ;
int dp[N];
int a[N];
int res[N];
int main() {
int n;
scanf("%d", &n);
int e, v;
int ans = ;
for(int i = ; i < n; i++) {
scanf("%d", &v);
a[i] = v;
dp[v] = dp[v-] + ;
if(ans < dp[v]) {
ans = dp[v];
e = i;
}
}
printf("%d\n", ans);
int tot = ;
int flag = a[e];
for(int i = e; i >= ; i--) {
if(a[i] == flag) {
res[tot++] = i + ;
flag--;
}
if(tot == ans) break;
}
for(int i = tot - ; i > ; i--) {
printf("%d ", res[i]);
}
printf("%d\n", res[]);
return ;
}
Consecutive Subsequence CodeForces - 977F(dp)的更多相关文章
- 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 ...
- Codeforces 977F - Consecutive Subsequence - [map优化DP]
题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
- New Year and Old Subsequence CodeForces - 750E (dp矩阵优化)
大意: 给定字符串, 每次询问区间[l,r]有子序列2017, 无子序列2016所需要删除的最小字符数 转移用矩阵优化一下, 要注意$(\mathbb{Z},min,+)$的幺元主对角线全0, 其余全 ...
- D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)
D - Yet Another Problem On a Subsequence CodeForces - 1000D The sequence of integers a1,a2,-,aka1,a2 ...
- codeforce 977 F. Consecutive Subsequence
F. Consecutive Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (DP)
题意:给你一个长度为\(n\)的序列,求一个最长的\({x,x+1,x+2,.....,x+k-1}\)的序列,输出它的长度以及每个数在原序列的位置. 题解:因为这题有个限定条件,最长序列是公差为\( ...
- 【Codeforces 977F】Consecutive Subsequence
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设f[i]表示i作为序列的最后一个数字,最长的连续序列的长度. 用f[i]和f[i-1]+1来转移即可 [代码] import java.io ...
- Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)
New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...
随机推荐
- tomcat下的server.xml详解
说明:以下内容是自己看书时从书本上整理而来,在此记录一下,方便自己日后复习(自用),如果和网上帖子有相同,请联系本博主. server.xml文件描述了如果启动Tomcat server: <S ...
- jsp导入数据库数据写法(模板)
1.导入表格模板 <%@ page language="java" contentType="text/html; charset=utf-8" page ...
- Hibernate的查询功能
1.Query对象 1.使用Query对象,不需要写sql语句,但是写hql语句 (1)hql:hibernate query language,提供查询语言,这个hql语言和普通sql语句相似 (2 ...
- Matlab中的基本数据类型介绍
Matlab中支持的数据类型包括: 逻辑(logical)字符(char)数值(numeric)元胞数组(cell)结构体(structure)表格(table)函数句柄(function handl ...
- idea环境下建立maven工程并运行scala程序
idea中scala编程环境及建立maven工程 1.下载idea软件并破解:http://blog.csdn.net/nn_jbrs/article/details/70139178 2.安装sca ...
- ButterKnife RadioGroup选择事件
ButterKnife 的点击事件都很清晰,在使用RadioGroup控件时的方法: <!-- 定义一组单选框 --> <RadioGroup android:id="@+ ...
- Codeforces 958C3 - Encryption (hard)
C3 - Encryption (hard) 思路: 记sum[i]表示0 - i 的和对 p 取模的值. 1.如果k * p > n,那么与C2的做法一致,O(k*p*n)复杂度低于1e8. ...
- using 自动释放资源示例
我们在使用SqlConnection的时候可以加入using,那么在using语句结束后就会自动关闭连接.那么这种情况是怎么是实现的呢?我们能够自己写一个类似于SqlConnection的类来让usi ...
- dynamic遇上ADO.NET
传说中的dynamic dynamic是个不合群.不按规则办事的家伙,可以说是个异形,但更恐怖的是它又是无所不知的,任何事情都难不了它(咳咳,它似乎与Lambda表达式是死对头).这令人想起<死 ...
- 教你一招 - 如何安装nopcommerce2.5
教你一招 - 如何安装nopcommerce2.5 29. 五月 2012 16:22 / wcf / 教你一招 . 解决方案 ...