Travel on a canoe

Source: Internet
Author: User

Describe

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.

Input
The
first line enters S, which represents the number of groups of test data;
The first row of each group of data includes two integers w,n,80<=w<=200,1<=n<=300,w is the maximum carrying 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
The
minimum number of canoes required for each group of people.
Sample input
385 65 84 85 80 84 8390 390 45 60100 550 50 90 40 60
Sample output
533
1#include <stdio.h>2 intMain ()3 {4     ints;5scanf"%d",&s);6      while(s--)7     {8         intw,n,i,j,t,k=0;9scanf"%d%d",&w,&n);Ten         intA[n]; One          for(i=0; i<n;i++) Ascanf"%d",&a[i]); -          for(i=0; i<n-1; i++) -              for(j=i;j<n;j++) the             { -                 if(a[i]>A[j]) -                 { -t=A[i]; +a[i]=A[j]; -a[j]=T; +                 } A             } atI=0, j=n-1; -          while(i<=j) -         { -              if(A[I]+A[J]&LT;=W)/*if less than the carrying capacity*/ -             { -i++; inj--; -k++; to             } +             Else if(I==J)/*If there's only one person left, a boat alone.*/ -k++; the             Else/*other*/ *             { $j--;Panax Notoginsengk++; -             } the         } +printf"%d\n", k); A     } the}

Travel 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.