poj 2533 Longest Ordered Subsequence

//有两种做法:一种是DP,另一种是二分查找! 
#include <iostream>
using namespace std;

int num[1010], dp[1010];

int main()
{
    int n, i, j, ans = 0;
    cin >> n; 
    for (i = 0; i < n; i++){
        cin >> num[i];
    }
    for (i = 0; i < n; i++){
        dp[i] = 1;
    }
        
    for (i = 1; i < n; i++){
        for (j = 0; j < i; j++){
            if (num[i] > num[j] && dp[i] <= dp[j])
               dp[i] = dp[j] + 1;
        }
    }
    
    for (i = 0; i < n; i++){
        if (dp[i] > ans)
            ans = dp[i];
    }
    
    cout << ans << endl;
    
    system("pause");
}



/*
#include <iostream>
using namespace std;

int num[1010];

int main()
{
    int i, j, n, top = 0, tmp, low, high, mid;
    cin >> n;
    num[0] = -1;
    for (i = 0; i < n; i++){
        cin >> tmp;
        if (tmp > num[top]){
            num[++top] = tmp;
        }
        else{
             low = 1, high = top;
             while (low <= high){
                   mid = (low + high) / 2;
                   if (tmp > num[mid]){
                       low = mid + 1;
                   }
                   else{
                        high = mid - 1;
                   }
             }
             num[low] = tmp;
        }
    }
    
    cout << top << endl;
    
    system("pause");
}
*/

你可能感兴趣的:(poj 2533 Longest Ordered Subsequence)