题目
描述
进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别。一条独木舟最多只能乘坐两个人,且乘客的总重量不能超过独木舟的最大承载量。我们要尽量减少这次活动中的花销,所以要找出可以安置所有旅客的最少的独木舟条数。现在请写一个程序,读入独木舟的最大承载量、旅客数目和每位旅客的重量。根据给出的规则,计算要安置所有旅客必须的最少的独木舟条数,并输出结果。
输入
第一行输入s,表示测试数据的组数;
每组数据的第一行包括两个整数w,n,80<=w<=200,1<=n<=300,w为一条独木舟的最大承载量,n为人数;
接下来的一组数据为每个人的重量(不能大于船的承载量);
输出
每组人数所需要的最少独木舟的条数。
样例输入
3
85 6
5 84 85 80 84 83
90 3
90 45 60
100 5
50 50 90 40 60
样例输出
5
3
3
代码
#include "stdafx.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 1000;
int w, n, A[maxn], ans;
void solve(int x, int y) {
if (x <= y) {
if (A[x] + A[y] > w) {
ans++;
solve(x, y - 1);
}
else {
ans++;
solve(x + 1, y - 1);
}
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &w, &n);
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
sort(A, A + n);
ans = 0;
solve(0, n-1);
printf("%d\n", ans);
}
system("PAUSE");
return 0;
}
收获
- 这是什么OJ。。。没见过
- 贪心的思想:当前利益最大化。
- 背包之乘船问题。