milk解题报告 —— icedream61 博客园(转载请注明出处)
------------------------------------------------------------------------------------------------------------------------------------------------
【题目】
  我是牛奶制造商,我一天需要N加仑的牛奶,总共有M个农民可以供给我。
  这M个农民的信息共M行,第i个农民有num[i]加仑牛奶,每加仑价格为price[i]。
  求我买够牛奶所需的最少钱数。
  数据保证,农民生产的牛奶总数一定是够的。
【数据范围】
  0<=N<=2,000,000
  0<=M<=5,000
  0<=price[i]<=1,000
  0<=num[i]<=2,000,000
【输入样例】
  100 5
  5 20
  9 40
  3 10
  8 80
  6 30
【输出样例】
  630
------------------------------------------------------------------------------------------------------------------------------------------------
【分析】
  排序一下,贪心即可。
------------------------------------------------------------------------------------------------------------------------------------------------
【总结】
  一遍AC。
  顺带复习了下类的运算符号重载。

------------------------------------------------------------------------------------------------------------------------------------------------

【代码】

  1. /*
  2. ID: icedrea1
  3. PROB: milk
  4. LANG: C++
  5. */
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. using namespace std;
  10.  
  11. const int maxn = ;
  12. const int maxm = ;
  13.  
  14. struct Farmer
  15. {
  16. int price;
  17. int num;
  18. friend bool operator<(Farmer const &x,Farmer const &y) { return x.price<y.price; }
  19. };
  20.  
  21. int N,M;
  22. Farmer F[+maxm];
  23.  
  24. void qsort(int l,int r)
  25. {
  26. if(l>=r) return;
  27. int i=l,j=r;
  28. Farmer x=F[(l+r)>>];
  29. while(true)
  30. {
  31. while(F[i]<x) ++i;
  32. while(x<F[j]) --j;
  33. if(i>j) break;
  34. swap(F[i],F[j]);
  35. ++i; --j;
  36. }
  37. qsort(l,j); qsort(i,r);
  38. }
  39.  
  40. int main()
  41. {
  42. ifstream in("milk.in");
  43. ofstream out("milk.out");
  44.  
  45. in>>N>>M;
  46. for(int i=;i<=M;++i) in>>F[i].price>>F[i].num;
  47.  
  48. qsort(,M);
  49.  
  50. int cost=;
  51. for(int i=;N && i<=M;++i)
  52. {
  53. int num=min(N,F[i].num);
  54. N-=num; cost+=F[i].price*num;
  55. }
  56. out<<cost<<endl;
  57.  
  58. in.close();
  59. out.close();
  60. return ;
  61. }

USACO Section1.3 Mixing Milk 解题报告的更多相关文章

  1. USACO Section1.5 Prime Palindromes 解题报告

    pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  2. USACO Section1.5 Superprime Rib 解题报告

    sprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  3. USACO Section1.5 Number Triangles 解题报告

    numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  4. USACO Section1.4 Arithmetic Progressions 解题报告

    ariprog解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...

  5. USACO Section1.3 Combination Lock 解题报告

    combo解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  6. USACO Section1.3 Prime Cryptarithm 解题报告

    crypt1解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  7. USACO Section1.3 Barn Repair 解题报告

    barn1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  8. USACO Section1.2 Palindromic Squares 解题报告

    palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...

  9. USACO Section1.2 Dual Palindromes 解题报告

    dualpal解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...

随机推荐

  1. C++ POD类型

    POD( Plain Old Data)概念: Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to m ...

  2. Poj(2488),按照字典序深搜

    题目链接:http://poj.org/problem?id=2488 思路:按照一定的字典序深搜,当时我的想法是把所有的可行的路径都找出来,然后字典序排序. 后来,凡哥说可以在搜索路径的时候就按照字 ...

  3. python将图像转化为矩阵

    Image.fromarray(matrix).show()

  4. 【洛谷P1063】NOIP2006能量项链

    能量项链 https://www.luogu.org/problemnew/show/P1063 好像比合并石子更水.. 区间动规,f[l][r]表示合并区间l~r的最大能量 按区间长度dp 枚举中间 ...

  5. 10分钟了解 代理模式与java中的动态代理

    前言    代理模式又分为静态代理与动态代理,其中动态代理是Java各大框架中运用的最为广泛的一种模式之一,下面就用简单的例子来说明静态代理与动态代理. 场景    李雷是一个唱片公司的大老板,很忙, ...

  6. Juery返回Json数据格式,webForm中使用

    此篇的详细篇 //webForm中使用异步就会用到一般处理程序,前台调用一般处理程序这个页面去执行里面的方法 using System.Web.Script.Serialization; Newton ...

  7. AngularJS 表达式中添加过滤器实例

    过滤器可以通过一个管道字符(|)和一个过滤器添加到表达式中 历练实例: <!DOCTYPE html><html><head><meta http-equiv ...

  8. 深入浅出:promise的各种用法

    https://mp.weixin.qq.com/s?__biz=MzAwNTAzMjcxNg==&mid=2651425195&idx=1&sn=eed6bea35323c7 ...

  9. java.lang.UnsupportedOperationException: Exception occurred during processing request: null

    1.Action有问题,Struts2注解拼写错误,注解包版本不匹配! 2.今天还有一个错误,Tomcat服务器异常,无法启动,Remove/clean后还是无法启动 :极大可能是web.xml 写错 ...

  10. matlab2018a安装后帮助文档打不开解决方法

    安装matlab2018a破解版后,帮助文档提示需要许可证问题(破解版没有可用许可证): 解决方法是把文档设置为离线即可(预设---->帮助---->安装在本地---->小窗口)