C. 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.
本题坑还算比較多的,我用的递推,dp1[i]表示以i结尾的递增序列的长度,dp2[i]表示已i開始的递增序列的长度,要找出最长的仅仅改变一个数就能构成递增序列的序列长度仅仅须要枚举i即可了,代码例如以下
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
typedef long long LL;
using namespace std;
int A[100005];
int dp1[100005],dp2[100005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
scanf("%d",A+1);
dp1[1]=1;
for(int i=2;i<=n;i++)
{
scanf("%d",A+i);
if(A[i]>A[i-1])dp1[i]=dp1[i-1]+1;
else dp1[i]=1;
}
dp2[n]=1;
for(int i=n-1;i>=1;i--)
{
if(A[i]<A[i+1])dp2[i]=dp2[i+1]+1;
else dp2[i]=1;
}
int m=1;
A[0]=0;
A[n+1]=1000000005;
for(int i=1;i<=n;i++){
int t=dp1[i-1]+dp2[i+1]+1;
if(A[i+1]-A[i-1]<2)t=max(dp1[i],dp2[i])+1;
m=max(m,t);
}
cout<<m<<endl;
}
return 0;
}
C. DZY Loves Sequences的更多相关文章
- cf446A DZY Loves Sequences
A. 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 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 Round #FF 446A DZY Loves Sequences
预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...
- [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 ...
- 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 ...
- 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] + ...
- DZY Loves Sequences
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
随机推荐
- 2018-8-27-C#-powshell-调用
title author date CreateTime categories C# powshell 调用 lindexi 2018-8-27 16:20:4 +0800 2018-06-18 20 ...
- Google Capture The Flag 2018 (Quals) - Reverse - Beginner's Quest - Gatekeeper
参考链接:https://ctftime.org/task/6264 题目 It's a media PC! All fully purchased through the online subscr ...
- Support for the experimental syntax 'decorators-legacy' isn't currently enabled (7:1):
1.产生原因:项目不支持装饰器 2.解决方法: 2.1 执行 yarn 安装完整依赖: 2.2 如果依赖时yarn.lock变化了,并且项目有git目录,则将提示的文件提交到git仓库 ? Are y ...
- idea中ehcahe配置中 Cannot find the declaration of element 'ehcache'.
ehcahe.xml 中报错: Cannot find the declaration of element 'ehcache'. 打开settings->languages&frame ...
- web源码泄露
http://www.am0s.com/ctf/175.html vim 文件泄露 1.备份文件 源文件:index.php 备份文件:index.php~ 2.临时文件 源文件:index.php ...
- BitMap位图
BitMap位图算法https://blog.csdn.net/varyall/article/details/79662029 常见面试题 题1:在2.5亿个整数找出不重复的整数,内存不足以容纳着2 ...
- css--图片整合(精灵图)
图片整合(精灵图) 精灵图的优点: 减少图片的字节 减少了网页的http请求,从而大大的提高了页面的性能 解决了网页设计师在图片命名上的困扰,只需对一张集合的图片上命名就可以了,不需要对每一个小元素进 ...
- POJ 3784 Running Median (动态中位数)
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...
- Windows 下搭建 SVN服务器
目录 一 .安装Visual SVN 二.配置SVN 三.安装TortoiseSVN 四.上传项目到远程仓库 五.从远程仓库下载项目 六.检出项目 七.版本回退 参考链接 https://blog ...
- re模块的高级使用
主要有四个方法: search : 从字符串的任意位置开始检索,首次匹配即结束 findall : 全部匹配 sub : 将正则匹配结果进行替换 split : 根据正则匹配结果将字符串进行切分,然后 ...