题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1432
题目:
n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?
Input
第一行包含两个正整数n (0<n<=10000)和m (0<m<=2000000000),表示人数和独木舟的承重。 接下来n行,每行一个正整数,表示每个人的体重。体重不超过1000000000,并且每个人的体重不超过m。
Output
一行一个整数表示最少需要的独木舟数。
排序,贪心。
#include <iostream>
#include<bits/stdc++.h>
#define N 11000
using namespace std;
int a[N],b[N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
int top=0,ans=0;
for(int i=n-1;i>=0;i--)
{
if(top&&b[top]>=a[i])
top--;
else
{
ans++;
b[++top]=m-a[i];
}
}
cout<<ans<<endl;
}