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 ...
随机推荐
- AngularJS bootStraping
看这个 http://www.dotnet-tricks.com/Tutorial/angularjs/5aWL261214-Understanding-AngularJS-Bootstrap-Pro ...
- SMTP邮件传输协议发送邮件和附件
在以前接触的项目中,一直都是在做网站时用到了发送mail 的功能,在asp 和.net 中都有相关的发送mail 的类, 实现起来非常简单.最近这段时间因工作需要在C++ 中使用发送mail 的功能, ...
- 转: requestAnimationFrame,Web中写动画的另一种选择
HTML5/CSS3时代,我们要在web里做动画选择其实已经很多了: 你可以用CSS3的animattion+keyframes; 你也可以用css3的transition; 你还可以用通过在canv ...
- [android]Gradle: 执行失败的任务 ': processDebugManifest'
发现这一问题的解决方案: gradle 组装-信息确实给了我提示清单有不同版本的 SDK 并不能合并. 编辑我的清单和 build.gradle 文件和再工作的一切所需. 要弄清楚你需要编辑 uses ...
- Linux常用的系统监控shell脚本
http://www.linuxqd.com下面是我常用的几个Linux系统监控的脚本,大家可以根据自己的情况在进行修改,希望能给大家一点帮助.1.查看主机网卡流量 #!/bin/bash #netw ...
- [置顶] ※数据结构※→☆线性表结构(list)☆============单向链表结构(list single)(二)
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始:链表是使用指针进行构造的列表:又称为结点列表,因为链表是由一个个结点组装起来的:其中每个结点都有指 ...
- SQL Server数据转换【包括Geometry类型】的技巧总结
1. 字段的组合: update new_master_location set tmp_street_unique=street+'_'+city+'_'+state+'_'+zip+'_'+con ...
- PL/SQL 基础编程
PL/Sql 编程 PL/Sql结构 [declare] --声明变量 begin --执行部分 [exception] ---异常处理部分 end PL/Sql 基本数据类型 数值类型 1. nu ...
- 练习笔记:net,JqueryUI实现自动补全功能
1.首先建立个空的Web项目 2.将下载好的JqueryUI文件保存到JS文件加下 3.引入JS文件 <link href="JS/css/ui-lightness/jquery-ui ...
- Object-c中@private、@protected、@public解析
Objective-C中,类的实例化变量的范围有@private.@protected.@public.他们代表的意思和C++中相同,只是前面添加了一个@符号.下面介绍一下他们代表的范围: 指令 意思 ...