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个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...
随机推荐
- 与你相遇好幸运,Linux常用命令
开机挂载硬盘 cat /etc/fstab /dev/sda1 /mnt/sda1 ext3 defaults 0 0 挂载硬盘 mount /dev/sdb5 /home/dis ...
- attributeError:'module' object has no attribute ** 解决办法
写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...
- javascript实用技巧,js小知识
一.js整数的操作 使用|0和~~可以将浮点转成整型且效率方面要比同类的parseInt,Math.round 要快,在处理像素及动画位移等效果的时候会很有用.性能比较见此. var foo = (1 ...
- OGG异常处理
ALTER REPLICAT LCMA1REP,BEGIN NOW 从最新的trail文件开始读取 ALTER REPLICAT LCMA1REP,EXTSEQNO 191(对应的 trail的序号 ...
- 使用ASP.NET MVC、Rabbit WeixinSDK和Azure快速开发部署微信后台
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:公众号后台系统和数据都基本准备妥当了,可以来分享下我是如何开发本微信公众号的后台系统了 ...
- Socket通信客户端设计(Java)
public class Client extends JFrame implements Runnable{ private JPanel jPanel= new JPanel(); private ...
- android:imeOptions属性(转)
默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘 设置 android:imeOptions="actionDone" ,软键盘下方变成“完成”,点击后光 ...
- ubuntu下整合eclipse和javah生成jni头文件开发android的native程序(转)
本文介绍两种利用javah命令生成jni头文件的方法,第一种为大众所知的javah命令,第二种为整合javah到eclipse里面.推荐第二种方式,方便快捷,随时修改随时生成 0:前提和条件: 1:u ...
- POJ 1947 Rebuilding Roads 树形DP
Rebuilding Roads Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...
- 接着上一篇 《Is WPF dead》
最近美国的PM传来消息,说微软在收集开发者的意见,会对WPF进行改进,微软会主要在1) performance 2) interop 3) touch and 4) access to WinRT A ...