HDU1051 Wooden Sticks【贪心】

题目链接:

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

题目大意:

有N根木棍,每根木棍的长度和质量是已知的,机器要处理这N根木棍,处理时间和木棍的长度和

权重有关。第一根木棍的处理时间为1min,之后处理的木棍如果长度大于等于前一根木棍的长度

并且权重也大于等于前一根木棍的长度,就不需要处理时间;否则就需要1min的处理时间。问:

最小的处理时间为多少。

思路:

贪心思想。先将木棍按长度从小到大排列,如果长度一致,则按权重从小到大排列。然后根据题目

要求,如果后边的木棍长度和权重都大于前者,则不需要处理时间,否则加1min处理时间。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 5050;

struct Node
{
    int l, w;
    int flag;
}node[MAXN];

int cmp(Node a, Node b)
{
    if(a.l != b.l)
        return a.l < b.l;
    return a.w < b.w;
}

int main()
{
    int N,T;
    cin >> T;
    while(T--)
    {
        cin >> N;
        memset(node,0,sizeof(node));
        for(int i = 0; i < N; ++i)
        {
            cin >> node[i].l;
            cin >> node[i].w;
        }
        sort(node,node+N,cmp);
        int Sum = 0;
        for(int i = 0; i < N; ++i)
        {
            if(node[i].flag == 1)
                continue;
            Sum++;
            int k = i;
            for(int j = i+1; j < N; ++j)
            {
                if(node[j].l >= node[k].l && node[j].w >= node[k].w && node[j].flag == 0)
                {
                    node[j].flag = 1;
                    k = j;
                }
            }
        }
        printf("%d\n",Sum);
    }

    return 0;
}
时间: 2024-11-08 22:10:00

HDU1051 Wooden Sticks【贪心】的相关文章

HDU1051 Wooden Sticks 【贪心】

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11627    Accepted Submission(s): 4807 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

HDU 1051 Wooden Sticks (贪心)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11249    Accepted Submission(s): 4629 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

ZOJ 1025. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some

HDOJ 1051. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15564    Accepted Submission(s): 6405 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

SPOJ MSTICK. Wooden Sticks 贪心 结构体排序

MSTICK - Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine

POJ 1065. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19992   Accepted: 8431 Description There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworkin

HDU 1051 Wooden Sticks 贪心||DP

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22000    Accepted Submission(s): 8851 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU Wooden Sticks (贪心)

Wooden Sticks Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 19   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description There is a pile of n woo

Wooden Sticks(贪心)

Wooden Sticks. win the wooden spoon:成为末名. 题目地址:http://poj.org/problem?id=1065 There is a pile of n wooden sticks. a woodworking machine:木工机器. setup time:启动时间. my codes: #include<iostream> #include<cstdio> #include<cstring> #include<al