https://www.51nod.com/Challenge/Problem.html#!#problemId=1432
n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?
输入
第一行包含两个正整数n (0<n<=10000)和m (0<m<=2000000000),表示人数和独木舟的承重。
接下来n行,每行一个正整数,表示每个人的体重。体重不超过1000000000,并且每个人的体重不超过m。
输出
一行一个整数表示最少需要的独木舟数。
输入样例
3 6
1
2
3
输出样例
2
从两边开始取,如果可以就拿否则下一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <queue> #include <set> #include <utility> #include <vector> #include <map> #include <stack> using namespace std; #define MAX 10000+10 int n,m,a[MAX],ans; int main(){
scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); int f=0,e=n-1; while(f<e){ if(a[f]+a[e]<=m){ f++;e--; } else { e--; } ans++; } if(f==e)ans++; printf("%d",ans); return 0; }
|