51Nod Canoe (greedy)

n people, each of whom has a known weight. The canoe has a fixed load-bearing capacity, and each canoe can sit at most two people, and one or two people can sit. Obviously, the total weight is required not to exceed the load capacity of the canoes. Assuming that the weight of each person does not exceed the load capacity of the canoes, how many canoes are required at least?

Input
The first line contains two positive integers n (0<n<=10000) and m (0<m<=2000000000), which represent the number of people and the load-bearing capacity of the canoe.
The next n lines, each with a positive integer, represent the weight of each person. The weight does not exceed 1000000000, and the weight of each person does not exceed m.
Output
One integer per line indicates the minimum number of canoes required.
Input example
3 6
1
2
3
Output example
2
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
int n,m;
int a[10006];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    int l=0,r=n-1;
    int ans=0;
    while(l<=r)
    {
        if(a[l]+a[r]<=m) l++;
        r--;
        years ++ ;
    }
    printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325172159&siteId=291194637