51nod 1432 独木舟

1432 独木舟
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注
n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?
Input
第一行包含两个正整数n (0

#include <bits/stdc++.h>

using namespace std;
const int MAXN=1e4+5;
int peo[MAXN];
int m,n;

int main()
{
    //cout << "Hello world!" << endl;
    cin>>n>>m;
    int cnt=0;
    for(int i=0;i<n;i++)
    {
        cin>>peo[i];
    }
    sort(peo,peo+n);
    int l=0,r=n-1;
    while(l<r)
    {
        if(peo[r]+peo[l]<m)
            l++,r--,cnt++;
        else
            r--,cnt++;
    }
    if(l==r)cnt++;
    cout<<cnt<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/bryce1010/p/9386960.html