POJ 1065-Wooden Sticks 贪心问题

题目来源: http://acm.pku.edu.cn/JudgeOnline/problem?id=1065

 

 

解题报告:

 

本题采用贪心算法, 先从一堆sticks中找到长度最小的stick, 对长度相等的, 找到weight最小的,  找到后, 设长度为l, 重量为w, 然后, 从剩下的sticks中找到长度、重量都大于等于l和w的sticks中长度及重量最小的stick,再依次找下去,如果不存在,则就直接找长度、重量最小的stick,且时间+1.

 

接下来,证明, 贪心算法的可行性.

 

第一步, 证明总能找到一组最优解是用贪心策略得到的。

 

设该题有一个最优解 ()(l,w)()

 

设其中,(l,w)为最小的stick,则l,w一定小于之前的调用的stick的l'和w',即在调用这根stick时,set up时间一定要加一。因此,将(l,w)挪到最前方,不会减少也不会增加调用这根stick的set up time。然后,对l',w'都大于等于l,w的sticks中的l',w'最小的stick,如果它在原最优解中,不需算set up time, 那么把它挪到(l,w)最小的那根stick后,也不会减少、不会增加set up time。如果,它在原最优解中,需要计算set up time,那么它一定不是最优解,否则,将它挪到(l,w)后,可以减少一个set up time。 依次归纳下去,可以得到一定有一个最优解可以通过贪心策略得到。

 

第二步,证明子问题也需要最优解,显然,如果子问题不是最优的,那么总问题的解一定也不是最优的,因此子问题也需要最优解。

 

 #include <iostream> using namespace std; int *l, *w; int cnt; #define INF 1000000 int min(int begin, int end, int pl, int pw) //找出从l[begin], w[begin]到l[end], w[end]中l,w都大于pl,pr //中的l的最小值的位置,若不存在,则直接返回l的最小值的位置 { int minl=INF, minw=INF; int locate=-1; for(int i=begin;i<end;i++) { if(l[i]>=pl && w[i]>=pw) { if(minl>l[i]) { minl=l[i]; minw=w[i]; locate=i; } else if(minl==l[i]) { if(minw > w[i]) { minw=w[i]; locate=i; } } } } if(locate==-1) { cnt++; for(int i=begin;i<end;i++) { if(minl>l[i]) { minl=l[i]; minw=w[i]; locate=i; } else if(minl==l[i]) { if(minw > w[i]) { minw=w[i]; locate=i; } } } } return locate; } void exchange(int i, int j) { int temp1=l[i]; int temp2=w[i]; w[i]=w[j]; w[j]=temp2; l[i]=l[j]; l[j]=temp1; } int main() { int n; cin >> n; while(n--) { int m; cin >> m; l=new int[m]; w=new int[m]; cnt=1; for(int i=0;i<m;i++) scanf("%d%d",&l[i],&w[i]); int pl=0,pw=0; for(int i=0;i<m;i++) { int t=min(i,m,pl,pw); pl=l[t]; pw=w[t]; exchange(i,t); } delete [] l; delete [] w; cout << cnt << endl; } return 0; }

 

 

附录:

 

Wooden Sticks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10697   Accepted: 4337

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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .

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 2n 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

你可能感兴趣的:(input,processing,each,Exchange,output,Shapes)