Milk Problem Solving Report--icedream61 Blog Park (reproduced please specify the source)
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
I am a milk manufacturer, I need n gallons of milk a day, and a total of M farmers can supply me.
This m farmer's information is a total of M-line, the first farmer has num[i] gallon milk, the price per gallon is price[i].
Ask me to buy enough milk for the minimum amount of money needed.
The data guarantee that the total amount of milk produced by farmers must be adequate.
"Data Range"
0<=n<=2,000,000
0<=m<=5,000
0<=price[i]<=1,000
0<=num[i]<=2,000,000
"Input Sample"
100 5
5 20
9 40
3 10
8 80
6 30
"Output Example"
630
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
Sort it out and be greedy.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
Once again AC.
Incidentally, the following class is reviewed for the operation symbol overloading.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Code
1 /*2 id:icedrea13 Prob:milk4 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten One Const intMAXN =2000000; A Const intMAXM = the; - - structFarmer the { - intPrice ; - intnum; -FriendBOOL operator< (FarmerConst&x,farmerConst&y) {returnx.price<Y.price;} + }; - + intn,m; AFarmer f[1+MAXM]; at - voidQsortintLintR) - { - if(L>=R)return; - inti=l,j=R; -Farmer x=f[(l+r) >>1]; in while(true) - { to while(f[i]<x) + +i; + while(X<f[j])--J; - if(I>J) Break; the swap (f[i],f[j]); *++i; --J; $ }Panax Notoginseng qsort (L,J); qsort (i,r); - } the + intMain () A { theIfstreaminch("milk.in"); +Ofstream out("Milk.out"); - $ inch>>N>>M; $ for(intI=1; i<=m;++i)inch>>F[i].price>>F[i].num; - -Qsort1, M); the - intcost=0;Wuyi for(intI=1; N && i<=m;++i) the { - intnum=min (n,f[i].num); WuN-=num; cost+=f[i].price*num; - } About out<<cost<<Endl; $ - inch. Close (); - out. Close (); - return 0; A}
Usaco Section1.3 Mixing Milk Problem Solving report