cf446A DZY Loves Sequences
1 second
256 megabytes
standard input
standard output
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
In a single line print the answer to the problem — the maximum length of the required subsegment.
6
7 2 3 1 5 6
5
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
题意是有一串序列,可以把其中的一个数修改成任意值,求这样改完以后最长上升子序列长度
三维dp:前i个、是否用掉第i个、是否和以前的相连的最长长度
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define inf 0x7fffffff
#define ll long long
using namespace std;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,ans;
int a[100005],f[100005][2][2];//used? last?
int main()
{
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
for(int i=1;i<=n;i++)
{
f[i][0][0]=f[i][1][1]=1;
if(i!=1)f[i][1][0]=2;
if(a[i]>a[i-1])
{
f[i][0][0]=max(f[i][0][0],f[i-1][0][0]+1);
f[i][1][0]=max(f[i][1][0],f[i-1][1][0]+1);
f[i][1][1]=max(f[i][1][1],f[i-1][0][0]+1);
}
else
{
f[i][1][1]=max(f[i][1][1],f[i-1][0][0]+1);
}
if(i!=1&&a[i]>a[i-2]+1)f[i][1][0]=max(f[i][1][0],f[i-1][1][1]+1);
ans=max(ans,f[i][0][0]);
ans=max(ans,f[i][1][0]);
ans=max(ans,f[i][1][1]);
}
printf("%d",ans);
}
cf446A DZY Loves Sequences的更多相关文章
- CF446A DZY Loves Sequences 简单dp
DZY has a sequence a, consisting of n integers. We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ...
- 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 Round #FF (Div. 2):C. DZY Loves Sequences
C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #FF 446A DZY Loves Sequences
预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...
- [CodeForces - 447C] C - DZY Loves Sequences
C - DZY Loves Sequences DZY has a sequence a, consisting of n integers. We'll call a sequence ai, ai ...
- Codeforces 447C - DZY Loves Sequences
447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...
- C. DZY Loves Sequences
C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
随机推荐
- cf445A DZY Loves Chessboard
A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...
- hdu3401:单调队列优化dp
第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...
- shell输出加颜色
shell输出加颜色 #cat a.sh #!/bin/sh blue=`tput setaf 4` reset=`tput sgr0` echo "${blue}[INFORMATION] ...
- hadoop2对比hadoop1
hadoop2对比hadoop1 1.体系结构 HDFS+MapReduce,共同点都是分布式的,主从关系结构. HDFS=一个NameNode+多个DataNode, NameNode含有我们用户存 ...
- WAS集群系列(6):集群搭建:步骤4:安装WAS升级软件
逐步点击"下一步",注意一处流程,例如以下列举: "升级软件"安装的路径设置,建议与之前的WAS及IHS安装的绝对路径同样,例如以下所看到的: 逐步点击,完毕安 ...
- CSS3绘制环形进度条
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Eclipse搭建Android开发环境(安装ADT,Android4.4.2)(转)
使用Eclipse做Android开发,需要先在Eclipse上安装ADT(Android Development Tools)插件. 1.安装JDK 1.7 JDK官网http://www.orac ...
- 模型 Model
模型层包含所有视图或控制器不包含的应用程序逻辑 模型应该包含所有应用程序业务逻辑和数据库访问逻辑 主要部分 bll和dal 例如,使用ado.net或者ef5.0访问sql数据库的代码
- EClipse开发NDK流程
EClipse开发NDK流程(现在studio也在2.2之后支持了非常简单,只要创建项目的时候勾选c++支持就可以了) 什么情况下使用ndk,1.保护代码,java很容易反编译,c/c++反汇编比 ...
- "ORA-00942: 表或视图不存在 "的原因和解决方法[转]
采用Oracle数据库,使用Powerdesigner设计,生成Sql文件导入后查询出现“ORA-00942: 表或视图不存在 ”,很是郁闷,这个问题以前出现过,当初解决了,但因好久没有使用,这次竟然 ...