Title Description
For a canoe trip, canoes can be rented at the port, and there is no difference between them. A canoe can take up to two persons and the total weight of the passenger must not exceed the maximum capacity of the canoe. We want to minimize the cost of this activity, so find the minimum number of canoe bars that can accommodate all passengers. Now please write a program that reads the maximum capacity of the canoe, the number of passengers and the weight of each passenger. According to the rules given, calculate the minimum number of canoe bars that must be placed for all passengers and output the results.
Enter a description
The first line, enter S, indicates the number of groups of test data
The first row of each group of data consists of two integers w,n,80<=w<=200,1<=n<=300,w is the maximum capacity of a canoe, n is the number of people
The next set of data is the weight of each person (not larger than the ship's carrying capacity)
Output description
The minimum number of canoes required for each group of people.
Input sample
3 - 6 5 - - the - the - 3 - $ - - 5 - - - + -
Output sample
5 3 3
Test code
1#include <stdio.h>2#include <stdlib.h>3 4 intcmpConst void*a,Const void*b)5 {6 return*(int*) B-* (int*) A;7 }8 9 intMainvoid)Ten { One intS, W, N, I, p, q, num; A inta[ -]; -scanf"%d", &s); - while(s--) the { -scanf"%d%d", &w, &n); - for(i =0; I < n; i++) - { +scanf"%d", A +i); - } +Qsort (A, N,sizeof(a[0]), CMP); Ap =0; atQ = n-1; -num =0; - while(Q >=p) - { - if(A[p] = =W) - { inp++, num++; - } to Else if(A[p] + a[q] <=W) + { -p++, Q--, num++; the } * Else $ {Panax Notoginsengp++, num++; - } the } +printf"%d\n", num); A } the return 0; +}
Travel on a canoe