| 查看: 456 | 回复: 1 | ||
| 【奖励】 本帖被评价1次,作者stephenliu89增加金币 0.5 个 | ||
[资源]
【原创】一道编程练习题,贪心法,附我的解法
|
||
|
Mixing Milk Since milk packaging is such a low margin business, it is important to keep the price of the raw product (milk) as low as possible. Help Merry Milk Makers get the milk they need in the cheapest possible manner. The Merry Milk Makers company has several farmers from which they may buy milk, and each one has a (potentially) different price at which they sell to the milk packing plant. Moreover, as a cow can only produce so much milk a day, the farmers only have so much milk to sell per day. Each day, Merry Milk Makers can purchase an integral amount of milk from each farmer, less than or equal to the farmer's limit. Given the Merry Milk Makers' daily requirement of milk, along with the cost per gallon and amount of available milk for each farmer, calculate the minimum amount of money that it takes to fulfill the Merry Milk Makers' requirements. Note: The total milk produced per day by the farmers will be sufficient to meet the demands of the 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' want 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 55 209 403 108 806 30 OUTPUT FORMAT A single line with a single integer that is the minimum price that Merry Milk Makers can get their milk at for one day. SAMPLE OUTPUT (file milk.out) 630 =============================================== 简单的贪心法题目,很容易AC My Solution: #include #include using namespace std; long int N, price = 0 ,amount = 0; //value int M; //number of farmer struct str { int P; //price long int A; //amount } farmer[5001],temp; int main() { ifstream fin ( "milk.in" ); ofstream fout ( "milk.out" ); int i, j; fin >> N >> M; for ( i = 0; i < M; i++ ) fin >> farmer[ i ].P >> farmer [ i ].A; for ( j = M - 1; j >= 0; j--) for ( i = 0; i < j; i++ ) if( farmer[ i ].P > farmer[ i + 1 ].P ) { temp = farmer[ i ]; farmer [ i ] = farmer [ i + 1 ]; farmer [ i + 1 ] = temp; } i = -1; while ( amount < N ) { i++; if ( farmer [ i ].A <= N - amount ) { price += farmer[ i ].A * farmer[ i ].P; amount += farmer [ i ].A; } else { price += ( N - amount ) * farmer[ i ].P; amount +=N - amount; } } fout << price << endl; return 0; } [ Last edited by stephenliu89 on 2010-8-29 at 16:22 ] |
» 猜你喜欢
请问哪里可以有青B申请的本子可以借鉴一下。
已经有4人回复
真诚求助:手里的省社科项目结项要求主持人一篇中文核心,有什么渠道能发核心吗
已经有6人回复
孩子确诊有中度注意力缺陷
已经有14人回复
三甲基碘化亚砜的氧化反应
已经有4人回复
请问下大家为什么这个铃木偶联几乎不反应呢
已经有5人回复
请问有评职称,把科研教学业绩算分排序的高校吗
已经有5人回复
2025冷门绝学什么时候出结果
已经有3人回复
天津工业大学郑柳春团队欢迎化学化工、高分子化学或有机合成方向的博士生和硕士生加入
已经有4人回复
康复大学泰山学者周祺惠团队招收博士研究生
已经有6人回复
AI论文写作工具:是科研加速器还是学术作弊器?
已经有3人回复
» 本主题相关价值贴推荐,对您同样有帮助:
“数值分析”、“数值解法”等中常出现的“数值”一词是什么意思?
已经有17人回复
晶体结构优化和画能带图的方法是否保持一致???
已经有12人回复
简单回复
recen2楼
2010-10-28 18:11
回复













回复此楼