算法复习之贪心算法poj2709

题意:一套涂料有3~12种颜色,每种颜色50ml。Emily上课需要n种颜色的涂料,第i种颜色需要color[i]ml,此外,Emily还需要gray ml的灰色涂料,每ml灰色的涂料需要3种不同颜色的其他涂料各1ml融合而成。问emily要上课,至少需要买几套涂料?
 
思路:贪心。由于n很小,所以每次1ml的其他涂料融合成灰色时,再对他们进行排序。

 

代码如下:

 

#include<iostream>
#include<algorithm>
using namespace std;
const int Max = 15;

int main()
{
    int n, i, color[Max], gray;
    while(cin >> n && n != 0)
	{
        for(i = 0; i < n; i ++)
            cin >> color[i];
        cin >> gray;
        sort(color, color + n);
        int ans = 0, max = 0;
        while(max < color[n-1])
		{  //  首先找去满足除了灰色的其他颜色,最少需几套涂料。
            ans ++;
            max += 50;
        }
        while(1)
		{
            while(color[2] < max && gray > 0)
			{  //  模拟3种不同颜色融合成灰色的情况。
                color[0] ++;
                color[1] ++;
                color[2] ++;
                gray --;
                sort(color, color + n);
            }
            if(gray == 0) 
				break;
            ans ++;
            max += 50;
        }
        cout << ans << endl;
    }
    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值