hiho1718(LIS)
生活随笔
收集整理的這篇文章主要介紹了
hiho1718(LIS)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
就是求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 內(nèi)存限制:256MB描述
給定一個由N個元素組成的序列: A1, A2, ... AN,請你找出其中最長的子序列B1, B2, ... BM滿足其中至多有一次上升,即只有一次Bi+1 - Bi > 0。
輸入
第一行包含一個整數(shù)N。 ?
第二行包含N個兩輛不同的整數(shù),A1, A2, ... AN。 ?
對于30%的數(shù)據(jù),1 <= N <= 1000 ?
對于100%的數(shù)據(jù),1 <= N <= 100000 , 1 <= Ai <= 100000
輸出
最長的滿足條件的序列的長度
樣例輸入總結
以上是生活随笔為你收集整理的hiho1718(LIS)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汽车电子can总线(一)物理层
- 下一篇: 联发科技嵌入式_【MTK联发科技嵌入式软