【贪心算法】POJ-1017
一、题目
Description
A factory produces products packed in square packets of the same height h and of the sizes 11, 22, 33, 44, 55, 66. 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 11 to the biggest size 66. 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、2 * 2...6 * 6的产品各a[i]个,包装袋大小固定为6 * 6,问最少最要多少个包装袋,可以把每个订单中所有产品包装起来。
- 这个题目有点类似硬币问题,在选择时从最大的6 * 6的产品开始依次往小进行计算。对于6 * 6、5 * 5、4 * 4的产品,各需要包装袋a[6]、a[5]、a[4]个,其中放置5 * 5产品的包装袋可以额外装11个1 * 1的产品,放置4 * 4产品的可以额外装5个2 * 2的产品;对于3 * 3的产品比较特殊,对4取模后,根据余数0、1、2、3分四种情况进行判断,每次选择时尽可能得装入更多的2 * 2的产品再装入1 * 1的产品;对于2 * 2和1 * 1的产品判断较为简单,不再多说。
- 在做这题时刚开始有点看不懂题意,便看了下discuss区,发现所有人都说这题非常非常难以及细节很多,导致不敢轻易下手。但是思路想清,WA了一两次之后,便AC了,好像也没想象中的那么难。
- 在网上观摩到大神的极短代码,算法思想非常精辟,特另附上,以供学习。
三、代码
我的渣解法:
#include<cstdio>
int a[7];
int ans;
void solve() {
ans += (a[4] + a[5] + a[6]);
a[1] -= a[5] * 11;
a[2] -= a[4] * 5;
if (a[2] < 0) {
a[1] += a[2] * 4;
}
ans += (a[3] / 4 + 1);
switch (a[3] % 4) {
case 0: {
ans --;
break;
}
case 1: {
if (a[2] > 0) {
a[2] -= 5;
if (a[2] < 0) {
a[1] += a[2] * 4;
}
a[1] -= 7;
} else {
a[1] -= 27;
}
break;
}
case 2: {
if (a[2] > 0) {
a[2] -= 3;
if (a[2] < 0) {
a[1] += a[2] * 4;
}
a[1] -= 6;
} else {
a[1] -= 18;
}
break;
}
case 3: {
if (a[2] > 0) {
a[2] -= 1;
a[1] -= 5;
} else {
a[1] -= 9;
}
break;
}
}
if (a[2] > 0) {
ans += a[2] / 9;
if (a[2] % 9 > 0) {
ans ++;
a[1] -= (9 - a[2] % 9) * 4;
}
}
if (a[1] > 0) {
ans += (a[1] + 35) /36;
}
printf("%d\n", ans);
}
int main () {
while (1) {
ans = 0;
for (int i = 1; i <= 6; i ++) {
scanf("%d", &a[i]);
}
if (!a[1] && !a[2] && !a[3] && !a[4] && !a[5] && !a[6]) break;
solve();
}
return 0;
}
大神的精辟解法:
#include<stdio.h>
int main()
{
int n,a,b,c,d,e,f,x,y;
int u[4]={0,5,3,1};
while(1)
{
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0)
break;
n=d+e+f+(c+3)/4;
y=5*d+u[c%4];//在已有n个的情况下,能装下y个2*2的
if(b>y)
n+=(b-y+8)/9;//把多的2*2的弄进来
x=36*n-36*f-25*e-16*d-9*c-4*b;
if(a>x)
n+=(a-x+35)/36;//把1*1的弄进来
printf("%d\n",n);
}
return 0;
}
【贪心算法】POJ-1017的更多相关文章
- POJ 1017 Packets【贪心】
POJ 1017 题意: 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. 这些产品通常 ...
- poj 1088 滑雪(贪心算法)
思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...
- POJ 2287 田忌赛马 贪心算法
田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...
- poj_1042 贪心算法
poj 1042 gone fishing 题目要求: 由有n个湖, 按照顺序排列,一个人从第一个湖向最后一个湖行进(方向只能从湖0到湖n-1),途中可以在湖中钓鱼.在每个湖中钓鱼时,开始的5分钟内可 ...
- poj_2709 贪心算法
poj 2709 painter 题目要求 给定涂料,每套涂料含有3-12种不同的颜色(开始时候给定选用的颜料套的颜色数目),且一套涂料中每种颜色均有50ml.且一套涂料中的任意三种不同的颜色各X m ...
- ACM 贪心算法总结
贪心算法的本质: 就是当前状态的最优解,它并不考虑全局. 什么是当前状态的最优解? 成本问题? https://www.cnblogs.com/xuxiaojin/p/9400892.html (po ...
- 贪心算法(Greedy Algorithm)
参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...
- 算法导论----贪心算法,删除k个数,使剩下的数字最小
先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- ACM_ICPC hdu-2111(简单贪心算法)
一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...
随机推荐
- Linux服务-http
目录 1. httpd简介 2. httpd版本 2.1 httpd的特性 2.2 httpd-2.4新增的模块 3. httpd基础 3.1 httpd自带的工具程序 3.2 rpm包安装的http ...
- rails 中http请求发生access-control-allow-origin错误
在api项目中 本地项目无法访问服务器api 百度了下,查出原因 接着找到rails项目的解决方法,安装rack-cors这个gem包 具体方法如下: Gemfile中加入 gem 'rack-cor ...
- windows下搭建permeate漏洞测试系统实战
最近一直在搭建漏洞测试环境练习. 在此期间遇到很多问题,但是通过学习都一一解决.通过写此文来记录遇到的问题和解决方法. 首先,在github上看到了一个不错的permeate渗透测试系统.于是想搭建拿 ...
- Go语言中的常量
1 概述 常量,一经定义不可更改的量.功能角度看,当出现不需要被更改的数据时,应该使用常量进行存储,例如圆周率.从语法的角度看,使用常量可以保证数据,在整个运行期间内,不会被更改.例如当前处理器的架构 ...
- PCIE_DMA实例一:xapp1052详细使用说明
一:前言 很多和我一样初学pcie的硬件工程师都会遇到这样一个问题,看了不少pcie相关的资料,还是搞不清这玩意儿到底该怎么用.于是我们打开ISE的core_generator工具,生成了一个pcie ...
- 补交课下测试(ch12并发编程) 08.第八周
有关线程图,下面说法正确的是() A .图的原点表示没有任何线程完成一条指令的初始状态 B . 向右向上是合法的转换 C .向左向下是合法的转换 D .对角线是合法的转换 E .一个程序执行的历史被模 ...
- idea 和 WebStorm 配置 http代理 并更换主题
proxy,http,socks5 当前 idea 主题为:(idea 自带) idea 编辑器的主题颜色字体为:(网上下载的 jar 包) 因为今天在安装下面这个主题时需要在 idea的 plugi ...
- 关于SDK-manager中我们需要下载哪些?
废话少说,直接看图说话…… 图片取自博客文章——链接跳转:点击跳转
- Python中的注释
1.1 注释的目的 通过用自己熟悉的语言,在程序中对某些代码进行标注说明,这就是注释的作用,能够大大增强程序的可读性. 1.2 注释的分类 1.2.1 单行注释 以#开头,#右边的所有东西当做说明,而 ...
- Qt-网易云音乐界面实现-7 消息中心实现,主要是QListWidget 自定义Item 和QTabwidget使用
最近写的有点烦躁, 感觉内容真的很多!很多!很多. 目前真的想知道网易官方在出这款产品是,用了多少人和多长时间. 今天写的这个消息中心,有点糙,只是原理实现了没有完全复制过来,心里有团火,不想写了. ...