Usaco Section1.3 Mixing Milk Problem Solving report

Source: Internet
Author: User

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&GT;=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&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.