题意:就是老鼠要用猫粮换粮食,第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;
}

HDU 1009 FatMouse' Trade (贪心算法)的更多相关文章

  1. HDU 1009 FatMouse' Trade(贪心)

    FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...

  2. HDU 1009 FatMouse' Trade(简单贪心 物品可分割的背包问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  3. HDU 1009 FatMouse' Trade(简单贪心)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  4. hdu 1009:FatMouse' Trade(贪心)

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. HDU 1009 FatMouse' Trade【贪心】

    解题思路:一只老鼠共有m的猫粮,给出n个房间,每一间房间可以用f[i]的猫粮换取w[i]的豆,问老鼠最多能够获得豆的数量 sum 即每一间房间的豆的单价为v[i]=f[i]/w[i],要想买到最多的豆 ...

  6. HDOJ.1009 FatMouse' Trade (贪心)

    FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...

  7. Hdu 1009 FatMouse' Trade

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. Hdu 1009 FatMouse' Trade 2016-05-05 23:02 86人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  9. Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. ios 获得指定url的cookie

    NSArray *myCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookiesForURL:[NSURL URLWithStrin ...

  2. 复习:使用HTML编写简单程序

    今天我试着用HTML编写了九九乘法表 代码如下 浏览器显示如下 2.输出静夜思 代码如下 2.浏览器显示

  3. select语法图

  4. HTML的属性和css基础

    1.name属性: name属性,用于指定标签元素的名称,<a>标签内必须提供href或name属性:<a name ="value"> 2.id属性: 1 ...

  5. 单点登录(SSO)解决方案之 CAS客户端与Spring Security集成

    接上篇:单点登录(SSO)解决方案之 CAS服务端数据源设置及页面改造 Spring Security Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制 ...

  6. httpclient4例子

    参考:http://hc.apache.org/httpclient-3.x/tutorial.html import org.apache.http.HttpEntity; import org.a ...

  7. LIS问题(DP解法)---poj1631

    题目链接:http://poj.org/problem?id=1631 这个题题目有些难看懂hhh,但实质就是求LIS--longest increasing sequence. 以下介绍LIS的解法 ...

  8. Python3 ord() 函数

    Python3 ord() 函数  Python3 内置函数 描述 ord() 函数是 chr() 函数(对于 8 位的 ASCII 字符串)的配对函数,它以一个字符串(Unicode 字符)作为参数 ...

  9. VMware克隆CentOS网络配置

    配置网络 如果是克隆CentOS的: vi /etc/udev/rules.d/70-persistent-net.rules 注释掉网络eth0,把最后一个改为eth0,记录下mac地址. vi / ...

  10. SourceTree下载 及使用

    SourceTree 代码库管理工具 https://www.cnblogs.com/QianChia/p/8531725.html#_label0 SourceTree的基本使用 https://w ...