Wooden Sticks
Problem Description There is a pile of n wooden sticks. The length and weight of each stick is known in advance. The sticks is 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 is associated with cleaning operations and changing tools and shapes. The setup times of the woodworking machine is 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 would need no setup time for a stick of length l ' and weight W ' if l<=l ' and W<=w '. Otherwise, it'll need 1 minute for setup.
You is 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 is (4,9), (5,2), (2,1), (3,5), and (1,4), then the Minimum setup time should be 2 minutes since there are 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 on the first line of the input file. Each test case consists of a lines:the first line has a integer n, 1<=n<=5000, that represents the number of Wo Oden sticks in the test case, and the second line contains n 2 positive integers L1, W1, L2, W2, ..., LN, WN, each of Magn Itude at most 10000, where Li and wi is the length and weight of the i th wooden stick, respectively. The 2n integers is 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 This is the first time I have compared the system's contact greedy algorithm. Feel worse. The problem cannot be done. But I have heard a very classic words, take out and the big everybody to encourage a bit. "Failure is not a shameful thing, there is nothing to gain from failure." ", yes, the title does not have any shame, from the topic of no gain is."
I refer to other people's code, combined with their own understanding, summed up the knowledge points and pay attention to the point.
This topic is sorted first, according to the length or weight of the row can, when the length (weight) of the same weight (length) row, from large to small or from childhood to most can. I know here, no problem. After sorting, the problem can be simplified (assuming that the length of the line varies by length, length, and so on by weight, I assume that the row is from large to small.) That is, all the weight values after sorting can be represented at least as several sets. The length is no longer necessary, from the first number of arrays to traverse, as long as the weight value satisfies the condition, then the two sticks will satisfy the condition.
At first I did not understand why greedy can find the best solution. Also in this problem tangled for a long time, feel more painful. Later through their own instead focusing, or thought out. Note: There is no difference in the results of greed or motion, that is, greed can be solved, and efficiency is higher than moving. This is determined by the particularity of the data of the problem.
There are some rules to note: Describe it with a picture.
The reference code is as follows:
A few days without knocking at the code, feeling degraded. He was careless and dead.
#include <iostream>
#include <algorithm>
using namespace std;
struct SIZE
{
int l;
int w;
} STICKS[5005];
int flag[5005];
BOOL CMP (const size &A,CONST size &B)//Here is the sort.
{//write sorting functions with special care.
//if (A.W!=B.W)//Wrong here, this means that if the weight varies, according to the length of the row, if the weight is equal, then the weight row. (No sense.) If the length
of the IF (A.L!=B.L)
return a.l>b.l;//varies by length, from the large to the small row
else
return a.w>b.w;//the length is equal, Then by weight from large to small arrange
}
int main ()
{
int n,min,cases;
int i,j,s;
cin>>cases;
for (j=0;j<cases;j++)
{
cin>>n;
for (i=0;i<n;i++)
{
cin>>sticks[i].l>>sticks[i].w;
flag[i]=0;
}
Sort (sticks,sticks+n,cmp);
s=0;
for (i=0;i<n;i++)
{
if (flag[i]) continue;
MIN=STICKS[I].W;
for (int j=i+1;j<n;j++)
{
if (MIN>=STICKS[J].W &&!flag[j])
{
min=sticks[j].w;
flag[j]=1;
}
}
s++;
}
cout<<s<<endl;
}
System ("pause");
return 0;
}