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取看题解 ...
随机推荐
- Spring重要注解@ControllerAdvice
@ControllerAdvice是一个@Component,用于定义@ExceptionHandler,@InitBinder和@ModelAttribute方法,适用于所有使用@RequestMa ...
- vue init webpack nameXXX 报错问题:
vue新建demo项目报错如下: M:\lhhVueTest>vue init webpack L21_VueProject vue-cli · Failed to download repo ...
- 记录Python类与继承的一个错误
今天在学python的类与继承的时候遇到一个错误,原来是自己在ctrl+c ctrl+v的时候漏了一个括号 class Car(): def __init__(self,make,year,mode ...
- leecode第八十八题(合并两个有序数组)
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums ...
- windows版 Java调用人脸识别离线sdk
最近因工作需求在java-web服务中调用人脸识别离线sdk,主要通过JNA及JNI技术,但均未调试通过,JNA调用时出现以下异常,一直未解决,求大佬指点,导常信息如下: in BaiduFaceAp ...
- Java中的包扫描(工具)
在现在好多应用场景中,我们需要得到某个包名下面所有的类, 包括我们自己在src里写的java类和一些第三方提供的jar包里的类,那么怎么来实现呢? 今天带大家来完成这件事. 先分享代码: 1.这个类是 ...
- Memcached遇到的问题及解决办法
1. memcached make: *** No targets specified and no makefile found. Stop. 其实是因为在安装libevent时增加了版本号导致的, ...
- jenkins之从0到1利用Git和Ant插件打war包并自动部署到tomcat(第一话):初次启动jenkins,输入给定密码后登录失败问题解决
Jenkins是一个持续集成平台,它能够从git等源码管理服务器拉取代码.打包并发布到tomcat等中间件,只要配置好相关插件,就可以做到项目的自动化构建.部署,不论是对开发来说监控代码质量,还是对测 ...
- 「SDOI2008」Sandy 的卡片
用第一个串建立后缀自动机.然后别的串在上面跑.从根节点开始.如果当前不能转移,一直移到slink或者根.如果移到根,能匹配长度变为0,否则变为maxlen[能转移的点]+1,再转移.转移完往slink ...
- codeforces736b Taxes (Codeforces Round #382 (Div. 1))
题意:纳税额为金额的最大因数(除了本身).为了逃税将金额n分为n1+n2+.......问怎样分纳税最少. 哥德巴赫猜想: 任一大于2的偶数都可写成两个质数之和. 质数情况: 任何大于5的奇数都是三个 ...