[贪心入门]独木舟问题
n个人,已知每个人体重,独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n,cnt=0;
long int m;
scanf("%d %ld",&n,&m);
int p[10000];
for(int i=0;i<n;i++)
scanf("%d",&p[i]);
sort(p,p+n);
int begin=0;
int last=n-1;
while(begin<=last)
{
if(p[begin]+p[last]<=m)
{
begin++;
last--;
cnt++;
}
else
{
last--;
cnt++;
}
}
printf("%d",cnt);
return 0;
}