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 ...
随机推荐
- 将窗口置顶的方法:SetWindowPos、AttachThreadInput、SwitchToThisWindow
将窗口置顶的方法:SetWindowPos.AttachThreadInput.SwitchToThisWindow [转]http://hi.baidu.com/neil_danky/item/f9 ...
- 请求http服务
①服务方法 [HttpGet]//get服务 public JsonResult GetUserName(int id) { try { IXiao_UserBLL bll = new Xiao_Us ...
- linux杂记(六)档案权限
[KANO@kelvin ~]$ ls -al 总用量 drwx------. KANO KANO 10月 : . drwxr-xr-x. root root 9月 : .. drwxrwxr-x. ...
- js程序调试技巧
1.No "Access-Control-Allow-origin" 解决方案:这是API参数没有穿对的跨域错误,修改API(ajax请求路径)以保证其参数传递正确即可: 2.lo ...
- 50句高级SQL语句
一个题目涉及到的50个Sql语句 --(下面表的结构以给出,自己在数据库中建立表.并且添加相应的数据,数据要全面些. 其中Student表中,SId为学生的ID) ------------------ ...
- 运行SPL Streams debugger(sdb)的两种方法
You can use the SPL Streams Debugger in InfoSphere® Streams Studio to help you debug your SPL applic ...
- sublime模式下开启vim并修改esc
首先我用的是sublime text2 sublime下开启vim模式: 在Preference -> Setting-User里面加上 "ignored_packages" ...
- c++ 学习笔记(常见问题与困惑)(转载)
本问转自: http://www.cnblogs.com/maowang1991/p/3290321.html 1.struct成员默认访问方式是public,而 class默认访问方式是privat ...
- Mysql innodb 后台的7大线程与3大内存
A:一个master 线程(innodb 几乎在这个线程上实现有所有功能) B:一个lock 监控线程 C:一个错误监控线程 D:四个IO线程(insert buffer thread\log thr ...
- 《转》java动态代理(JDK和cglib)
该文章转自:http://www.cnblogs.com/jqyp/archive/2010/08/20/1805041.html JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的 ...