51nod1432 独木舟
来源:互联网 发布:windows app文件夹 编辑:程序博客网 时间:2024/05/16 00:55
n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?Input第一行包含两个正整数n (0<n<=10000)和m (0<m<=2000000000),表示人数和独木舟的承重。接下来n行,每行一个正整数,表示每个人的体重。体重不超过1000000000,并且每个人的体重不超过m。Output一行一个整数表示最少需要的独木舟数。Input示例3 6123Output示例2按从小到大先排序,然后第一个和最后一个相加,如果结果比船的载重大,那么就把指向最后一个的指针往前移动一个,第一个不动,船数加一,然后第一个和倒数第二个相加,如果比载重小,那么指向第一个的指针往后移动一个,倒数第二个指针往前移动,船数加一,以此类推。#include <iostream>#include <algorithm>#define N 10000using namespace std;long person[N];int main(){ int per, boat = 0; long load; cin>>per>>load; for(int i = 0; i < per; i++) { cin>>person[i]; } sort(person, person + per); long *p_low = person, *p_high = person + per - 1; while(p_low <= p_high) { if(*p_low + *p_high > load) { p_high--; boat++; } else { p_low++; p_high--; boat++; } } cout<<boat<<endl; return 0;}
0 0
- 51nod1432 独木舟
- 51 Nod1432独木舟
- 水题:51Nod1432-独木舟
- 51nod1432-贪心-独木舟
- 51nod1432 贪心
- 51nod1432【贪心】
- 51nod 独木舟
- 51NOD 1432 独木舟
- 51nod 独木舟问题
- 51nod---1432 独木舟
- 51ND 1432 独木舟
- 51nod-独木舟问题
- 51nod 1432 独木舟
- 51nod-1432 独木舟
- 51Nod 1432 独木舟
- 51Nod 1432 独木舟
- 51nod独木舟问题
- 【51Nod】1432 独木舟
- C++ 指针与const的组合复习
- IOC(控制反转,反向控制)
- UVA 12664 Interesting Calculator
- ListView in fragment 列表视图的使用
- C++, ID、指针、handle (void *)的区别
- 51nod1432 独木舟
- linux下定时器函数
- BZOJ1984月下“毛景树”
- HDU 4081 Qin Shi Huang's National Road System (次小生成树算法)
- Flash as3加载外部swf和exe
- C++中头文件(.h)和源文件(.cpp)都应该写些什么
- windows异常处理 __try __except
- 配置gradle for mac
- OC中的数组、字典、集合