您的位置:首页 > 其它

UVA 1149 Bin Packing

2016-07-23 16:09 344 查看

UVA-1149

题意:给出n个物品和箱子长度 l,每个箱子最多装2个,求要用几个箱子装完这些东西。

解题思路:先排序,然后从大的一边取,取到不能取了,换小的一边取取。注意控制次数,一共就取2个。

/*************************************************************************
> File Name: 0722d.cpp
> Author: Narsh
>
> Created Time: 2016年07月22日 星期五 19时09分18秒
************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int s[101000],n,T,l,num;
bool cmp(int a,int b) {
return a<b;
}
int main() {
scanf("%d",&T);
while (T--) {
scanf("%d%d",&n,&l);
num=0;
for (int i = 1; i <= n; i++)
scanf("%d",&s[i]);
sort(s+1,s+1+n);
int h=1,t=n;
while (h<=t) {
num++;
int leave=l,times=0;;
while (leave >= s[t] && h <= t && times < 2) {
leave-=s[t];
times++;
t--;
}
while (leave >= s[h] && h <= t && times < 2) {
leave-=s[h];
times++;
h++;
}
}
printf("%d\n",num);
if (T) printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: