51Nod 1432 独木舟

基准时间限制:1 秒 空间限制:131072 KB 分值: 10  难度:2级算法题
 收藏
 关注
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 <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
typedef long long LL;

const int maxn = 10000 + 5;
LL weight[maxn];

int main()
{
    LL n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>weight[i];
    }
    sort(weight,weight+n);
    int cnt = 0;
    int left = 0;
    int right = n-1;
    while(left <= right)
    {
        if(weight[left] + weight[right] <= m)
        {
            left++;
            right--;
            cnt++;
        }
        else
        {
            right--;
            cnt++;
        }
    }
    cout<<cnt<<endl;
    return 0;
}


posted @ 2017-03-06 22:16  legolas007  阅读(21)  评论(0编辑  收藏  举报