USACO Section1.3 Mixing Milk 解题报告
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。
顺带复习了下类的运算符号重载。
------------------------------------------------------------------------------------------------------------------------------------------------
【代码】
- /*
- ID: icedrea1
- PROB: milk
- LANG: C++
- */
- #include <iostream>
- #include <fstream>
- using namespace std;
- const int maxn = ;
- const int maxm = ;
- struct Farmer
- {
- int price;
- int num;
- friend bool operator<(Farmer const &x,Farmer const &y) { return x.price<y.price; }
- };
- int N,M;
- Farmer F[+maxm];
- void qsort(int l,int r)
- {
- if(l>=r) return;
- int i=l,j=r;
- Farmer x=F[(l+r)>>];
- while(true)
- {
- while(F[i]<x) ++i;
- while(x<F[j]) --j;
- if(i>j) break;
- swap(F[i],F[j]);
- ++i; --j;
- }
- qsort(l,j); qsort(i,r);
- }
- int main()
- {
- ifstream in("milk.in");
- ofstream out("milk.out");
- in>>N>>M;
- for(int i=;i<=M;++i) in>>F[i].price>>F[i].num;
- qsort(,M);
- int cost=;
- for(int i=;N && i<=M;++i)
- {
- int num=min(N,F[i].num);
- N-=num; cost+=F[i].price*num;
- }
- out<<cost<<endl;
- in.close();
- out.close();
- return ;
- }
USACO Section1.3 Mixing Milk 解题报告的更多相关文章
- USACO Section1.5 Prime Palindromes 解题报告
pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.5 Superprime Rib 解题报告
sprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.5 Number Triangles 解题报告
numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.4 Arithmetic Progressions 解题报告
ariprog解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...
- USACO Section1.3 Combination Lock 解题报告
combo解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- USACO Section1.3 Prime Cryptarithm 解题报告
crypt1解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.3 Barn Repair 解题报告
barn1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- USACO Section1.2 Palindromic Squares 解题报告
palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...
- USACO Section1.2 Dual Palindromes 解题报告
dualpal解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...
随机推荐
- C++ POD类型
POD( Plain Old Data)概念: Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to m ...
- Poj(2488),按照字典序深搜
题目链接:http://poj.org/problem?id=2488 思路:按照一定的字典序深搜,当时我的想法是把所有的可行的路径都找出来,然后字典序排序. 后来,凡哥说可以在搜索路径的时候就按照字 ...
- python将图像转化为矩阵
Image.fromarray(matrix).show()
- 【洛谷P1063】NOIP2006能量项链
能量项链 https://www.luogu.org/problemnew/show/P1063 好像比合并石子更水.. 区间动规,f[l][r]表示合并区间l~r的最大能量 按区间长度dp 枚举中间 ...
- 10分钟了解 代理模式与java中的动态代理
前言 代理模式又分为静态代理与动态代理,其中动态代理是Java各大框架中运用的最为广泛的一种模式之一,下面就用简单的例子来说明静态代理与动态代理. 场景 李雷是一个唱片公司的大老板,很忙, ...
- Juery返回Json数据格式,webForm中使用
此篇的详细篇 //webForm中使用异步就会用到一般处理程序,前台调用一般处理程序这个页面去执行里面的方法 using System.Web.Script.Serialization; Newton ...
- AngularJS 表达式中添加过滤器实例
过滤器可以通过一个管道字符(|)和一个过滤器添加到表达式中 历练实例: <!DOCTYPE html><html><head><meta http-equiv ...
- 深入浅出:promise的各种用法
https://mp.weixin.qq.com/s?__biz=MzAwNTAzMjcxNg==&mid=2651425195&idx=1&sn=eed6bea35323c7 ...
- java.lang.UnsupportedOperationException: Exception occurred during processing request: null
1.Action有问题,Struts2注解拼写错误,注解包版本不匹配! 2.今天还有一个错误,Tomcat服务器异常,无法启动,Remove/clean后还是无法启动 :极大可能是web.xml 写错 ...
- matlab2018a安装后帮助文档打不开解决方法
安装matlab2018a破解版后,帮助文档提示需要许可证问题(破解版没有可用许可证): 解决方法是把文档设置为离线即可(预设---->帮助---->安装在本地---->小窗口)