24小时热门版块排行榜    

查看: 512  |  回复: 1
【奖励】 本帖被评价1次,作者stephenliu89增加金币 0.5

stephenliu89

银虫 (小有名气)


[资源] 【原创】一道编程练习题,贪心法,附我的解法

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 ]
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
简单回复
recen2楼
2010-10-28 18:11   回复  
 
相关版块跳转 我要订阅楼主 stephenliu89 的主题更新
☆ 无星级 ★ 一星级 ★★★ 三星级 ★★★★★ 五星级
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[找工作] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +4 gy1nBQXYQJqL 2026-08-29 8/400 2026-08-30 12:03 by l0VvVHGBGRLv
[考博] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 ASdOkHsho7FD 2026-08-28 6/300 2026-08-30 11:10 by l0VvVHGBGRLv
[基金申请] 国社科又开始会评了,不知道这次命运如何 +3 雨打竹帘 2026-08-30 3/150 2026-08-30 09:43 by KNIGHT2011
[考博] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 gy1nBQXYQJqL 2026-08-29 5/250 2026-08-30 07:05 by ZPa0EcMwuECS
[硕博家园] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +4 gy1nBQXYQJqL 2026-08-29 5/250 2026-08-30 06:55 by ZPa0EcMwuECS
[考博] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +6 ASdOkHsho7FD 2026-08-28 10/500 2026-08-30 06:11 by ZPa0EcMwuECS
[论文投稿] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +5 gy1nBQXYQJqL 2026-08-29 6/300 2026-08-30 01:58 by ZPa0EcMwuECS
[基金申请] 中青基了要发朋友圈吗? +4 349506619 2026-08-28 4/200 2026-08-29 22:41 by alongwaytogo
[基金申请] 国社科申报系统有变化 +3 kynobel 2026-08-23 3/150 2026-08-29 21:57 by 余韵清
[基金申请] 国自然面上复盘~欢迎讨论 (金币+15) +15 晴天加油 2026-08-26 16/800 2026-08-29 18:28 by symmetry
[基金申请] 怎么查啊 +6 huang1991js 2026-08-26 6/300 2026-08-28 08:42 by winsaint
[基金申请] 面上合作单位盖章 +5 ssyjh 2026-08-27 5/250 2026-08-27 20:50 by gdfollow
[文学芳草园] 梦想 +3 myrtle 2026-08-26 3/150 2026-08-27 10:01 by angelyueyi
[基金申请] 为什么 国际(地区)合作与交流项目 没有放榜? 10+3 majunge000 2026-08-26 11/550 2026-08-27 08:42 by 北京莱茵编辑
[基金申请] 出来了 +9 trojank 2026-08-26 9/450 2026-08-26 14:25 by 宝贝虫子
[基金申请] 国合里面能看到了 +7 一怀馨秋 2026-08-26 7/350 2026-08-26 11:23 by zhaosm1982
[基金申请] 国际合作可查了,中了面上 (EPI+1)(金币+50) +18 Ldrop2023 2026-08-26 18/900 2026-08-26 11:15 by cmrandy
[基金申请] 系统进不去 +4 yanglien 2026-08-26 5/250 2026-08-26 11:10 by wenfengw83
[基金申请] 今天务委会开完了,明天出结果吗 +19 angus9576 2026-08-25 23/1150 2026-08-26 10:03 by zp519
[基金申请] 某些机构,以效率低为荣,以效率低作为存在感 +9 yuleib84 2026-08-25 10/500 2026-08-25 17:14 by alexon
信息提示
请填处理意见