24小时热门版块排行榜    

查看: 480  |  回复: 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 的主题更新
☆ 无星级 ★ 一星级 ★★★ 三星级 ★★★★★ 五星级
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 材料080500调剂求收留 +4 一颗meteor 2026-03-13 4/200 2026-03-19 10:32 by 30660438
[考研] 一志愿吉林大学材料学硕321求调剂 +6 Ymlll 2026-03-18 9/450 2026-03-19 10:28 by 星空星月
[考研] 085410人工智能专硕317求调剂(0854都可以) +3 xbxudjdn 2026-03-18 3/150 2026-03-18 22:14 by zhq0425
[考研] 0854可跨调剂,一作一项核心论文五项专利,省、国级证书40+数一英一287 +8 小李0854 2026-03-16 8/400 2026-03-18 14:35 by 搏击518
[考研] 297求调剂 +8 戏精丹丹丹 2026-03-17 8/400 2026-03-18 14:30 by laoshidan
[考研] 070300化学319求调剂 +6 锦鲤0909 2026-03-17 6/300 2026-03-18 13:22 by Iveryant
[考研] 0703化学求调剂 总分331 +3 ZY-05 2026-03-13 3/150 2026-03-18 10:58 by macy2011
[考博] 26博士申请 +3 1042136743 2026-03-17 3/150 2026-03-17 23:30 by 轻松不少随
[考研] 296求调剂 +5 大口吃饭 身体健 2026-03-13 5/250 2026-03-17 21:05 by 不惑可乐
[考研] 277调剂 +5 自由煎饼果子 2026-03-16 6/300 2026-03-17 19:26 by 李leezz
[硕博家园] 湖北工业大学 生命科学与健康学院-课题组招收2026级食品/生物方向硕士 +3 1喜春8 2026-03-17 5/250 2026-03-17 17:18 by ber川cool子
[考研] 308求调剂 +4 是Lupa啊 2026-03-16 4/200 2026-03-17 17:12 by ruiyingmiao
[考研] 26考研求调剂 +6 丶宏Sir 2026-03-13 6/300 2026-03-17 16:13 by 醉在风里
[考研] 290求调剂 +6 孔志浩 2026-03-12 11/550 2026-03-17 14:41 by 周舟舟77
[考研] 材料工程专硕274一志愿211求调剂 +6 薛云鹏 2026-03-15 6/300 2026-03-17 11:05 by 学员h26Tkc
[考研] 药学383 求调剂 +3 药学chy 2026-03-15 4/200 2026-03-16 20:51 by 元子^0^
[考研] 0854控制工程 359求调剂 可跨专业 +3 626776879 2026-03-14 9/450 2026-03-16 17:42 by 626776879
[考研] 070300化学学硕求调剂 +6 太想进步了0608 2026-03-16 6/300 2026-03-16 16:13 by kykm678
[考研] 0856求调剂 +3 刘梦微 2026-03-15 3/150 2026-03-16 10:00 by houyaoxu
[考研] 材料工程调剂 +9 咪咪空空 2026-03-12 9/450 2026-03-13 22:05 by 星空星月
信息提示
请填处理意见