独木舟上的旅行

 

描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别。一条船可坐多个乘客,但乘客的总重量不能超过独木舟的最大承载量。我们要尽量减少这次活 动中的花销,所以要找出可以安置所有旅客的最少的独木舟条数。现在请写一个程序,读入独木舟的最大承载量、旅客数目和每位旅客的重量。根据给出的规则,计算要 安置所有旅客必须的最少的独木舟条数,并输出结果。
输入 第一行输入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 180 5

90 5 5 90 45

 

样例输出 5 3

2

 

代码如下:

 

[cpp] view plaincopyprint?
  1. #include<iostream>  
  2. #include<string>  
  3. #include<cstring>  
  4. #include<cstdio>  
  5. #include<algorithm>  
  6. #define CLR(arr, val) memset(arr, val, sizeof(arr))  
  7. usingnamespace std; 
  8. int res[310]; 
  9.  
  10. int main() 
  11.     //freopen("Input.txt", "r", stdin);  
  12.     int ncase, wei, num, i, count, j; 
  13.     scanf("%d", &ncase); 
  14.     while(ncase--) 
  15.     { 
  16.         CLR(res, 0); 
  17.         scanf("%d%d", &wei, &num); 
  18.         for(i = 0; i < num; ++i) 
  19.             scanf("%d", &res[i]); 
  20.         sort(res, res + num); //按重量排序  
  21.         count = 0; 
  22.         for(i = num - 1; i >= 0; --i) 
  23.         { 
  24.             if(!res[i]) continue; //安排过则遍历下一个  
  25.             for(j = i - 1; j >= 0; --j) 
  26.                 if(res[i] + res[j] <= wei) 
  27.                 { 
  28.                     res[i] += res[j]; res[j] = 0; //累加后清零  
  29.                 } 
  30.             count++; 
  31.         } 
  32.         printf("%d\n", count); 
  33.     } 
  34.     return 0; 
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define CLR(arr, val) memset(arr, val, sizeof(arr))
using namespace std;
int res[310];

int main()
{
	//freopen("Input.txt", "r", stdin);
	int ncase, wei, num, i, count, j;
	scanf("%d", &ncase);
	while(ncase--)
	{
		CLR(res, 0);
		scanf("%d%d", &wei, &num);
		for(i = 0; i < num; ++i)
			scanf("%d", &res[i]);
		sort(res, res + num); //按重量排序
		count = 0;
		for(i = num - 1; i >= 0; --i)
		{
			if(!res[i]) continue; //安排过则遍历下一个
			for(j = i - 1; j >= 0; --j)
				if(res[i] + res[j] <= wei)
				{
					res[i] += res[j]; res[j] = 0; //累加后清零
				}
			count++;
		}
		printf("%d\n", count);
	}
	return 0;
}
posted @ 2012-11-10 14:16  燕笑语兮  阅读(192)  评论(0编辑  收藏  举报