首页 > 代码库 > hdu 1051 - 贪心,水题

hdu 1051 - 贪心,水题

题目链接

一堆小木棍,每个有两个属性值(l,w),对小木棍分组,每一组内的小木棍存在这样一个序列满足s1<=s2<=s3.....<=sn,[s1<=s2当且仅当s1.l<=s2.l&&s1.w<=s2.w],问最小的分组数

------------------------------------------------------------------------------------------

贪心

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
typedef long long LL;
using namespace std;
typedef pair<int,int> int2;
const int N = 6000;
int2 dots[N];
bool vis[N];
bool cmp(const int2 &a,const int2 &b){
     if(a.first==b.first) return a.second<b.second;
     return a.first<b.first;
}
int main(){
    int t; cin>>t; while(t--){
        int n; cin>>n;
        for(int i=0;i<n;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            dots[i] = make_pair(a,b);
        }
        std::sort(dots,dots+n,cmp);
        memset(vis,false,sizeof(vis));
        int ans = 0;
        for(int i=0;i<n;i++){
            if(!vis[i]){
                ans++;
                int weight = dots[i].second;
                for(int j=i+1;j<n;j++){
                    if((!vis[j])&&(dots[j].second>=weight)){
                        weight = dots[j].second;
                        vis[j] = true;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

hdu 1051 - 贪心,水题