首页 > 代码库 > hdu 1087 最大上升子序列的和(dp或线段树)

hdu 1087 最大上升子序列的和(dp或线段树)

Super Jumping! Jumping! Jumping!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23328    Accepted Submission(s): 10266


Problem Description
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.



The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.
 

 

Input
Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N 
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.
 

 

Output
For each case, print the maximum according to rules, and one line one case.
 

 

Sample Input
3 1 3 2
4 1 2 3 4
4 3 3 2 1
0
 

 

Sample Output
4
10
3
 1 /* 2 时间复杂度O(n^2) 3 */ 4 #include <iostream> 5 #include <cstdio> 6 #include <cstring> 7 using namespace std; 8  9 typedef __int64 LL;10 const int maxn=1005;11 LL dp[maxn],f[maxn];12 inline int max(int a,int b){return a>b?a:b;}13 int main()14 {15     int n,i,j;16     while(scanf("%d",&n),n)17     {18         for(i=1;i<=n;i++)19         {20             scanf("%d",f+i);dp[i]=f[i];21         }22         for(i=1;i<=n;i++)23         {24             for(j=i-1;j>0;j--)25                 if(f[i]>f[j])26                     dp[i]=max(dp[i],dp[j]+f[i]);27         }28         LL ans=0;29         for(i=1;i<=n;i++)30             ans=max(ans,dp[i]);31         printf("%I64d\n",ans);32     }33     return 0;34 }
  1 /*  2 时间复杂度O(nlogn)  3 */  4 #include <iostream>  5 #include <cstdio>  6 #include <cstring>  7 using namespace std;   8   9 const int maxn = 1005; 10 #define lson l,mid,rt<<1 11 #define rson mid+1,r,rt<<1|1 12 int sum[maxn<<2]; 13 int a[maxn],b[maxn],c[maxn]; 14 inline int max(int a,int b){return a> b?a:b;} 15  16 void swap(int &a,int &b){int t=a;a=b;b=t;} 17  18 void qsort(int l,int r) 19 { 20     if(l<r) 21     { 22         int t=b[l],i=l,j=r; 23         while(i!=j) 24         { 25             while(b[j]>=t && i<j) j--; 26             while(b[i]<=t && i<j) i++;     27             if(i<j) swap(b[i],b[j]); 28         } 29         b[l]=b[i];b[i]=t; 30         qsort(l,i-1); 31         qsort(i+1,r); 32     } 33 } 34 int binarysearch(int l,int r,int val)//二分查找,未找到返回-1 35 { 36     int mid,ans=-1; 37     while(l<=r) 38     { 39         mid=(l+r)>>1; 40         if(val>c[mid]) l=mid+1; 41         else if(val==c[mid]) return mid; 42         else r=mid-1; 43     } 44     return ans; 45 } 46  47 void build(int l,int r,int rt) 48 { 49     sum[rt] = 0; 50     if(l == r) 51         return ; 52     int mid = (l + r)>>1; 53     build(lson); 54     build(rson); 55 } 56 void updata(int pos,int v,int l,int r,int rt) 57 { 58     if(l == r) 59     { 60         sum[rt]=v; 61         return ; 62     } 63     int mid = (l + r)>>1; 64     if(pos <= mid) 65         updata(pos,v,lson); 66     else 67         updata(pos,v,rson); 68     sum[rt]=(sum[rt<<1]>sum[rt<<1|1]?sum[rt<<1]:sum[rt<<1|1]); 69 } 70 int query(int L,int R,int l,int r,int rt) 71 { 72     if(L <= l && R >= r) 73     { 74         return sum[rt]; 75     } 76     int mid = (l + r)>>1; 77     int ans; 78     if(L <= mid)  79         ans = query(L,R,lson); 80     if(R > mid) 81         ans = max(ans,query(L,R,rson)); 82     return ans; 83 } 84  85 int main() 86 { 87     int n,i; 88     while(scanf("%d",&n),n) 89     { 90         for(i=1;i<=n;i++) 91         { 92             scanf("%d",a+i);b[i]=a[i]; 93         } 94         qsort(1,n); 95         int cnt=1;c[1]=b[1]; 96         for(i=2;i<=n;i++) if(b[i]!=b[i-1])//离散化去重 97             c[++cnt]=b[i]; 98         build(1,cnt,1); 99         int x,maxv,ans=0;100         for(i=1;i<=n;i++)101         {102             x=binarysearch(1,cnt,a[i]);103             if(x>1)104             {105                 maxv=query(1,x-1,1,cnt,1);106                 updata(x,maxv+a[i],1,cnt,1);107             }108             else 109             {110                 maxv=0;111                 updata(x,maxv+a[i],1,cnt,1);112             }113             if(maxv+a[i]>ans) ans=maxv+a[i];114         }115         printf("%d\n",ans);        116     }117     return 0;118 }

 

 

hdu 1087 最大上升子序列的和(dp或线段树)