您的位置:首页 > 其它

51NOD 1432 独木舟 贪心

2017-08-27 17:46 260 查看
1432
独木舟


基准时间限制:1 秒 空间限制:131072 KB 分值:
10
难度:2级算法题


收藏


关注


取消关注

n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?

Input

第一行包含两个正整数n (0<n<=10000)和m (0<m<=2000000000),表示人数和独木舟的承重。
接下来n行,每行一个正整数,表示每个人的体重。体重不超过1000000000,并且每个人的体重不超过m。


Output

一行一个整数表示最少需要的独木舟数。


Input示例

3 6
1
2
3


Output示例

2


思路:排序,左右匹配成功则赋值0,最后遍历不是0的就单独一个船。
Code:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int AX = 1e4+666;
LL a[AX];
int main(){
int n,w;
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> w;
for( int i = 0 ; i < n ; i++ ){
cin >> a[i];
}
sort(a,a+n);
int ans = n ;
int res = 0 ;
int tot = 0 ;
for( int i = n-1 ; i > tot ; i-- ){
if( a[i] + a[tot] <= w ){
a[i] = 0;
a[tot] = 0;
tot++;
res++;
}
}
for( int i = 0 ; i < n ; i++ ){
if( a[i] ) res ++;
}
cout << res << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: