Painter
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5621   Accepted: 3228

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

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

Sample Output

  1. 2
  2. 8
  3. 2
  4. 3
  5. 4

Source

  思路:

给出几种颜色需求的ml量,然后最后一个数是灰色需求量,灰色可以由任何三中不同颜色的颜色组成,每个颜料盒有所给出的颜色的炎凉50ml

问最少给出几个颜料盒,可以组成所需求颜色

显然贪心可以解决,先求出满足的普通色所需的最小盒数,然后把剩余颜料从大到小排列,那前三种每个取出1ml组成1

ml的灰色,在这里本来我是把选出三个颜色中,直接选取第三个(最小容量)的所有容量k,组成kml的灰色,后来发现不行,贪心必须每次都要要求最好

所以每次1ml来选才能达到要求

代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #define N 10000
  6. using namespace std;
  7. int n,col[N],mx=-1,gray,cnt;
  8. bool cmp(int a,int b){return a>b;}
  9. int main()
  10. {
  11. while(scanf("%d",&n) == 1 && n != 0)
  12. {
  13. mx=-1,cnt=0;
  14. for(int i=1;i<=n;i++)
  15. {
  16. scanf("%d",&col[i]);
  17. mx=max(mx,col[i]);
  18. }
  19. scanf("%d",&gray);
  20. cnt+=(mx/50);
  21. if(mx%50!=0)cnt++;
  22. for(int i=1;i<=n;i++)col[i]=cnt*50-col[i];
  23. while(gray>0)
  24. {
  25.  
  26. sort(col+1,col+n+1,cmp);
  27. if(col[3]>0)
  28. {
  29. gray--;
  30. col[1]--;
  31. col[2]--;
  32. col[3]--;
  33. }
  34. else
  35. {
  36. cnt++;
  37. for(int i=1;i<=n;i++)col[i]+=50;
  38. }
  39. }
  40. printf("%d\n",cnt);
  41. }
  42. }

【poj2709】Painter--贪心的更多相关文章

  1. POJ2709 染料贪心

    题意:       要搭配出来n种颜料,每种颜料要用mi升,除了这n种颜色还有一个合成灰色的毫升数,灰色是由三种不同的颜色合成的,三种m m m 的不同颜色能合成m升灰色,然后问你满足要求至少要多少盒 ...

  2. poj2709 贪心基础

    D - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...

  3. poj 1681 Painter's Problem

    Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...

  4. POJ 2706 Painter

    Painter Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3157   Accepted: 1962 Descripti ...

  5. poj_2709 贪心算法

    poj 2709 painter 题目要求 给定涂料,每套涂料含有3-12种不同的颜色(开始时候给定选用的颜料套的颜色数目),且一套涂料中每种颜色均有50ml.且一套涂料中的任意三种不同的颜色各X m ...

  6. [SinGuLaRiTy] 贪心题目复习

    [SinGuLaRiTy-1024] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [POJ 2709] 颜料 (Painter) 题目描述 ...

  7. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  8. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  9. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. C# EF添加ADO.NET实体数据模型时,产生.Desiger.cs文件为空

    // T4 code generation is enabled for model 'D:\DKX4003\work\VWFC_CCS\SrcCCG-branch\CCGSPBOCOne-FCA\C ...

  2. (三)自定义Realm

    一.Realm概念 Realm:域,Shiro从从Realm获取安全数据(如用户.角色.权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用 ...

  3. 洛谷 P1540 机器翻译

    链接:https://www.luogu.org/problemnew/show/p1540 题目: 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译 ...

  4. centos源码安装nginx

    1.安装依赖 nginx对以下工具包有依赖,我们可以一键安装,命令: yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-dev ...

  5. restTemplate源码解析(三)创建ClientHttpRequest请求对象

    所有文章 https://www.cnblogs.com/lay2017/p/11740855.html 正文 上一篇文章中,我们大体看了一下restTemplate的核心逻辑.再回顾一下核心代码 p ...

  6. export default和export的使用方式

    在node中使用 var 名称=require('模块标识符') 来导入 module.exports 和exports 来暴露成员 在ES6中,也通过规范的形式,规定了ES6中如何导入和导出模块 E ...

  7. CA、证书及openssl用法

    CA和证书 摘要:涉及到网络安全这一块,想必大家都听过CA吧.像百度.淘宝.京东等这些知名网站,每年都要花费一笔money来买CA证书.但其实简单的企业内的CA认证,我们自己就可以实现,今天我就讲解一 ...

  8. python使得文件不包含重复行

    set函数去重 # -*- coding:utf-8 -*- srcTxt=open('1.txt','r').readlines() noRepeat=open('2.txt','w') st=se ...

  9. Exams(二分

    题意:给你每天要考的科目,和每门科目需要复习多长时间,问最少需要几天才能完成所有的考试. 思路:二分答案,然后判断答案是否可行,这边需要进行贪心,即倒着往前推, 比如第i天,那么前面有i-1天是,可供 ...

  10. 从c到c++<四>

    总结一下:内联函数实际上就是用inline修饰的函数,这些函数会在编译时由编译器来将代码展开,而不用像上面第二点提到的人工展开,它的使用场景:代码很短.使用频率高. 具体代码如下: 对于这两者实际上还 ...