A typical greedy algorithm, like the previous 1029, is also a scheduling problem during working hours.
# Include <iostream>
# Include <vector>
# Include <algorithm>
Using namespace STD;
Const int maxsize = 5000;
Struct stick
{// Wood bar
Int length; // Length
Int width; // width
Bool isvisited;
} Sticks [maxsize];
Bool lessthan (const stick & S1, const stick & S2)
{// Sort by size
Return s1.length <s2.length;
}
Int main (void)
{
INT cases, I, J, K, N;
Cin> cases;
For (I = 1; I <= cases; ++ I)
{
Cin> N;
Vector <stick> stickvect;
For (j = 0; j <n; ++ J)
{
Cin> sticks [J]. length> sticks [J]. width;
Sticks [J]. isvisited = false;
Stickvect. push_back (sticks [J]);
}
Sort (stickvect. Begin (), stickvect. End (), lessthan); // sort
// Greedy method
Int length, width, time = 0, Count = 0;
For (j = 0; j <n; ++ J)
{
If (stickvect [J]. isvisited = false)
{// Not accessed yet, serving as the vanguard of a New Round
Time ++; // a new round, time plus
Length = stickvect [J]. length;
Width = stickvect [J]. width;
Stickvect [J]. isvisited = true;
For (k = 0; k <n; ++ K)
{
If (stickvect [K]. isvisited = false)
{
If (stickvect [K]. length> = Length & stickvect [K]. width> = width)
{// This wooden bar can be successfully passed together with the vanguard
Stickvect [K]. isvisited = true;
Length = stickvect [K]. length;
Width = stickvect [K]. width;
}
}
}
}
}
Cout <time <Endl;
}
Return 0;
}