poj 3903 Stock Exchange

//和2533是同一类的题目,都是简单的LIS题目,这题用二分+DP比较好,因为数据比较大!容易超时! 
#include <iostream>
using namespace std;

long num[100001], tmp[100001]; 

int main()
{
    int n, i, j, len, left, right, mid;
    while (cin >> n){ 
        for (i = 0; i < n; i++){
            cin >> num[i]; 
        } 
        
        memset(tmp, 0, sizeof(tmp)); 
        len = 0; 
        tmp[0] = -1;
        for (i = 0; i < n; i++){
            if (num[i] > tmp[len]){
                tmp[++len] = num[i]; 
            }
            else{
                 left = 1, right = len;
                 while (left <= right){
                       mid = (left + right) / 2;
                       if (num[i] > tmp[mid]){
                           left = mid + 1; 
                       }
                       else{
                            right = mid - 1; 
                       } 
                 } 
                 tmp[left] = num[i]; 
            } 
        } 
        
        cout << len << endl;
    } 
    
    system("pause"); 
} 

你可能感兴趣的:(poj 3903 Stock Exchange)