【模拟】【贪心】POJ2709Painter
題目
Painter
Time Limit: 1000MS Memory Limit: 65536K
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
題目大意
一個玩具店賣顏料,每套顏料有N(3≤N≤12)種顏色,每種顏色50ml,玩具店從不賣灰色顏料,但你可以用任意三種不同的顏料配出灰色,配出的灰色的體積不是三種顏料體積之和,而是最少的那個顏料,現在給出你需要的N種顏料的體積和灰色的體積,求最少需要多少套顏料。
思路
看過題目,首先想到將除開灰色之外需要的最少套數算出來,然后判斷用剩下的顏料能否配出足夠的灰色。
這個思路……當然沒有問題,但是怎么判斷“能否配出足夠的灰色”呢?
當然不能直接取剩余最多的3個顏料算,如果最多的三個顏料夠配出灰色還好,如果不能配出灰色,后面的顏料也是可以用的。
例如有5盒顏料,編號1,2,3,4,5,其中1,2,3號剩余的最多,配出的灰色卻還不夠,所以我們想再買一盒,然后繼續看1,2,3號,事實上,4,5號也是可以和1,2,3配對的,而且有可能需要的盒數更少,所以不能一直算第一次最大的三個。
那很好辦,每次sort從大到小一次就可以了啊,但還有一個問題,每次都把sort后的1,2,3號顏料直接全部配成灰色嗎?不是的,你看看樣例4就知道了,雖然我并不知道樣例4怎么算的,但肯定不是每次配最多的3個。
所以,我們索性1毫升1毫升的配每配1次sort1次,N只有12,也不用擔心超時~
代碼
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int p[15],gray; bool cmp(int a,int b){return a>b;}//自定義sort排序從大到小 int main() {while(1){int N,tmax=-1;scanf("%d",&N);if(!N) return 0;for(int i=1;i<=N;i++){scanf("%d",&p[i]);tmax=max(tmax,(p[i]+49)/50);//算出除開灰色最少需要多少套顏料,注意是上取整}scanf("%d",&gray);for(int i=1;i<=N;i++)p[i]=tmax*50-p[i];//將用去的顏料減去while(gray>0){sort(p+1,p+N+1,cmp);if(p[3]<=0)//要配的是p[1],p[2],p[3],sort后p[3]是最小的,如果p[3]都不夠的,肯定要再買一套{for(int i=1;i<=N;i++)p[i]+=50;//所有顏料增加50mltmax++;//套數增加}p[1]--;p[2]--;p[3]--;gray--;//每次只配1ml}printf("%d\n",tmax);} }轉載于:https://www.cnblogs.com/LinqiongTaoist/p/7203721.html
總結
以上是生活随笔為你收集整理的【模拟】【贪心】POJ2709Painter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用C语言来实现模块化
- 下一篇: 【Java核心计算 基础知识(第9版)】