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 ...
随机推荐
- Delphi 调用系统中的计算器、记事本、画图软件方法
1.直接调用 前面uses加 ShellAPI ShellExecute(Handle, 'open', PChar('calc.exe'), nil, nil, SW_SHOW); 2.直接调 ...
- Delphi TcxTreelist 设置scrollbars 不起作用的原因
最近设置TcxTreelist的滚动条,发现水平的不起作用, 即使设置 sboth也不起作用. 查找,发现设置的一些属性导致了这个原因, 建立备忘,如下图: 1.属性, 这个页面,设置的表格,怎么也看 ...
- pager-taglib使用示范
把pager-taglib.jar导入到当前项目/Test/WebRoot/WEB-INF/lib下 建立一个分页类 package web; import java.util.List; /** * ...
- NetAnalyzer笔记 之 五 一些抓包技巧分享(不定期更新)
[创建时间:2016-03-12 10:00:00] [更新时间:2016-05-21 10:00:00] NetAnalyzer下载地址 前一段时间应为工作关系,NetAnalyzer笔记系列已经很 ...
- 再看ADO对象模型
在敲学生管理系统之前,我们就学习过ADO的有关知识.但是昨天被问到ADO的几个对象,顿时无言!为什么会出现这样的结果呢,明明是学习过了,而且也实践过(红皮书的五个例子).这充分说明了,在以往的学习过程 ...
- java 图片 批量 压缩 +所有压缩
/* oldsrc : 原图片地址目录 如 'd:/' newsrc : 压缩后图片地址目录 如 'e:/' widthdist,heightdist : 压缩后的宽和高 ...
- LoadRuner性能测试之内存分析方法及步骤(Windows)
1.首先观察Available Mbytes(可用内存),至少要>=1/2的内存空间 2.然后观察Pages/sec值是不是很大 3.再观察Page Faules/sec是不是很大,其值表示 ...
- lseek() 定位一个已经打开的文件
Lseek lseek()的作用是,设置文件内容的读写位置. 每个打开的文件都有一个"当前文件偏移量",是一个非负整数,用以度量从文件开始处计算的字节数.通常,读写操作都是从当前文 ...
- for循环和经典案例
循环:初始条件,循环条件,状态改变,循环体.for(初始条件;循环条件;状态改变){ 循环体}for(int i=1;i<=10;i++){ }例子:100以内与7有关的数.求100以内所有数的 ...
- 未能加载文件或程序集“System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”
最近用vs2012发布程序,然后将更新后的程序文件部署到服务器上,由于服务器上本来有此系统,所以只更新了修改的文件 . 进行系统登录时提示:未能加载文件或程序集“System.Web.Extensio ...