题目描述
旅行社计划组织一个独木舟旅行。租用的独木舟都是一样的,最多乘两人,而且载重有一个限度。现在要节约费用,所以要尽可能地租用最少的舟。
本题的任务是读入独木舟的载重量,参加旅行的人数,每个人的体重,计算出所需要的独木舟数目。
输入
第1行是w(80<=w<=200),表示每条独木舟最大的载重量
第2行是正整数n(1<=n<=30000),表示参加旅行的人数
接下来的n行,每行是一个正整数ti(5<=ti<=w),每个人的重量
输出
输出一行一个数,表示最少的独木舟数目
样例输入
100
9
90
20
20
30
50
60
70
80
90
样例输出
6
标准答案:
1.二分,标准答案:(有13种以上的答案)
#include<bits/stdc++.h>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=1E9+7;
const int N=1000000+5;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
using namespace std; LL a[N];
int main(){
LL n,m;scanf("%lld%lld",&n,&m);
for(int i=1; i<=m;i++)scanf("%lld",&a[i]);
sort(a+1,a+m+1);int left=1,right=m,res=0;
while(left<=right) {
if(a[left]+a[right]<=n)left++,right--;
else right--;res++;
}printf("%d\n",res);
}