hdu1025 超经典最长上升子序问题

来源:互联网 发布:数据库系统工程师希赛 编辑:程序博客网 时间:2024/06/14 17:33
/*    2015.11.16    1025.最长上升自序问题    n(1 ≤ n ≤ 500,000)    Sample Input21 22 131 22 33 1Sample OutputCase 1:My king, at most 1 road can be built.Case 2:My king, at most 2 roads can be built.*/#include <cstdio>#include <iostream>#include <cstdlib>#include <cstring>#include <string>#include <algorithm>const int MAX = 500005;const int INF = 5000000;using namespace std;int arr[MAX];int solve[MAX];int solveQ(int n){    int fir, sec;    for(int i=0; i<n; i++){        solve[i] = INF;    }    for(int i=1; i<=n; i++){        scanf("%d%d", &fir, &sec);        arr[fir] = sec;    }    for(int i=1; i<=n; i++){        *lower_bound(solve,solve+n, arr[i]) = arr[i];    }    return lower_bound(solve, solve+n,INF) - solve;}int main(){    int n, ans;    int i=1;    while(scanf("%d", &n)!=EOF){        ans = solveQ(n);        printf("Case %d:\n", i++);        if(ans == 1){            printf("My king, at most 1 road can be built.\n\n");        }else{            printf("My king, at most %d roads can be built.\n\n", ans);        }    }    return 0;}/*    具体解体思路见挑战程序设计竞赛中P65页,很经典。    另,该题有坑。    1.如果结果是1,则单词road应为单数,反之为复数。    2.一句话结束后有两个回车。*/

0 0
原创粉丝点击