题目内容:
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).
(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