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

 

转载于:https://www.cnblogs.com/fanminghui/p/4335996.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值