您的位置:首页 > 其它

POJ 1017 Packets 【贪心 模拟】

2015-12-15 22:11 316 查看
Packets

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 48497Accepted: 16428
Description

A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because
of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels
necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.
Input

The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1*1 to the biggest
size 6*6. The end of the input file is indicated by the line containing six zeros.
Output

The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last
``null'' line of the input file.
Sample Input
0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0

Sample Output
2
1


恩,题目大意就是说,同等高度的1*1到6*6的包裹,然后给出对应的个数,求所用的最少的包裹个数,即可以将许多小的放到一个大的里面。虽然是贪心,但不会做,只能如此了。。。代码不怎么好,后面有测试数据

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[10];
int rec[10];
int main()
{
int t,sum,cnt;
while(1)
{
sum=cnt=0;
for(int i=1;i<=6;++i)
{
scanf("%d",&a[i]);
sum+=a[i];
rec[i]=0;
}
if(sum==0)
return 0;
for(int i=6;i>=4;i--)
{
rec[i]=a[i]*(36-i*i);
cnt+=a[i];
}
if(a[3]%4)
{
rec[3]=36-(a[3]%4)*9;
cnt=cnt+a[3]/4+1;
}
else
cnt+=a[3]/4;
if(a[4]&&a[2])
{
if(a[4]*5>=a[2])
{
rec[4]=(a[4]*5-a[2])*4;
a[2]=0;
}
else
{
rec[4]=0;
a[2]-=a[4]*5;
}
}
if(rec[3]&&a[2])
{
int num=rec[3]/4-1;
if(a[2]<=num)
{
a[2]=0;
rec[3]=rec[3]-a[2]*4;
}
else
{
a[2]-=num;
rec[3]-=num*4;
}
}
if(a[2]%9)
{
cnt+=a[2]/9+1;
rec[2]=36-(a[2]%9)*4;
}
else
cnt+=a[2]/9;
if(a[1])
{
int temp=0;
for(int i=2;i<=5;++i)
temp+=rec[i];
if(temp<a[1])
{
a[1]=a[1]-temp;
if(a[1]%36)
cnt=cnt+a[1]/36+1;
else
cnt+=a[1]/36;
}
}
printf("%d\n",cnt);
}
return 0;
}


Input:
0 0 4 0 0 1
7 5 1 0 0 0
36 9 4 1 1 1
0 9 4 1 1 0
0 0 4 0 0 0
36 0 0 0 0 0
0 9 0 0 0 0
79 96 94 30 18 14
53 17 12 98 76 54
83 44 47 42 80 3
15 26 13 29 42 40
41 61 36 90 54 66
78 56 445 45 23 65
13 4 8 29 45 3
15 75 45 98 34 53
40 9 0 2 0 0
41 9 0 2 0 0
44 0 0 0 4 0
0 2 3 0 0 0
37 7 2 0 1 0
12 2 0 1 0 0
13 2 0 1 0 0
0 0 0 0 0 0

Output:
2
1
6
4
1
1
1
86
231
137
115
219
245
79
197
3
4
4
2
3
1
2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: