Http://acm.nyist.net/JudgeOnline/problem.php? PID = 71
Canoe travel time limit: 3000 MS | memory limit: 65535 kb difficulty: 2
-
-
Description
-
-
During a canoe trip, the canoe can be rented at the port and there is no difference between them. A canoe can only take two people at most, and the total weight of a passenger cannot exceed the maximum carrying capacity of the canoe. We need to minimize the cost of this event, so we need to find the minimum number of canoes that can accommodate all passengers. Write a program to read the maximum carrying capacity, number of passengers, and weight of each passenger in the canoe. Based on the given rules, calculate the minimum number of canoes required for the placement of all passengers and output the results.
-
-
Input
-
-
Input s in the first line, indicating the number of test data groups;
The first row of each group of data contains two integers: W, N, 80 <= W <= 300, 1 <= n <=, W indicates the maximum carrying capacity of a canoe, and N indicates the number of people;
The next set of data is the weight of each person (not greater than the carrying capacity of the ship );
-
-
Output
-
-
The minimum number of canoes required for each group.
-
-
Sample Input
-
-
3 85 6 5 84 85 80 84 90 3 90 45 60 100 5 50 90 40 60
-
-
Sample output
-
-
5 3 3
Solution: after sorting by weight, set the marker for each person, and then install the marker for each ship. Note that each ship can hold up to 2 people.
# Include <stdio. h>
# Include <stdlib. h>
Int CMP (const void * a, const void * B ){
Return * (int *) A-* (int *) B;
}
Int A [330], flag [330];
Int main (){
Int S, W, N, I, T, ANS, sum, Ren;
Scanf ("% d", & S );
While (s --){
Scanf ("% d", & W, & N );
For (I = 0; I <n; I ++ ){
Scanf ("% d", & A [I]);
Flag [I] = 0;
}
Qsort (A, N, sizeof (A [0]), CMP );
Sum = N, ANS = 0;
While (SUM ){
T = W;
Ren = 2;
For (I = n-1; I> = 0; I --){
If (t-A [I]> = 0 & flag [I] = 0 & Ren ){
T-= A [I];
Flag [I] = 1;
Sum --;
Ren --;
}
}
Ans ++;
}
Printf ("% d \ n", ANS );
}
Return 0;
}
NYOJ-71-canoe trip