[51nod] 1432 独木桥 贪心

n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?
Input
第一行包含两个正整数n (0<n<=10000)和m (0<m<=2000000000),表示人数和独木舟的承重。
接下来n行,每行一个正整数,表示每个人的体重。体重不超过1000000000,并且每个人的体重不超过m。
Output
一行一个整数表示最少需要的独木舟数。
Input示例
3 6
1
2
3
Output示例
2

贪心策略:每次最轻的人应该和能和他坐一起的人中最重的那一个坐一起,如果没有的话,只能单独做一个
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
LL n, m, ans;
LL w[10010];


int main()
{
    //freopen("1.txt", "r", stdin);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> w[i];

    sort(w, w+n);
    int L = 0, R = n-1;
    while (L <= R) {
        if (w[L] + w[R] <= m)
            L++;
        R--;
        ans++;
    }
    cout << ans;

    return 0;
}

 

 
posted @ 2017-07-22 17:26  whileskies  阅读(151)  评论(0编辑  收藏  举报