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 ...
随机推荐
- cf494A Treasure
A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- jQuery 遍历 json 方法大全
1.for循环: var obj = { "status":1, "bkmsg":"\u6210\u529f", "bkdata& ...
- poj 2251 Dungeon Master(bfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- html5+css3中的background: -moz-linear-gradient 用法 (转载)
转载至-->http://www.cnblogs.com/smile-ls/archive/2013/06/03/3115599.html 在CSS中background: -moz-linea ...
- eclispe远程调试tomcat
在eclispe中新建web应用,名字叫webtest.里面只有一个HelloServlet.Web.xml配置如下. 修改tomcat的启动脚本startup.bat.复制startup.bat为s ...
- [置顶] Android安全机制分析
Android系统是基于Linux内核开发的,因此,Android系统不仅保留和继承了Linux操作系统的安全机制,而且其系统架构的各个层次都有独特的安全特性[2] . 1. Linux内核层安全机制 ...
- BuguMongo是一个MongoDB Java开发框架,集成了DAO、Query、Lucene、GridFS等功能
http://code.google.com/p/bugumongo/ 简介 BuguMongo是一个MongoDB Java开发框架,它的主要功能包括: 基于注解的对象-文档映射(Object-Do ...
- Chapter 4: Spring and AOP:Spring's AOP Framework -- draft
Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...
- MyEclipse 安装activiti designer
下载activiti designer 文件地址:http://activiti.org/designer/archived/ 注意:我的是myeclipse9.0,我下载的版本是:(当我下载高版本安 ...
- asp.net实现将网页存为mht格式文件,类似于网页另存为mht功能
MHT 首先说一下什么是mht格式文件,MHT叫“web单一文件”,就是把网页中包含的图片,CSS文件以及HTML文件全部放到一个MHT文件里面,而且浏览器可以直接读取显示.可以通过ie浏览器将一个网 ...