hdu 1051_貪心

http://acm.hdu.edu.cn/showproblem.php?pid=1051

#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn = 5010;
struct stick
{
    int length;
    int weight;
    bool vis;
};
bool cmp(stick s1, stick s2)
{
    if(s1.length < s2.length)
        return true;
    else if(s1.length == s2.length)
        return s1.weight < s2.weight;
    return false;
}
int n;
stick s[maxn]; 
int main()
{
    int test;
    scanf("%d", &test);
    while(test--)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &s[i].length, &s[i].weight);
            s[i].vis = false;
        }
        sort(s, s+n, cmp);
        
        int ans = 0;
       
        for(int i = 0; i < n; i++)
        {
            if(!s[i].vis)
            {
                s[i].vis = true;
                ++ans;
                
                int weight = s[i].weight;
                for(int j = i+1; j < n; j++)
                    if(!s[j].vis && s[j].weight >= weight)
                    {
                        s[j].vis = true;
                        weight = s[j].weight;
                    }
            }
        }
        
        printf("%d\n", ans);
    }
    
    return 0;
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章