51NOD 1432 独木舟 贪心
2017-08-27 17:46
260 查看
1432
独木舟
基准时间限制:1 秒 空间限制:131072 KB 分值:
10
难度:2级算法题
收藏
关注
取消关注
n个人,已知每个人体重。独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人。显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟?
Input
Output
Input示例
Output示例
思路:排序,左右匹配成功则赋值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;
}
独木舟
基准时间限制: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;
}
相关文章推荐
- 51nod 1432 独木舟(贪心)
- 51nod 1432 独木舟(贪心)
- 51nod 1432 独木舟 (水题 贪心)
- 51NOD 1432 独木舟(贪心+递归)
- 【51Nod】1432 - 独木舟(贪心)
- 51Nod 1432 独木舟 贪心
- 51nod 1432 独木舟 贪心
- 51Nod 1432 独木舟(贪心)
- 51nod 1432 独木舟 贪心
- 51Nod 1432 独木舟 (贪心
- 51NOD 1432 独木舟(贪心
- 51nod 1432 独木舟(贪心)
- 积分赛 (一)——51Nod - 1432 【贪心】
- 51Nod 1432 独木舟
- [51nod] 1432 独木桥 贪心
- 贪心算法基础之独木舟问题 51nod 贪心教程
- 51nod贪心例子独木舟问题
- 51Nod - 1432 模拟 + 贪心
- 51Nod:独木舟问题(贪心)
- 51NOD 1432 贪心