codeforces#FF DIV2C题DZY Loves Sequences(DP)
题目地址:http://codeforces.com/contest/447/problem/C
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.
能够先进行预处理。将每个数字可向左延伸的数目记录下来,再将可向右延伸的记录下来,最后遍历一遍,假设这个数能够变成大于左边小于右边的数。即右边-左边>1。这是就能够为左边延伸的+右边延伸的+1。假设不行,则仅仅能满足某一边,这是就要为左边或右边较大的延伸数+1.
代码例如以下。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
int a[200000], b[200000], c[200000];
int main()
{
int n, i, j, max1=-1;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
b[0]=1;
for(i=1;i<n;i++)
{
if(a[i]>a[i-1])
{
b[i]=b[i-1]+1;
}
else
b[i]=1;
}
c[n-1]=1;
for(i=n-2;i>=0;i--)
{
if(a[i]<a[i+1])
c[i]=c[i+1]+1;
else
c[i]=1;
}
if(n==1)
printf("1\n");
else
{max1=2;
if(max1<b[n-2]+1)
max1=b[n-2]+1;
if(max1<c[1]+1)
max1=c[1]+1;
for(i=1;i<=n-2;i++)
{
if(a[i+1]-a[i-1]>1)
{
if(max1<b[i-1]+c[i+1]+1)
max1=b[i-1]+c[i+1]+1;
}
else
{
if(max1<b[i-1]+1)
max1=b[i-1]+1;
if(max1<c[i+1]+1)
max1=c[i+1]+1;
}
}
printf("%d\n",max1);}
return 0;
}
codeforces#FF DIV2C题DZY Loves Sequences(DP)的更多相关文章
- CodeForces 447C DZY Loves Sequences DP
题目:click here 题意:求给定序列更改其中一个元素后的最长连续上升子序列的长度 分析:最长的连续子序列有2种,一种是严格上升(没有更改元素)的长度加1,一种是两段严格上升的加起来. #inc ...
- CodeForces 446A DZY Loves Sequences (DP+暴力)
题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...
- codeforces#FF(div2) D DZY Loves Modification
首先要知道选择行列操作时顺序是无关的 用两个数组row[i],col[j]分别表示仅选择i行能得到的最大值和仅选择j列能得到的最大值 这个用优先队列维护,没选择一行(列)后将这行(列)的和减去对应的n ...
- 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 446A DZY Loves Sequences
预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...
- 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 - 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 ...
随机推荐
- ActiveMQ的入门demo
步骤: 1 :下载ActiveMQ 官网:http://activemq.apache.org/ 2 :解压AcitveMQ, 根据自己的操作系统选择运行win64或者win32下的activemq. ...
- Angular ng-repeat 对象和数组遍历
直接上代码 <!DOCTYPE html> <html> <head> <meta name="description" content= ...
- windows理论基础(一)
windows体系结构 一. 用户模式和内核模式 (user mode &kernel mode) Intel x86 处理器的体系结构定义了四种特权级,或特为四个环.来保护系统代码不会被低 ...
- phome_enewsclass 数据表字段解释(栏目主表)
字段名 类型 解释 附加说明 classid smallint(6) 栏目ID bclassid smallint(6) 父栏目ID classname varchar(50) 栏目名称 ...
- MyEclipse13中修改Servlet.java源代码
Servlet.java源代码想要修改的步骤,与低版本的不同废话少说,直接来步骤: 1,在myEclipse的安装目录中搜索com.genuitec.eclipse.wizards文件,如图:选择co ...
- 【hadoop】14、hadoop2.5的mapreduce的 配置
配置mapreduce <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href ...
- ios蓝牙开发(三)app作为外设被连接的实现
再上一节说了app作为central连接peripheral的情况,这一节介绍如何使用app发布一个peripheral,给其他的central连接 还是这张图,central模式用的都是左边的类,而 ...
- ShineTime 是一个效果非常精致的缩略图相册
ShineTime 是一个效果非常精致的缩略图相册,鼠标悬停到缩略图的时候有很炫的闪光效果,基于 CSS3 实现,另外缩略图也会有立体移动的效果.特别适用于个人摄影作品,公司产品展示等用途,快来来围观 ...
- Web Service 入门例子
在网上很常见的一个列子,实现计算器,看完总得自己练练,不然一段时间不接触又忘了 新建一个空网页,因为.net framework 4.0 不能直接建web 服务,只能在项目中添加 然后在项目中添加We ...
- WIN7下OC开发环境的搭建
折腾了一天,才搭建好OC的开发环境,用于OC学习.其中折腾劲儿我也是醉了.感谢我的破联想Ideapad Y470 坚持到了最后,感谢我的固态,感谢CCAV. 用到的工具及下载地址: 1.MAC10.1 ...