1010 of greedy strategy

来源:互联网 发布:长期成本 知乎 编辑:程序博客网 时间:2024/05/16 20:47
Problem 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. <br>
 

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 07 25 60 400 250 0 60 0 5004 90 95 75 95 104 90 95 75 95 115 0 0 0 0 0 3330
 

Sample Outputong
28234
 题目要求:提供所需除灰色数目颜色的种类和所有颜色的需求量,灰色能用任意三种颜色混合。计算出所需最少的买的桶数。
 解题思路:1先将出灰色的最大颜色所需数,除以50后有再模50有余数加一为最初所需的桶数。然后进行排列在大的三个元素若不为0则都减一灰色计数加一,递归后直到出现0后跳出比较灰色计数与所需灰色,若前者大于后跳出输出桶数,否者桶数加一后进入前一个递归。
细节:要每次减一后进行一次排序否者会出错(不知为什么没有超时)。
ps:本打算用优先队列做,但是插入后不能进行修改,后只能用数组加sort。
#include<iostream>#include<cstdio>#include<vector>#include<algorithm>using namespace std;bool cp(int a,int b){    return a>b;}int main(){    vector<int> a;    int color[20],count;    int i,n,g,g1,max,min,flag=0;    while(scanf("%d",&n)!=EOF&&n)    {        for(i=0;i<n;i++)            scanf("%d",&color[i]);            scanf("%d",&g);            max=0;            for(i=0;i<n;i++)            if(color[i]>max)max=color[i];            count=max/50;            if(max%50)count++;            g1=0;            if(g1==g){printf("%d\n",count);continue;}            else for(i=0;i<n;i++)            a.push_back(count*50-color[i]);            sort(a.begin(),a.end(),cp);            mc:while(g1<g)            {                while(1)            {                if(a[2]==0||a[1]==0||a[0]==0){flag=1;break;}              a[0]--;a[1]--;a[2]--;              g1++;              sort(a.begin(),a.end(),cp);            }  if(flag){flag=0;break;}            }            if(g1<g){                count++;                vector<int>::iterator it;                for(it=a.begin();it!=a.end();it++)                *it+=50;                goto mc;            }            else printf("%d\n",count);            a.clear();        }}


0 0
原创粉丝点击