51nod_learn_greedy_ Canoe Problem

Source: Internet
Author: User

n individuals, who are known to weigh each person. The canoe is load-bearing, with a maximum of two persons per canoe, and one person or two persons can be seated. 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?

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
The greedy strategy is ... Make as many boats as possible to carry two people ... We can sort the first ... Then take it from both sides. If the current largest can and the smallest go together, go together can not go on their own ... And then until everyone else is done.
1 /*************************************************************************2 > File name:code/51nod/learn/greedy/3.cpp3 > Author:111qqz4 > Email: [Email protected]5 > Created time:2015 October 05 Monday 19:42 15 seconds6  ************************************************************************/7 8#include <iostream>9#include <iomanip>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <map> -#include <Set> -#include <queue> -#include <vector> +#include <stack> -#include <cctype> +                   A #defineYn hez111qqz at #defineJ1 CUTE111QQZ - #defineMS (A,X) memset (A,x,sizeof (a)) - using namespacestd; - Const intdx4[4]={1,0,0,-1}; - Const intdy4[4]={0,-1,1,0}; -typedefLong LongLL; intypedefDoubleDB; - Const intINF =0x3f3f3f3f; to Const intn=1e4+5; + LL A[n]; - intans; the intN; * LL m; $ intMain ()Panax Notoginseng { -  //#ifndef Online_judge the  //freopen ("In.txt", "R", stdin); + //#endif A  thescanf"%d%lld",&n,&m); +     for(inti =0; I < n; i++) scanf ("%lld",&a[i]); -Sort (a,a+n); $    inti =0; $    intj = N1; -    intCNT =0; -    intAns =0 ; the     -     while(cnt<n&&i<=j)Wuyi    { the        if(i==j) -        { Wucnt++; -ans++; About         Break; $        } -      -        if(a[i]+a[j]<=m) -        { Ai++; +j--; theCNT = cnt +2; -ans++; $        } the        Else the        { thej--; theCNT = cnt +1; -ans++; in        } the    } theprintf"%d\n", ans); About    the     the //#ifndef Online_judge the //fclose (stdin); + //#endif -     return 0; the}
View Code

51nod_learn_greedy_ Canoe Problem

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.