CodeForces - 446A DZY Loves Sequences(dp)
题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增。
分析:
1、因为至多修改一个数字,假设修改a[i],
2、若能使a[i] < a[i + 1] 且 a[i] > a[i - 1],则修改a[i]能得到的最长连续子序列长度为l[i - 1] + r[i + 1] + 1。
3、若不满足条件2,则修改a[i]能得到的最长连续子序列长度应取l[i - 1] + 1(即从a[i-1]能向左延伸的最大长度加上a[i]形成的序列)和r[i + 1] + 1的最大值。
4、枚举a[i]取最大值即可。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int l[MAXN];//从a[i]向左延伸的长度
int r[MAXN];//从a[i]向右延伸的长度
int main(){
int n;
scanf("%d", &n);
l[0] = 1;
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
if(i){
if(a[i] > a[i - 1]){
l[i] = l[i - 1] + 1;
}
else{
l[i] = 1;
}
}
}
if(n == 1){
printf("1\n");
return 0;
}
r[n - 1] = 1;
for(int i = n - 2; i >= 0; --i){
if(a[i] < a[i + 1]){
r[i] = r[i + 1] + 1;
}
else{
r[i] = 1;
}
}
int ans = Max(r[1], l[n - 2]) + 1;//修改a[0]或a[n-1]能得到的最长连续序列长度的最大值
for(int i = 1; i < n - 1; ++i){
if(a[i + 1] - a[i - 1] > 1){
ans = Max(ans, l[i - 1] + r[i + 1] + 1);
}
else{
ans = Max(ans, l[i - 1] + 1);
ans = Max(ans, r[i + 1] + 1);
}
}
printf("%d\n", ans);
return 0;
}
CodeForces - 446A DZY Loves Sequences(dp)的更多相关文章
- codeforces#FF DIV2C题DZY Loves Sequences(DP)
题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...
- Codeforces 446A. DZY Loves Sequences (线性DP)
<题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...
- codeforces 446A DZY Loves Sequences
vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...
- CodeForces 446A DZY Loves Sequences (DP+暴力)
题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...
- Codeforces Round #FF 446A DZY Loves Sequences
预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...
- Codeforces 447C - DZY Loves Sequences
447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...
- CodeForces 444C. DZY Loves Physics(枚举+水题)
转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...
- CodeForces 445B. DZY Loves Chemistry(并查集)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...
- CodeForces - 710E Generate a String (dp)
题意:构造一个由a组成的串,如果插入或删除一个a,花费时间x,如果使当前串长度加倍,花费时间y,问要构造一个长度为n的串,最少花费多长时间. 分析:dp[i]---构造长度为i的串需要花费的最短时间. ...
随机推荐
- Kubernetes企业安全
导读 所有利益相关者预先参与一个布局良好的计划,这是构建更安全的容器环境的第一步.如今,容器仍然是应用程序部署和迁移的主流技术.行业专家Paul Rubens将其分解为可以理解的几个部分——陷阱.容器 ...
- Thinkcmf任意漏洞包含漏洞分析复现
简介 ThinkCMF是一款基于PHP+MYSQL开发的中文内容管理框架,底层采用ThinkPHP3.2.3构建.ThinkCMF提出灵活的应用机制,框架自身提供基础的管理功能,而开发者可以根据自身的 ...
- 在普通WEB项目中使用Spring
Spring是一个对象容器,帮助我们管理项目中的对象,那么在web项目中哪些对象应该交给Spring管理呢? 项目中涉及的对象 我们回顾一下WEB项目中涉及的对象 Servlet Request ...
- 058、Java中定义一个没有参数没有返回值的方法
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 045、Java中使用if语句进行判断
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-music
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- ES6学习笔记-扩展运算符(...)
扩展运算符的定义: es6中引入扩展运算符(...),它用于把一个数组转化为用逗号分隔的参数序列. 它常用在不定参数个数时的函数调用,数组合并等情形. 用法一:不定参数个数时的函数调用 <scr ...
- 指令——ps -ef
一个完整的指令的标准格式: Linux通用的格式——#指令主体(空格) [选项](空格) [操作对象] 一个指令可以包含多个选项,操作对象也可以是多个. 指令:ps [process show] 作 ...
- NOIP2016天天爱跑步解题思路
算法:LCA,树上差分+(乱搞) 如果有写错的地方请大佬更正 对于100%数据: u表示起点,v表示终点 对于一条u到v的路径,先讨论LCA!=u&&LCA!=v的情况: 分为u到LC ...
- JavaScript.StringObjec.replace
//StringObject.replace(/regexp/,newContent); //1当newContent为新字符串,就直接用newContent对匹配的内容进行替换. //2当newCo ...