杭电1051 Wooden Sticks

本文探讨了如何通过贪心算法解决木棒加工问题,旨在寻找最短的机器准备时间。通过对木棒按长度和重量排序,实现了有效的加工顺序规划。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目内容:

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

题目大意:

对于给定长度和重量的木棒,对其进行加工,而在加工的顺序中,若后一根木棒的长度和重量均大于前一根,则可以节省机器重置的时间,对于给定的木棒,求出最小时间


题目分析:

从题意上看,应当归类为最长子序列问题,我所想到第一方法便是利用dp来解决,但卡住了很久,后来决定尝试用贪心的方法来尝试一下,在并没有很大把握情况下水过了这道题,贪心的方案是,将木棒的长度和重量分别作为第一关键字和第二关键字sort排序后,其中一种属性必然不需要再继续考虑,而剩下的则是另一种属性在苏排序后的序列中每出现一个比前者小的数据,总的时间即+1,即贪心的方案,至于此种贪心的正确性,我还在继续验证中


题目代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>

using namespace std;

struct wood
{
    int length;
    int weight;
    int mark;
}sticks[5005];

bool cmp(struct wood x,struct wood y)
{
    if(x.length<y.length)
        return 1;
    else if(x.length==y.length)
    {
        if(x.weight<y.weight)
            return 1;
        else
            return 0;
    }
    else
        return 0;
}

int max(int x,int y)
{
    return x>y?x:y;
}

int min(int x,int y)
{
    return x<y?x:y;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        int max=0;
        int flag;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&sticks[i].length,&sticks[i].weight);
            sticks[i].mark=0;
        }
        sort(sticks,sticks+n,cmp);
        //for(int i=0;i<n;i++)
            //printf("%d %d\n",sticks[i],sticks[i].weight);
        for(int i=0;i<n;i++)
        {
            if(sticks[i].mark==0)
            {
                max++;
                flag=sticks[i].weight;
                //printf("%d %d %d\n",sticks[i].length,sticks[i].weight,max);
                for(int j=i+1;j<n;j++)
                    if(sticks[j].mark==0&&sticks[j].weight>=flag)
                    {
                        flag=sticks[j].weight;
                        sticks[j].mark=1;
                    }
            }
        }
        printf("%d\n",max);
    }
    return 0;
}

解题评价:

作为一道可以用贪心的解决的问题,题目自然不算难,但对于贪心的正确性的证明和使用dp重做则是这道题的重点,这一点我也还在思考中

当前水平评分:4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值