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 ...
随机推荐
- MapReuce 编程总结-多MapReduce执行
学习hadoop,必不可少的就是写MapReduce程序,当然,对于简单的分析程序,我们只需一个MapReduce就能搞定,这里就不提单MapReuce的情况了,网上例子很多,大家可以百度Google ...
- IIS上不能播放mp4
iis不支持mp4格式,需要手动添加. 进入iis服务管理器,打开你的网站,然后点击MIME类型---添加(扩展名:mp4 MIME类型:application/octet-stream) 如此即 ...
- web之ios开关按钮
就是这个开关,代码是从weui上拷贝出来的. 至于weui自己百度吧 ^_^~ 开关代码如下: <!DOCTYPE html> <html lang="en"&g ...
- Spring集成Quartz定时器
<!-- Spring集成Quartz开始 --> <bean id="startQuertz" lazy-init="false" auto ...
- 在python文本编辑器里如何设置Tab为4个空格
python中缩进一般为四个空格,我总结3种常用编辑器中种如何设置Tab键为四个空格 第一种:下载python3.5时自带de 一个IDLE编辑器 在Options选项下的Configure IDLE ...
- return和break的区别
/* Name:return和break的区别 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月25日 02:13:22 Description:以下代码 ...
- 射频识别技术漫谈(28)——基于MF1射频卡的酒店门锁设计
电子门锁是现代星级酒店管理电子化.智能化的重要电子设备.相较于传统的机械锁,基于RFID技术的电子门锁使用方便,易于管理,安全性高,可实现对开锁用户的分优先级自动管理,对房间入住信息实现自动统计与报表 ...
- 删除input或textarea输入框在移动版显示的阴影(Safari/iPhone)
移动端浏览网页元素时,自动给input和textarea添加了内部阴影的效果,只需加入样式即可去除 input,textarea{-webkit-appearance: none;} input[ty ...
- c 统计字符串中字符出现的个数
1.单纯用数组来解题 思路:从左往右循环,每次碰到一个字符就和左边的字符串比较,如果有相同的就右移,如果没有找到相同的就从这个位置向右统计个数并输出. #include<stdio.h> ...
- SQL Server 数据控制语句(DCL)
DCL控制语句用来设置更改用户或角色的权限. 授予权限操作——GRANTSQL Server服务器通过手语权限表来控制用户对数据库的访问.在数据库中添加一个新用户之后,若不尽兴额外操作,该用户只有ch ...