如何找到升序子序列的最小数量

时间:2017-05-16 15:08:30

标签: algorithm dynamic-programming

我遇到了问题。 http://poj.org/problem?id=1065
问题是要找到最小的递增子序列数 我看到有人要找到最长下降子序列的长度。 我不知道为什么这两个数字相等。

#include <iostream>
#include <algorithm>
#include <functional>
#include <memory.h>
/* run this program using the console pauser or add your own getch, 
system("pause") or input loop */
using namespace std;
pair<int,int> stick[5000];
int dp[5000];

int main(int argc, char** argv) {
int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>stick[i].first>>stick[i].second;
        }
    sort(stick,stick+n);
    memset(dp,-1,sizeof(int)*5000);
    for(int i=0;i<n;i++){
       *lower_bound(dp,dp+n,stick[i].second,greater<int>())=stick[i].second; 
        }
        cout<<lower_bound(dp, dp + n, -1, greater<int>()) - dp<<endl;

    }
return 0;
}

1 个答案:

答案 0 :(得分:0)

紧接着Dilworth's theorem。它是解决此类问题的标准技术。