[Tips and notes 71] travel on zhuqiao

Source: Internet
Author: User
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.

#include <stdio.h>#include <stdlib.h>#define MAXSIZE 300+5int main(){int Cases;    scanf("%d",&Cases);    while(Cases--)    {int arr[MAXSIZE];        int i,j;        int coat_Weight,n;        scanf("%d %d",&coat_Weight,&n);        for(i=0;i<n;i++)        {scanf("%d",&arr[i]);        }        for(i=0;i<n;i++)for(j=i+1;j<n;j++)            {if(arr[i]>arr[j])                {int temp=arr[i];                    arr[i]=arr[j];                    arr[j]=temp;                }                        }int head=0;        int end=n-1;        int sum=0;        while(head<=end)        {if(arr[head]+arr[end]>coat_Weight)            {end--;                sum++;                continue;            }else if(arr[head]+arr[end]<=coat_Weight)            {end--;                head++;                sum++;            }        }        printf("%d\n",sum);    }return 0;}


[Tips and notes 71] travel on zhuqiao

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.