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 ...
随机推荐
- [Luogu1436]棋盘分割(动态规划)
[Luogu1436]棋盘分割 题目背景 无 题目描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的两部分中的任意一块继续如此分割,这样割了(n-1)次后, ...
- (NOIP)CSP-S 2019前计划
前言 无 1.NOIP原题板刷 NOIP原题板刷 这是一篇咕了的blog 2.牛客 & ACwing & 洛谷 网课学习 收获还是蛮大的,不过我没有写博客 3.codeforces专项 ...
- Final修饰的字段是否可以通过反射设置值
案发现场 经常听说final修饰的字段是常量不能改变的他的值,但是以外发现 Integer.java源码中的字段“value”是final,但是可以通过反射改变他的值. public final cl ...
- bzoj4128 Matrix 矩阵 BSGS
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4128 题解 想了十分钟没有任何思路. 然后一眼瞥见一句话"数据保证在 \(p\) 内 ...
- jmeter性能工具 之 传参 (三)
jmeter 主要有三种方式:键值对传参,json格式传参,外部传参 1.键值对传参 可以参考上篇登陆,使用的传参方式是键值对传参 2.json 格式传参 用json 格式传参不要忘了加http 头 ...
- Task9.Attention
注意力模型最近几年在深度学习各个领域被广泛使用,无论是图像处理.语音识别还是自然语言处理的各种不同类型的任务中,都很容易遇到注意力模型的身影.所以,了解注意力机制的工作原理对于关注深度学习技术发展的技 ...
- <i>和<em>、<b>和<strong>标签的区别
在HTML4.01中:< b > < i > 是视觉要素(presentationl elements),分别表示无意义的加粗,无意义的斜体,仅仅表示样式上是粗体或斜体,而没有 ...
- Appium解决native+webview混合型APP(公众号、小程序)切换webview后元素无法定位问题
问题:最近在做一个安卓+H5混合开发的APP自动化测试,发现在从native切换到webview后,元素仍然无法找到,报错:no such element 思路:于是思考webview会不会像web页 ...
- [CSP-S模拟测试]:Market(背包DP)
题目描述 在比特镇一共有$n$家商店,编号依次为$1$到$n$.每家商店只会卖一种物品,其中第$i$家商店的物品单价为$c_i$,价值为$v_i$,且该商店开张的时间为$t_i$. $Byteasar ...
- HDU4405 Aeroplane chess (概率DP,转移)
http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落 ...