HDU 1009 FatMouse' Trade (贪心算法)
2016-06-02 18:34
417 查看
题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换。问给定的猫粮最多能换多少粮食。
析:贪心算法。我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换,如果不能,
肯定是最后一次了,就把剩下全部换了,看看能换多少。求和。
代码如下:
析:贪心算法。我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换,如果不能,
肯定是最后一次了,就把剩下全部换了,看看能换多少。求和。
代码如下:
#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <cstring> #include <map> #include <cctype> using namespace std; struct node{ double ave; int x, y; node() { } node(int xx, int yy) : x(xx), y(yy), ave((double)xx/(double)yy) { } bool operator < (const node &p) const { return ave > p.ave; } }; node a[1005]; int main(){ int n, m; while(~scanf("%d %d", &n, &m) && (m+n != -2)){ int x, y; for(int i = 0; i < m; ++i){ scanf("%d %d", &x, &y); a[i] = node(x, y); } sort(a, a+m); double ans = 0.0; for(int i = 0; i < m; ++i){ if(n >= a[i].y){ ans += a[i].x; n -= a[i].y; } else if(!n) break; else{ ans += a[i].ave * n; n = 0; } } printf("%.3lf\n", ans); } return 0; }
相关文章推荐
- Qt对话框_模态/非模态
- 关于C++中的继承和重载的区别
- BMFont制作美术字体
- 用代码画流程图和时序图快餐教程(3) - PlantUML画时序图
- CentOs下用Qt Creator出现找不到GL/gl.h问题
- Linux4:useradd、userdel、passwd、groupadd、chgrp、chown、df、du、sort、wget
- DSP需求平台
- 在特定的action里使用validates
- 观察者模式
- [记录][续爬虫]模拟京东登录并且抢购物券
- C#对Excel表的读取和写入
- 新建一个CCS4.1项目及其文件配置
- ServletContextListener 解析用法
- Linux3:more、which、find、chmod、tar、diff、grep、ps、netstat、uname
- 单例模式
- https://jivoi.github.io/老外大神的两篇笔录
- 创建Oracle数据库、数据库名与实例名与SID之间的关系(图文详解)
- Linux2:vi、ls、cd、pwd、mkdir、rm、mv、cp、cat、tail
- Tomcat自带DBCP数据库连接池配置
- js、oc、html交互