CF Round #FF (Div. 2) C DZY Loves Sequences_DP
来源:互联网 发布:辐射3胜利狙击枪数据 编辑:程序博客网 时间:2024/05/22 00:44
屌丝代码狗
ans=max(ans,以i+1开头的最长子串长+1)
ans=max(ans,以i-1结尾的最长子串长+1)
题意:
有一个长度为n的整数序列a,可以改变期中某一个元素的值,求最长子串的长度。1 ≤ n ≤ 10^5,1 ≤ ai ≤ 10^9。
Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In a single line print the answer to the problem — the maximum length of the required subsegment.
Sample test(s)
input
67 2 3 1 5 6
output
5
ans=max(ans,以i+1开头的最长子串长+1)
ans=max(ans,以i-1结尾的最长子串长+1)
if(a[i+1]-a[i-1]>1) ans=max(ans,以i-1结尾的最长子串长 + 以i+1为开头的最长子串长 + 1);
代码如下:
<span style="font-size:18px;">#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>using namespace std;#define mxn 100010int dp[2][mxn],n,a[mxn];//dp[0][i]为以i结尾的最长长度,dp[1][i]为以i开头的最长长度int main(){while(scanf("%d",&n)!=EOF){for(int i=0;i<n;++i)scanf("%d",&a[i]);for(int i=0;i<n;++i)for(int j=0;j<2;++j)dp[j][i]=1;int ans=1;for(int i=1;i<n;++i)if(a[i]>a[i-1])dp[0][i]=max(dp[0][i],dp[0][i-1]+1);for(int i=n-2;i>=0;--i)if(a[i]<a[i+1])dp[1][i]=max(dp[1][i],dp[1][i+1]+1);for(int i=0;i<n-1;++i)ans=max(ans,dp[1][i+1]+1);for(int i=n-1;i>1;--i)ans=max(ans,dp[0][i-1]+1);for(int i=1;i<n-1;++i)if(a[i+1]-a[i-1]>1)ans=max(ans,dp[0][i-1]+dp[1][i+1]+1);printf("%d\n",ans);}return 0;}</span>
0 0
- CF Round #FF (Div. 2) C DZY Loves Sequences_DP
- Codeforces Round #FF (Div. 2):C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) Problem C DZY Loves Sequences
- Codeforces Round #FF (Div. 2)C - DZY Loves Sequences
- Codeforces Round #FF (Div. 2)-C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) C DZY Loves Sequences
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2)-C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) C. DZY Loves Sequences
- Codeforces Round #FF (Div. 1) C - DZY Loves Fibonacci Numbers
- Codeforces Round #FF (Div. 二):C. DZY Loves Sequences
- Codeforces Round #FF (Div. 1) C. DZY Loves Fibonacci Numbers
- Codeforces Round #FF (Div. 2) Problem A DZY Loves Hash
- Codeforces Round #FF (Div. 2) Problem B DZY Loves Strings
- 20140716
- BigDecimal的自增长
- 下载输出到CSV文件
- Field 'id' doesn't have a default value
- OpenGL编程低级错误范例手册
- CF Round #FF (Div. 2) C DZY Loves Sequences_DP
- struts 2笔记:使用annotation,不用struts.xml文件来配置
- jsp page指令详解
- 告别码农,成为真正的程序员
- Android之SurfaceView学习(一)
- 黑马程序员---动态代理
- crm创建启用停用用户
- 精减版printf
- c++标准库