贪心 --- 排序 + 贪心
Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11023 Accepted Submission(s): 4530
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
【题目来源】
Asia 2001, Taejon (South Korea)
【题目大意】
有一堆木棒,这些木棒的长度和重量是给定的。这些木棒将被机器一根接一根的进行处理。
机器处理每一根木棒需要一些时间,这个时间被叫做建立时间。
这个时间是这样定义的:
1.第一根木棒的建立时间是1分钟;
2.在处理一根长度为l、重量为w的木棒之后,如果木棒的l'<=l、w<=w',那么这个机器将不需要建立时间。否则,这个机器将需要1分钟的建立时间。
你需要找到处理完这些木头的最短建立时间。题目中的例子看懂就知道怎么做了。
【题目分析】
首先按照l排序,当l相同的时候,再按照w排序,然后就是按照顺序一遍一遍的标记并统计,直至所有的都被标记,最后的count就是答案。
奇怪的是,我的代码在测试样例的时候是错的,我提交后竟然ac了。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct Node { int l,w; bool mark; }; bool cmp(Node a,Node b) { if(a.l==b.l) { return a.w<b.w; } else return a.l<b.l; } Node node[5050]; int main() { int T; scanf("%d",&T); while(T--) { int n,i,j; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d",&node[i].l,&node[i].w); node[i].mark=0; } sort(node,node+n,cmp); int cl=node[0].l,cw=node[0].w; node[0].mark=1; int cnt=1; for(i=1;i<n;i++) { for(j=i;j<n;j++) { if(!node[j].mark&&node[j].l>=cl&&node[j].w>=cw) { node[j].mark=1; cl=node[j].l; cw=node[j].w; } } j=1; while(node[j].mark) j++; i=j; if(i==n) break; cnt++; node[i].mark=1; cl=node[i].l; cw=node[i].w; } printf("%d\n",cnt); } return 0; }