ACM Greedy Mouse
Greedy Mouse
- 描述
-
A fat mouse prepared M pounds of cat food,ready to trade with the cats guarding the warehouse containing his
favorite food:peanut. The warehouse has N rooms.The ith room containsW[i] pounds of peanut and requires
F[i] pounds of cat food. Fatmouse does not have to trade for all the peanut in the room,instead,he may get
W[i]*a% pounds of peanut if he pays F[i]*a% pounds of cat food.The mouse is a stupid mouse,so can you tell
him the maximum amount of peanut he can obtain.
- 输入
- The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers W[i] and F[i] respectively. The test case is terminated by two -1. All integers are not greater than 1000.
- 输出
- For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of penaut that FatMouse can obtain.
- 样例输入
-
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1 - 样例输出
-
13.333
31.500
基本的背包问题,用贪心求解#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <utility>
using namespace std;
typedef pair<double,double> W;
bool cmp(const W& a,const W& b){ return a.first > b.first;}
int main(){
double m;
int n;
while(cin >> m >> n && m!=- && n!=-){
vector<W> exchange(n);
for(int i = ; i < n; ++ i){
double w,f;
cin >>w >>f;
exchange[i].first = w/f;
exchange[i].second = w;
}
sort(exchange.begin(),exchange.end(), cmp);
double res = ;
for(int i = ; i < n && m; ++i){
if(m > exchange[i].second/exchange[i].first){
res += exchange[i].second;
m -= exchange[i].second/exchange[i].first;
}else{
res+=m*exchange[i].first;
break;
}
}
printf("%0.3f\n",res);
}
}
ACM Greedy Mouse的更多相关文章
- POJ1386Play on Words[有向图欧拉路]
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11846 Accepted: 4050 De ...
- SPOJ Play on Words
传送门 WORDS1 - Play on Words #graph-theory #euler-circuit Some of the secret doors contain a very inte ...
- Play on Words[HDU1116]
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- POJ 1386 Play on Words(欧拉图的判断)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11838 Accepted: 4048 De ...
- 欧拉通路-Play on Words 分类: POJ 图论 2015-08-06 19:13 4人阅读 评论(0) 收藏
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10620 Accepted: 3602 Descri ...
- hdu 1116 Play on Words 欧拉路径+并查集
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- Play on Words(有向图欧拉路)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8571 Accepted: 2997 Description Some ...
- hdu 1116 Play on Words(欧拉通路)
Problem Description Some of the secret doors contain a very interesting word puzzle. The team of arc ...
- uva 10129 poj 1386 hdu 1116 zoj 2016 play on words
//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...
随机推荐
- js获取url方法
//设置或获取对象指定的文件名或路径.alert(window.location.pathname); //设置或获取整个 URL 为字符串.alert(window.location.href); ...
- 数据结构和算法 – 5.模式匹配和文本处理
了使用正则表达式,需要把 RegEx 类引入程序.大家可以在 System.Text.RegularExpression 名字域中找到这种类.一旦把这种类导入了程序,就需要决定想要用 RegEx 类来 ...
- Java集合源码学习(四)HashMap分析
ArrayList.LinkedList和HashMap的源码是一起看的,横向对比吧,感觉对这三种数据结构的理解加深了很多. >>数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据 ...
- .NET生成带Logo的二维码
使用ThoughtWorks.QRCode生成,利用这个库来生成带Logo的二维码(就是中间嵌了一个图片的二维码),直接见代码: HttpContext context = HttpContext.C ...
- 2014百度之星资格赛 1001:Energy Conversion(水题,逻辑题)
Energy Conversion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 对服务器的ftp端口进行修改
对服务器的ftp端口进行了修改,把21端口改了,比如221端口,就这样用221连接的时候,连接登录成功,但打不开目录,为何,总结如下: 1.完成一个FTP的传输过程不仅仅只需要21一个端口,而是2个端 ...
- java 杂物间 (一) Mybatis
这里放置的是一些杂物,生人勿入. Token的一般parse 过程. @Test public void shouldDemonstrateGenericTokenReplacement() { Ge ...
- php正则获取网页标题、关键字、网页描述代码
php正则获取网页关键字,代码如下: function get_keywords($html) { $html=strtolower($html); preg_match("@<hea ...
- Linux将文件拷到u盘
1:终端中获取root后,使用fdisk -l来查看位置 2:挂载u盘:mount /路径 /mnt cd /mnt可进入u盘,ls可查看文件 3:复制:cp /(文件路径) /mnt 4:退出u盘: ...
- 分享Kali Linux 2016.2第41周镜像虚拟机
分享Kali Linux 2016.2第41周镜像虚拟机该虚拟机使用Kali Linux 2016.2第41周镜像文件安装而成,系统已经更新到10月12日.里面已经进行如下配置:(1)设置官方软件源( ...