转载请注明出处:http://blog.csdn.net/u012860063
题目链接:http://poj.org/problem?id=3903
Description
Input
Output
Sample Input
6 5 2 1 4 5 3 3 1 1 1 4 4 3 2 1
Sample Output
3 1 1
第一种:http://blog.csdn.net/u012860063/article/details/34086819
代码如下:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int a[100047],dp[100047],n; int bin(int size,int k) { int l = 1,r = size; while(l<=r) { int mid = (l+r)/2; if(k>dp[mid]) l = mid+1; else r = mid-1; } return l; } int LIS(int *a) { int i,j,ans=1; dp[1] = a[1]; for(i = 2; i<=n; i++) { if(a[i]<=dp[1]) j = 1; else if(a[i]>dp[ans]) j = ++ans; else j = bin(ans,a[i]); dp[j] = a[i]; } return ans; } int main() { int i; while(~scanf("%d",&n)) { for(i = 1; i <= n; i++) { scanf("%d",&a[i]); } int ans = LIS(a); printf("%d\n",ans); } return 0; }
代码如下:
#include<cstdio> #include<algorithm> using namespace std; #define INF 0x3fffffff int a[100047],dp[100047]; int main() { int t,i; while(scanf("%d",&t)!=EOF) { for(i = 0; i <= t; i++) dp[i]=INF; for(i = 0; i < t; i++) { scanf("%d",&a[i]); } for(i = 0; i < t; i++) { *lower_bound(dp,dp+t,a[i])=a[i]; } printf("%d\n",lower_bound(dp,dp+t,INF)-dp); } return 0; }
poj3903 Stock Exchange最大上升子序列,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012860063/article/details/34495159