微智科技网
您的当前位置:首页POJ 3903-Stock Exchange/POJ 2533-Longest Ordered Subsequence(LIS-最长递增子序列长度)

POJ 3903-Stock Exchange/POJ 2533-Longest Ordered Subsequence(LIS-最长递增子序列长度)

来源:微智科技网
Stock Exchange
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 67 Accepted: 2342

Description

The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of them. He is very concerned about the evolution of the stock exchange. He follows stock prices every day looking for rising trends. Given a sequence of numbers p1, p2,...,pn representing stock prices, a rising trend is a subsequence pi1 < pi2 < ... < pik, with i1 < i2 < ... < ik. John’s problem is to find very quickly the longest rising trend.

Input

Each data set in the file stands for a particular set of stock prices. A data set starts with the length L (L ≤ 100000) of the sequence of numbers, followed by the numbers (a number fits a long integer). 
White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

Output

The program prints the length of the longest rising trend. 
For each set of data the program prints the result to the standard output from the beginning of a line.

Sample Input

6 
5 2 1 4 5 3 
3  
1 1 1 
4 
4 3 2 1

Sample Output

3 
1 
1

Hint

There are three data sets. In the first case, the length L of the sequence is 6. The sequence is 5, 2, 1, 4, 5, 3. The result for the data set is the length of the longest rising trend: 3.

Source


两道裸题,求LIS-最长递增子序列长度。
但是POJ 3903数据量需要O(nlogn)复杂度的代码来完成:

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 100010
int a[MAXN],dp[MAXN];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("F:/cb/read.txt","r",stdin);
    //freopen("F:/cb/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    while(cin>>n)
    {
        for(int i=0; i<n; ++i)
            cin>>a[i];
        fill(dp,dp+n,INF);
        for(int i=0; i<n; i++)
            *lower_bound(dp,dp+n,a[i])=a[i];
        int ans=lower_bound(dp,dp+n,INF)-dp;
        cout<<ans<<endl;
    }
}

POJ 2533 用n方复杂度可过:
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1100
int a[MAXN],dp[MAXN];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("F:/cb/read.txt","r",stdin);
    //freopen("F:/cb/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=0; i<n; ++i)
        cin>>a[i];
    int res=0;
    for(int i=0; i<n; ++i)
    {
        dp[i]=1;
        for(int j=0; j<i; ++j)
            if(a[j]<a[i])
                dp[i]=max(dp[i],dp[j]+1);
        res=max(res,dp[i]);
    }
    cout<<res<<endl;
}


因篇幅问题不能全部显示,请点此查看更多更全内容