转载于:http://blog.chinaunix.net/uid-22263887-id-1778908.html

Painter

Description

The local toy store sells small fingerpainting kits with between three and twelve 50ml bottles of paint, each a different color. The paints are bright and fun to work with, and have the useful property that if you mix X ml each of any three different colors, you get X ml of gray. (The paints are thick and “airy”, almost like cake frosting, and when you mix them together the volume doesn’t increase, the paint just gets more dense.) None of the individual colors are gray; the only way to get gray is by mixing exactly three distinct colors, but it doesn’t matter which three. Your friend Emily is an elementary school teacher and every Friday she does a fingerpainting project with her class. Given the number of different colors needed, the amount of each color, and the amount of gray, your job is to calculate the number of kits needed for her class.
Input

The input consists of one or more test cases, followed by a line containing only zero that signals the end of the input. Each test case consists of a single line of five or more integers, which are separated by a space. The first integer N is the number of different colors (3 <= N <= 12). Following that are N different nonnegative integers, each at most 1,000, that specify the amount of each color needed. Last is a nonnegative integer G <= 1,000 that specifies the amount of gray needed. All quantities are in ml.
Output

For each test case, output the smallest number of fingerpainting kits sufficient to provide the required amounts of all the colors and gray. Note that all grays are considered equal, so in order to find the minimum number of kits for a test case you may need to make grays using different combinations of three distinct colors.
Sample Input

3 40 95 21 0
7 25 60 400 250 0 60 0 500
4 90 95 75 95 10
4 90 95 75 95 11
5 0 0 0 0 0 333
0

Sample Output

2
8
2
3
4

解题思路
题意:

   给出需要几种颜料,和每种需要的数量,和灰色颜料需要的数量。要求的是至少需要买几套颜料才能满足要求。每套都包含所有需要的颜料,每种50ml,没有灰色颜料,灰色的需要用其他任意三种配成,而且体积不会增加,如用三种颜料,每种都是Xml,那么所配成的灰色颜料也是Xml。

思路:

   用贪心,先找到需要的最多的那种颜料量max(灰色除外),算出不算灰色时所需要count套颜料。然后用count*50减去每种需要的颜料,就是除了各自所需要的外,还剩多少可以去配灰色。用qsort按从大到小排序。一个循环,看第三种颜料是不是等于0,而且灰色颜料是不是不等于0.如果是的话,说明不够配灰色了,每种要加50ml,count++。然后灰色颜料数减一,如果等于0了,说明灰色配够了,退出。否则前三种颜料数减一,再把所有从小到大排序。再循环回去。我本来不是一个一个减的,就像 0 0 0 0 0 333 这个是50 50 50 50 50 把前三种都减50,变成0 0 0 50 50,这样比较快,但显然不是最优的,可是又想不到其他方法,后来看到网上一篇解题报告正好讲到这个问题。然后才解决了。

c++ AC 代码

#include<cstdio>
#include<cstdlib>
#define N 13int cmp(const void *p,const void *q)
{return *(int *)p > *(int *)q ? -1 : 1;
}int main()
{int n,grey,count,m;while(~scanf("%d",&n) && n){m = 0;int colors[N];for (int i = 0; i < n; i++){scanf("%d",colors+i);if(colors[i] > m)m = colors[i];}scanf("%d",&grey);if(m % 50)count = m / 50 +1;elsecount = m / 50;for(int i=0;i<n;i++)colors[i] = count*50 - colors[i];qsort(colors, n, sizeof(colors[0]), cmp);while(1){if(colors[2] == 0 && grey > 0){count++;for (int i = 0; i < n; i++)colors[i] += 50;}grey--;if(grey <= 0)break;else{colors[0]--; colors[1]--; colors[2]--;qsort(colors, n,sizeof(colors[0]),cmp);}}printf("%d\n",count);}return 0;
}

qsort:
头文件:#include<cstdlib>
原型:
void qsort(void *base, int nelem, unsigned int width, int ( * pfCompare)( const void *, const void *));
参数分别是数组指针,元素个数,每个元素大小(sizeof),比较函数
比较函数自己定义,如 int compare(const void a,const void b)
 
若a排在b前,返回负值
 
a排在b前后都行,返回0
 
a排在b后面,返回正值

poj 2709 Painter——贪心 买颜料问题相关推荐

  1. POJ 2709 Painter 【贪心算法】

    [原题链接] http://acm.pku.edu.cn/JudgeOnline/problem?id=2709 [题目大意] 要配置出n(3<=n<=12)种颜色的颜料,并配置出一定量的 ...

  2. POJ 2709 Painter

    题意: 一个商店里有许多颜料盒,但是每个颜料盒里面没有 grey 这种颜色,而这种颜色可以由任意其他三种颜色配成,每个颜料盒里一开始有 50ml 的各种非 grey 颜色, 告诉了 每种颜色和grey ...

  3. 贪心,POJ(2709)

    题目链接:http://poj.org/problem?id=2709 解题报告: #include <stdio.h> #include <algorithm> #inclu ...

  4. poj 1456 Supermarket 贪心+并查集(个人感觉有点难判断出来

    poj 1456 这第一眼还觉得只要贪心就可以了,但是emmm看了大佬的题解居然真的要用到并查集= = 大佬清晰的思路 大佬舒服的代码 #pragma warning(disable:4996) #i ...

  5. Poj 圣诞老人的礼物 贪心

    圣诞节?快乐^ - ^ POJ 4110 圣诞老人的礼物(贪心) 描述 圣诞节来临了,在城市A中圣诞老人准备分发糖果,现在有多箱不同的糖果,每箱糖果有自己的价值和重量,每箱糖果都可以拆分成任意散装组合 ...

  6. poj 1456 Supermarket (贪心, 并查集)

    链接: http://poj.org/problem?id=1456 题目: Description A supermarket has a set Prod of products on sale. ...

  7. poj 1681 Painter#39;s Problem(高斯消元)

    http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. 注意依据自由变元求其它解及求最优值 ...

  8. poj 1230(贪心)

    解题思路:这道题目是用贪心的思想,从左向右扫描场地的每一列是否合法.若不合法,贪心的找出从该列起向右延伸最长的m道墙,移除这m道墙使得该列合法. 我最开始代码会出现这样的问题:如果两个墙是连在一起的, ...

  9. POJ - 3614 Sunscreen(贪心/二分图最大匹配-多重匹配/网络流-最大流)

    题目链接:点击查看 题目大意:给出n头奶牛,奶牛们现在要晒太阳,每头奶牛需要[l,r]区间内的光照强度,现在有m种防晒霜,每种防晒霜可以让奶牛接受到val数值的光照强度,然后每种防晒霜只有num个,现 ...

最新文章

  1. java_web学习(8)会话与状态管
  2. java重写6,java重写equals()方法和hashCode()方法
  3. 【长沙集训】2017.10.10
  4. c++中vector使用的小问题
  5. 【每日SQL打卡】DAY 1丨部门工资最高的员工【难度中等】
  6. php 上一条下一条,thinkPhp里添加显示上一条和下一条
  7. 用计算机弹正义之道,正义之道
  8. 【今日CS 视觉论文速览】Fri, 1 Feb 2019
  9. linux查看网卡的驱动命令行,linux查看网卡驱动模块信息
  10. 讲座记录《捷联惯导解算的历史及发展》
  11. 部分格式文件解释以及万能文件查看器下载
  12. 『中安网培』***游戏过关攻略
  13. Envi处理MODIS流程
  14. Airtest IDE 连接 夜游神模拟器 自动化UI测试
  15. 公众号获取永久图片media_id
  16. C语言入门(初识C语言)
  17. 知识图谱:Konwledge Graph简介
  18. Linux下开启openmp编译,OpenMP程序的编译和运行
  19. web前端大作业--响应式风景旅游网页设计(国庆旅游主题-HTML+CSS+JavaScript)实现
  20. js选择文件进行导入(FileSaver.js)

热门文章

  1. C++核心准则C.150:unique_ptr管理的对象要用make_unique()​构建
  2. 如何设计一个API接口
  3. struts注解 配置拦截器 拦截器无效
  4. Mac音效增强软件:Boom 3D
  5. 揭秘宜信财富年度账单的技术实现
  6. 数据分析行业中的数据运营是怎么一回事?
  7. matlab联立两个方程组求解,MATLAB求解联立方程的问题
  8. vue展示信息卡片_vue 个人中心会员卡片组件
  9. 长期投资的信心来自哪里(兼谈安全边际)
  10. gitblit Git服务器