533
Idea: greedy hair, sort first.
The weight of a person is sorted in ascending order. After sorting, the person with the largest weight is traversed from I = 0 to find the person matching him (the person with the lightest weight ), if the two men are on the ship, if they do not exist, then I will be on the ship myself. In either case, I will not consider the next time after boarding the ship;
The following code is provided:
# Include <stdio. h> # include <stdlib. h> # define MAX 310 // number of people most supported by the program int weigh [MAX]; // store the weight of each person // The fast rank function int com (const void *, const void * B) {return (* (int *) B-* (int *) a);} int main () {int s; int w, n; // the maximum load of the ship corresponding to each group of test data, and the number of people scanf ("% d", & s); while (s --) {int count = 0; // The number of required vessels: int I, j, tag = 0; // whether a matching sign is found. 1 indicates matching scanf ("% d", & w, & n); for (I = 0; I <n; I ++) scanf ("% d", & weweigh [I]); qsort (weweigh, n, sizeof (weweigh [0]), com); // Sort/* ( I = 0; I <n; I ++) printf ("% d", weigh [I]); printf ("\ n "); */for (I = 0; I <n; I ++) {// printf ("loop % d \ n", I); for (j = n-1; j> = 0; j --) {// assign the weight of a person to-1 upon boarding, it means that he will not consider it next time if (weweigh [I]> 0 & weweigh [j]> 0 & weweigh [I] + weweigh [j] <= w) {// printf ("weweigh [% d] = % d, weweigh [% d] = % d \ n", I, weweigh [I], j, weweigh [j]); count ++; weweigh [I] = weweigh [j] =-1; // tag = 1 will not be considered next time; // indicates that personal I has found the matched break;} if (tag! = 1) {// if no match is found for the I-th individual, a person needs to take the ship count ++; // the ID of the person in the I-th ship is a separate boat. Weweigh [I] =-1 ;}} printf ("% d \ n", count );}}