Trip on a canoe

Source: Internet
Author: User
Subject travel time limit from NYOJ canoe: 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
385 65 84 85 80 84 8390 390 45 60100 550 50 90 40 60
Sample output
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 );}}


Trip on a canoe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.