51Nod1432 独木舟(贪心)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42391248/article/details/84858135

这道题很简单,思路也很好想,因为一只船最多两个人坐,所以就看最重的人和最轻的人体重加一起是否超过载重,如果超过了,船就只有最重的人坐。

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstdio>
using namespace std;
int main()
{
	int n,m,i;
	int a[10005];
	cin>>n>>m;
	for(i=0;i<n;i++)
		cin>>a[i];
	sort(a,a+n);
	int l=0,r=n-1;
	int ans=0;
	while(l<=r)
	{
		if(a[l]+a[r]<=m)
			l++;
		ans++;
		r--;
	}
	cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_42391248/article/details/84858135