刷題——Wooden Sticks POJ - 1065

/*
這道題求得就是不下降序列的最少種類
先將一個值按從小到排序,相同的時候另一個值也按從小到大的順序排列
然後遍歷一遍,用一個數組放置每種不下降序列的尾巴,依次更新
更新的過程可以用二分優化一下
*/
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct node{int w,h;};
node e[5100];
int n;
bool cmp(node a,node b){
    return a.w<b.w||(a.w==b.w&&a.h<b.h);
}
int a[5005],k;
void solve(int x){
    for(int i=0;i<k;i++){
        if(a[i]<=e[x].h){
            a[i]=e[x].h;
            return;
        }
    }
    a[k++]=e[x].h;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d %d",&e[i].w,&e[i].h);
        }
        sort(e,e+n,cmp);
        k=1;
        a[0]=e[0].h;
        for(int i=1;i<n;i++){
            solve(i);
        }
        printf("%d\n",k);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章