Wooden Sticks HDU - 1051 (贪心)

 Wooden Sticks

  HDU -1051 
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.
Output
The output should contain the minimum setup time in minutes, one per line. 
Sample Input
3 
5 
4 9 5 2 2 1 3 5 1 4 
3 
2 2 1 1 2 2 
3 
1 3 2 2 3 1
Sample Output
2
1
3
 
 
 
 
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct note
{
    int u,v;
}e[10000];
int cmp(note A,note B)
{
    if(A.u!=B.u)//在长度不等时,按长度从大到小排
        return A.u>B.u;
    else
        return A.v>B.v;//长度相等时,按重量从大到小排
}
int n,sum,flag;
int book[10000];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int i,j;
        memset(book,0,sizeof(book));
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&e[i].u,&e[i].v);
        }
        sort(e,e+n,cmp);
        int s=0,minn;
        for(i=0;i<n;i++)//贪心,,按例子说:5.4.3.2.1排好后按重量排,能 
        {               //从大到小排几组(重量),就是需要几分钟。
            if(book[i]==0)
            {
                minn=e[i].v;
            }
            else
                continue;
            for(j=i+1;j<n;j++)
            {
                if(e[j].v<=minn&&book[j]==0)
                {
                    book[j]=1;
                    minn=e[j].v;
                }
            }
            s++;
        }
        printf("%d\n",s);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值