1. 程式人生 > 實用技巧 >hdu1325 Is It A Tree? solution

hdu1325 Is It A Tree? solution

. 最長上升子序列加強版

描述

給出N個數,它們各不相同,求最長上升子序列

輸入

先給出一個數字N,代表有N組資料
對於每組資料,先給出一個數字TOT,TOT小於等於40000.
接下來有TOT個數字,為1到40000的某個排列.

輸出

針對每組資料,輸出最長上升序列的長度

樣例

輸入

複製
4
6
4
2
6
3
1
5
10
2
3
4
5
6
7
8
9
10
1
8
8
7
6
5
4
3
2
1
9
5
8
9
2
3
1
7
4
6

輸出

複製
3
9
1
4
#include<bits/stdc++.h>
using namespace std;
const
int N=40000+20; int a[N],ans=1,c[N],h[N]; int lowbit(int x) { return x&(-x); } void insert(int x,int vol) { while(x<=N) { c[x]=max(c[x],vol); x+=lowbit(x); } } int ask(int x) { int sum=0; while(x) { sum=max(sum,c[x]); x-=lowbit(x); } return
sum; } int main() { int t; cin>>t; while(t--) { memset(c,0,sizeof(c)); ans=0; int n; cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; for(int i=1; i<=n; i++) { h[i]=ask(a[i])+1; // for(int k=1;k<=N;k++)
// cout<<c[k]<<" "; // cout<<endl; // cout<<h[i]<<" daan"<<endl; insert(a[i]+1,h[i]); ans=max(ans,h[i]); } cout<<ans<<endl; } }