CF Round #FF (Div. 2) C DZY Loves Sequences_DP
2014-07-17 08:57
435 查看
屌丝代码狗
题意:
有一个长度为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
output
看到别人的做法以后才知道这题其实挺水的,dp构造可以很简单。比赛的时候惨爆了好吗,还差一个小时做完了A和B,然后一看C我和我的小伙伴们都兴奋了,这不明摆着DP吗,构造了半天DP,虽说不是特别复杂,但也有点微妙的违和感,交上去一直WA,不知道为什么啊有木有,结束了才看到反例发现自己把最后一位之前的情况考虑的太简单了,有反例。然后自己又按照原来的思路试了半天,一直狗,上网查了一下发现人家的算法很简单吗。。。先预处理出以i结尾和以i开头的所有最长上升子串长度,然后存在三种情况(虽然写代码的时候多写了一种第四种情况),
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);
代码如下:
题意:
有一个长度为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
6 7 2 3 1 5 6
output
5
看到别人的做法以后才知道这题其实挺水的,dp构造可以很简单。比赛的时候惨爆了好吗,还差一个小时做完了A和B,然后一看C我和我的小伙伴们都兴奋了,这不明摆着DP吗,构造了半天DP,虽说不是特别复杂,但也有点微妙的违和感,交上去一直WA,不知道为什么啊有木有,结束了才看到反例发现自己把最后一位之前的情况考虑的太简单了,有反例。然后自己又按照原来的思路试了半天,一直狗,上网查了一下发现人家的算法很简单吗。。。先预处理出以i结尾和以i开头的所有最长上升子串长度,然后存在三种情况(虽然写代码的时候多写了一种第四种情况),
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 100010 int 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>
相关文章推荐
- Codeforces Round #FF (Div. 2/C)/Codeforces446A_DZY Loves Sequences(DP)
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) C - DZY Loves Sequences (DP)
- 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) 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. 1) A. DZY Loves Sequences
- Codeforces Round #FF (Div. 2)-C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2)446A - DZY Loves Sequences(最长上升子序列变形)
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
- Codeforces Round #FF (Div. 1)——A贪心——DZY Loves Sequences
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
- Codeforces Round #FF (Div. 2)C. DZY Loves Sequences