codeforces C. DZY Loves Sequences

http://codeforces.com/contest/447/problem/C

题意:给你n个数的序列,然后让你改变其中的一个数,求得最长上升连续序列的长度值。

思路:先从左边开始求出连续递增序列的值记录在l数组,然后从右边开始再求一遍连续递增序列的值记录在r数组中,然后从2-n-1位置遍历,改变其中的一个数,使得组成一个新的递增序列,求出长度最大值就可以。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #define maxn 1000100
 7 #define ll long long
 8 using namespace std;
 9 const int inf=1<<30;
10 
11 int n,m,k;
12 ll a[maxn];
13 char str[maxn];
14 int l[maxn],r[maxn];
15 
16 int main()
17 {
18     scanf("%d",&n);
19     for(int i=1; i<=n; i++)
20     {
21         cin>>a[i];
22     }
23     l[1]=1;
24     for(int i=2; i<=n; i++)
25     {
26         if(a[i]>a[i-1])
27         {
28             l[i]=l[i-1]+1;
29         }
30         else
31             l[i]=1;
32     }
33     r[n]=1;
34     for(int i=n-1; i>=1; i--)
35     {
36         if(a[i+1]>a[i]) r[i]=r[i+1]+1;
37         else r[i]=1;
38     }
39     int ans=max(r[2]+1,l[n-1]+1);
40     for(int i=2; i<n; i++)
41     {
42         if(a[i+1]-a[i-1]>1) ans=max(ans,l[i-1]+r[i+1]+1);
43         else ans=max(ans,max(r[i+1],l[i-1])+1);
44     }
45     printf("%d\n",ans);
46     return 0;
47 }
View Code

 

posted @ 2015-03-13 20:52  null1019  阅读(119)  评论(0编辑  收藏  举报