n Individuals, known to each body weight, canoe load-bearing fixed, each canoe up to two people, can sit a person or two people. It is clear that the total weight does not exceed the load of the canoe, assuming that each person weighs less than the canoe load, how many canoes do I need at least?
Analysis:
an obvious strategy is to sort by the weight of a person.
Extreme Greed Strategy, the heaviest person to get on board-if the heaviest person and the lightest person weight sum does not exceed the load of the ship, then they occupy a boat. Otherwise (because assuming the heaviest person's weight does not exceed the load of the ship), the heaviest person occupies a single boat. Change to a (n–1) or (n–2) problem. The key is that this greedy strategy is correct. We can prove that the optimal solution can also be changed into this strategy.
(1) Assuming that the heaviest person and the lightest person weigh and exceed the weight of the ship, then the optimal solution is obviously the heaviest person to occupy a single boat, so the optimal and greedy strategies are the same in this case. (2) Assume the heaviest person and the lightest person's weight and do not exceed the load bearing of the ship.
(2.1) If the best solution, the heaviest person occupies a boat alone, you can put the lightest person also put up, so that the optimal solution of the number of ships does not increase. If the lightest person occupies a boat, we can put the heaviest man up, and the optimal number of ships will not increase.
(2.2) If the heaviest person in the optimal solution x and X ' Occupy a ship (x, X '), while the lightest man y and y ' occupy a boat (Y, y ')
Let's change (x, y) (x ', y ')
(x, y) clearly does not exceed the weight of the ship-because we assume so. Key look (x ', Y ').
x ' + y ' <= x ' + x because (x ', x) is not overweight, so (x ', Y ') is also legal. Therefore, the optimal number of ships will not increase.
so we can prove that if we could put the heaviest person and the lightest person on a boat, the optimal solution would not be affected.
by applying this strategy over and over again, you can reduce the size of N to (n–1) or (n–2) individuals to solve this problem.
input
The first line contains two positive integers n (0<n<=10000) and M (0<m<=2000000000), representing the number of people and the weight of the canoe. The next n lines, one positive integer per line, represent the weight of each person. Weigh no more than 1000000000, and each person's weight does not exceed M.
Output
An integer line indicates the minimum number of canoes required.
Input Example
3 6123
Sample Output
2
Please choose your familiar language, and in the following code box to complete your program, note the data range, the end result will cause Int32 overflow, this will output the wrong answer. See the following language descriptions for how input and output are handled by different languages.
#include <iostream>#include<cstdio>#include<algorithm>#include<queue>#include<vector>using namespacestd; Longpeople[10000]; intMain () {intn,i,j,ans=0; Longweight; CIN>>n;//this represents the number of peoplecin>>weight;//This means the weight of the ship. for(i=0; i<n;i++) cin>>People[i]; Sort (People,people+N); for(i=0, j = N1; i<=j;j--){ //traverse from top down if(people[i]+people[j]>weight) {ans++; }Else{i++; Ans++; }} printf ("%d", ans); return 0; }
If it helps you, do not forget to add praise Oh, the clatter!! See you next time! the
Introduction to Greedy basics four--canoe problem