Codeforces Round #FF 446A 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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n,a[110000],dp[110000]; int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",a+i);
int nx=-1;
for(int i=0;i<n;i++)
{
if(nx>i)
{
dp[i]=nx-i;
continue;
}
int j=i;
while(j+1<n&&a[j]<a[j+1]) j++;
dp[i]=j-i+1;
nx=max(nx,j+1);
}
int ans=dp[0];
for(int i=0;i<n;i++)
{
int p=dp[i]+i-1;
if(p-1>=0&&p+1<n&&a[p+1]>a[p-1]+1)
{
ans=max(ans,dp[i]-1+dp[p+1]+1);
}
if(p+2<n&&a[p+2]>a[p]+1)
{
ans=max(ans,dp[i]+dp[p+2]+1);
}
if(p+1<n||i-1>=0) ans=max(ans,dp[i]+1);
i=p;
}
printf("%d\n",ans);
return 0;
}
Codeforces Round #FF 446A DZY Loves Sequences的更多相关文章
- codeforces 446A DZY Loves Sequences
vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...
- Codeforces 446A. DZY Loves Sequences (线性DP)
<题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...
- CodeForces 446A DZY Loves Sequences (DP+暴力)
题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...
- CodeForces - 446A DZY Loves Sequences(dp)
题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增. 分析: 1.因为至多修改一个数字,假设修改a[i], 2.若能使a[i] < ...
- 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] + ...
- 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 ...
- 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#FF DIV2C题DZY Loves Sequences(DP)
题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...
- [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 ...
随机推荐
- JAVA: httpclient 详细说明——第四章;
httpclient 具体解释--第一章. httpclient 具体解释--第二章: httpclient 具体解释--第三章: httpclient 具体解释--第四章: httpclient 具 ...
- 用python做自己主动化測试--对server端的自己主动化測试(3)-很多其它http client实例
上一篇中仅仅是实现了一个非常easy的http client功能,request还提供了keep alive, SSL, 多文件上传,cookie 管理功能,http requests头管理等丰富的功 ...
- 深入浅出jsonp(转)
前言 第一次听说jsonp,其实早在2年之前.当时在做一个活动页面的抽奖模块,要从服务端get一个概率,当时什么都不懂,同事说用ajax,我就用ajax,同事说dataType改成jsonp,我就改成 ...
- GPU 编程入门到精通(五)之 GPU 程序优化进阶
博主因为工作其中的须要,開始学习 GPU 上面的编程,主要涉及到的是基于 GPU 的深度学习方面的知识.鉴于之前没有接触过 GPU 编程.因此在这里特地学习一下 GPU 上面的编程. 有志同道合的小伙 ...
- hdu3480二维斜率优化DP
Division Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others) Tota ...
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
- vs2013 ADO联系SQL server2012数据库
平时,给定ADO例如使用以下过程数据源中的数据的数据库应用程序 (1) 创建一个Connection 物.定义的连接字符串信息.它包含了数据源名称.用户ID.密码.连接超时 . 默认数据库的位置和光标 ...
- 【SSH三框架】Hibernate基金会七:许多附属业务
相对于上述一关系,在这里,下一个一对多关系说明. 另外,在上述.我们描述了许多人描述的一一对应关系.在关系数据库是多对一的关系.但也有许多关系. 但,只知道它是不够的,Hibernate它是一种面向对 ...
- python有些错误换行问题解决
有时候数据会遇到一些错误包.例如,正确的数据应: 20141010,aaa,bbb,ccc,ddd,eee 但实际的数据是来: 20141010,aaa,bbb, ccc,ddd, eee 这样出现错 ...
- log4j的配置信息(转)
首先,在项目中的classes 中新建立一个log4j.properties文件即可: 在实际编程时,要使Log4j真正在系统中运行事先还要对配置文件进行定义.定义步骤就是对Logger.Append ...