hdu 1051

原创 2018年04月17日 21:09:28

Wooden Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24299    Accepted Submission(s): 9869


Problem 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 woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:

(a) The setup time for the first wooden stick is 1 minute.
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup.

You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).
 

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
 

Source
 
//先排序---后选择第一个没有用过的木头一次向后找,用掉所有可以用掉的木头,然后返回第一个没用过的木头继续找
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
struct stick
{
    int len;
    int wei;
}a[5001];
bool used[5001];

bool cmp(stick k1,stick k2)       //按照长度从小到大排序,若长度相同按照重量递增排序
{
    if(k1.len==k2.len)
        return k1.wei<k2.wei;
    else
        return k1.len<k2.len;
}

int main()
{
    int T,n,i,j,st,cnt;
    bool flag;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=0;i<n;++i)
            scanf("%d%d",&a[i].len,&a[i].wei);
        sort(a,a+n,cmp);
        memset(used,false,sizeof(used));
        used[0]=true;
        st=0;       //记录第一个没有用过的木头
        cnt=0;
        while(st<n)
        {
            ++cnt;
            for(i=st+1,j=st,flag=true;i<n;++i)
            {
                if(used[i])
                    continue;
                if(a[j].len<=a[i].len&&a[j].wei<=a[i].wei)
                {
                    used[i]=true;
                    j=i;
                }
                else
                {
                    if(flag)
                    {
                        st=i;      //只记录第一个没用过的木头
                        flag=false;
                    }
                }
            }
            if(flag)    //说明都用过了
                break;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guihaiyuan123/article/details/79980877

HDU-1051-Wooden Sticks(C++ && 不水的贪心!)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • qq_16542775
  • qq_16542775
  • 2015-06-20 11:38:29
  • 1326

hdu acm 1051 贪心,排序,dilworth定理,最长递减序列长度

dilworth 定理: 最小链划分数  =  最大反链的长度。 于是先按照l 或者 w 排序, 再求...
  • royliu1
  • royliu1
  • 2014-10-21 21:57:55
  • 283

【HDU】1051 - Wooden Sticks(贪心 & 二分 || 动态规划)

点击打开题目 Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/...
  • wyg1997
  • wyg1997
  • 2016-07-25 19:09:22
  • 236

hdu-1051

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • wojiaohuangyu
  • wojiaohuangyu
  • 2014-10-28 12:37:17
  • 581

[ACM] hdu 1051 Wooden Sticks

Wooden Sticks Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota...
  • sr19930829
  • sr19930829
  • 2014-03-10 20:56:49
  • 2071

HDU:1051 Wooden Sticks(贪心+动态规划DP||LIS?)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • zugofn
  • zugofn
  • 2016-08-11 16:15:56
  • 213

ACM-贪心之Wooden Sticks——hdu1051

Wooden Sticks Problem Description There is a pile of n wooden sticks. The length and weight of eac...
  • lx417147512
  • lx417147512
  • 2013-10-21 15:53:16
  • 1301

HDU 1051 与1257类似 二维排序,然后贪心

#include #include #include #include using namespace std; typedef struct { int x, y; }...
  • vsooda
  • vsooda
  • 2012-09-21 09:18:46
  • 474

【HDU】-1051-Wooden Sticks(贪心,好||DP)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To...
  • Bear1998
  • Bear1998
  • 2016-08-11 11:40:21
  • 227

杭电OJ测试数据挖掘程序

即便是人手工提交代码,想要得到题目测试数据的长度最多需要提交31次。既然是机械化的重复性工作,当然是交给计算机来做啦。 diginput.py脚本代码如下: import requests imp...
  • hoxily
  • hoxily
  • 2015-03-28 16:36:51
  • 2351
收藏助手
不良信息举报
您举报文章:hdu 1051
举报原因:
原因补充:

(最多只允许输入30个字)