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