The Merry Milk Makers company buys milk from farmers, packages it into attractive 1- and 2-Unit bottles, and then sells that milk to grocery stores so we can each start our day with delicious cereal and milk.
Since milk packaging is such a difficult business in which to make money, it is important to keep the costs as low as possible. Help Merry Milk Makers purchase the farmers' milk in the cheapest possible manner. The MMM company has an extraordinarily talented marketing department and knows precisely how much milk they need each day to package for their customers.
The company has contracts with several farmers from whom they may purchase milk, and each farmer has a (potentially) different price at which they sell milk to the packing plant. Of course, a herd of cows can only produce so much milk each day, so the farmers already know how much milk they will have available.
Each day, Merry Milk Makers can purchase an integer number of units of milk from each farmer, a number that is always less than or equal to the farmer's limit (and might be the entire production from that farmer, none of the production, or any integer in between).
Given:
- The Merry Milk Makers' daily requirement of milk
- The cost per unit for milk from each farmer
- The amount of milk available from each farmer
Note: The total milk produced per day by the farmers will always be sufficient to meet the demands of the Merry Milk Makers even if the prices are high.
牛奶包装是一个如此低利润的生意,所以尽可能低的控制初级产品(牛奶)的价格变的十分重要.
请帮助快乐的牛奶制造者(Merry Milk Makers)以可能的廉价的方式取得他们所需的牛奶. 快乐的牛奶制造公司从一些农民那购买牛奶,每个农民卖给牛奶制造公司的价格不一定相同. 而且,如一只母牛一天只能生产一定量的牛奶,农民每一天只有一定量的牛奶可以卖. 每天,快乐的牛奶制造者从每个农民那购买一定量的牛奶,少于或等于农民所能提供的大值. 给出快乐牛奶制造者的每日的牛奶需求,连同每个农民的可提供的牛奶量和每加仑的价格,请计算快乐的牛奶制造者所要付出钱的小值. 注意: 每天农民生产的牛奶的总数对快乐的牛奶制造者来说足够的.
PROGRAM NAME: milk
INPUT FORMAT
Line 1: | Two integers, N and M. The first value, N, (0 <= N <= 2,000,000) is the amount of milk that Merry Milk Makers wants per day. The second, M, (0 <= M <= 5,000) is the number of farmers that they may buy from. |
Lines 2 through M+1: | The next M lines each contain two integers: Pi and Ai. Pi (0 <= Pi <= 1,000) is price in cents that farmer i charges. Ai (0 <= Ai <= 2,000,000) is the amount of milk that farmer i can sell to Merry Milk Makers per day. |
SAMPLE INPUT (file milk.in)
100 5 5 20 9 40 3 10 8 80 6 30
INPUT EXPLANATION
100 5 -- MMM wants 100 units of milk from 5 farmers5 20 -- Farmer 1 says, "I can sell you 20 units at 5 cents per unit"
9 40 etc.
3 10 -- Farmer 3 says, "I can sell you 10 units at 3 cents per unit"
8 80 etc.
6 30 -- Farmer 5 says, "I can sell you 30 units at 6 cents per unit"
OUTPUT FORMAT
A single line with a single integer that is the minimum cost that Merry Milk Makers must pay for one day's milk.
SAMPLE OUTPUT (file milk.out)
630
OUTPUT EXPLANATION
Here's how the MMM company spent only 630 cents to purchase 100 units of milk:Price per unit | Units available | Units bought | Price * # units | Total cost | Notes |
---|---|---|---|---|---|
5 | 20 | 20 | 5*20 | 100 | |
9 | 40 | 0 | Bought no milk from farmer 2 | ||
3 | 10 | 10 | 3*10 | 30 | |
8 | 80 | 40 | 8*40 | 320 | Did not buy all 80 units! |
6 | 30 | 30 | 6*30 | 180 | |
Total | 180 | 100 | 630 | Cheapest total cost |
题解:
简单的贪心算法:将所有的牛奶按价格升序排列(用快排),然后从低到高买入,直到买够 m 为止。
贪心的证明: 假设某次买了价格稍高的牛奶,可以得到最优解。那么把这次买的牛奶换成价格更低的牛奶,其它不变,那么所得的解较优。假设不成立。 利用桶排的思想可以把代码压缩到极限。因其价格范围为[0..1000]可以用计数排序来做,就可以得到一个傻瓜代码(参见代码四)。
最佳解题方法: 因为价格的范围在 1..1000 之内,所以,我们只要在读入数据的时候把相同价格的合并 即可,进行计算时,也只需要 fori:=0 to1000do 进行扫描如果有价格为 i 的牛奶就收购即可, 所以不需要排序。
代码如下:
/*
ID: zhaoxia13
LANG: C
TASK: milk
*/
#include <stdio.h>
#include <stdlib.h>
struct milk
{
int mon;
int qua;
}r[5005], temp;
void bi(int a, int b)
{
int i, j, mid;
i = a; j = b;
mid = r[(i+j)/2].mon;
while(i <= j)
{
while(i <= j && r[i].mon < mid)
i++;
while(i <= j && r[j].mon > mid)
j--;
if(i <= j)
{
temp = r[i]; r[i] = r[j]; r[j] = temp;
i++; j--;
}
}
if(i < b)
bi(i, b);
if(j > a)
bi(a, j);
}
int main()
{
freopen("milk.in", "r", stdin);
freopen("milk.out", "w", stdout);
int i, j, m, n, sum = 0, a1, b1, t = 0;
scanf("%d%d", &n, &m);
for(i = 0; i < m; i++)
{
scanf("%d%d", &a1, &b1);
r[t].mon = a1;
r[t].qua = b1;
t++;
}
bi(0, t);
for(i = 0; i <= t; i++)
{
if(n - r[i].qua >= 0)
{
n -= r[i].qua;
sum += r[i].mon * r[i].qua;
}
else
{
sum += r[i].mon * n;
break;
}
}
printf("%d\n", sum);
fclose(stdin);
fclose(stdout);
return 0;
}