hiho1718(LIS)

就是求2段下降子序列的和的最长,那正向一次LIS,反向一次LIS,然后枚举分界点就可以了。。




/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 100005
#define nm 1000005
#define pi 3.1415926535897931
using namespace std;
const int inf=1000000007;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}





int n,a[NM],d[NM],c[NM],f[NM],ans,len;
int main(){
	n=read();if(n==1)return 0*printf("1\n");
	dec(i,n,1)a[i]=read();
	d[len=1]=a[1];c[1]=1;
	inc(i,2,n){
		if(d[len]>=a[i])d[lower_bound(d,d+1+len,a[i])-d]=a[i];
		else d[++len]=a[i];
		c[i]=len;
	}
	mem(d);d[len=1]=-a[n];f[n]=1;d[0]=-inf;
	dec(i,n-1,1){
		if(d[len]>=-a[i])d[lower_bound(d,d+1+len,-a[i])-d]=-a[i];
		else d[++len]=-a[i];
		f[i]=len;
	}
	//inc(i,1,n)printf("%d ",b[i]);putchar('\n');
	//inc(i,1,n)printf("%d ",f[i]);putchar('\n');
	inc(i,1,n)ans=max(ans,c[i]+f[i+1]);
	return 0*printf("%d\n",ans);
}



#1718 : 最长一次上升子序列

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

给定一个由N个元素组成的序列: A1, A2, ... AN,请你找出其中最长的子序列B1, B2, ... BM满足其中至多有一次上升,即只有一次Bi+1 - Bi > 0。

输入

第一行包含一个整数N。  

第二行包含N个两辆不同的整数,A1, A2, ... AN。  

对于30%的数据,1 <= N <= 1000  

对于100%的数据,1 <= N <= 100000 , 1 <= Ai <= 100000

输出

最长的满足条件的序列的长度

样例输入
5  
3 5 4 1 2
样例输出
4

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值