Travel time limits on canoes: MS | Memory limit: 65535 KB Difficulty: 2
-
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
Code:
Package Acm71;import Java.util.arrays;import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner input = new Scanner (system.in); int group = Input.nextint (); for (int i = 0;i<group;i++) {int weight = Input.nex TInt (); int number = Input.nextint (); int array[] = new Int[number];for (int j = 0;j<array.length;j++) {Array[j] = input. Nextint ();} int people = solution (Array,weight,number); System.out.println (People);}} private static int solution (int[] array,int weight,int number) {//Sort Arrays.sort (array);//1: First judge inside int result = 0;int i = 0; Int J = Number-1;while (i <= j) {if (Array[i] + array[j] > weight) {j--;result++;} else{i++;j--;result++;}} return result;}}
Travel on a canoe