HDU1009老鼠的旅行 (贪心算法)
FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 62898 Accepted Submission(s):
21231
with the cats guarding the warehouse containing his favorite food,
JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of
JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade
for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of
JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now
he is assigning this homework to you: tell him the maximum amount of JavaBeans
he can obtain.
case begins with a line containing two non-negative integers M and N. Then N
lines follow, each contains two non-negative integers J[i] and F[i]
respectively. The last test case is followed by two -1's. All integers are not
greater than 1000.
number accurate up to 3 decimal places, which is the maximum amount of JavaBeans
that FatMouse can obtain.
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
31.500
- #include <string.h>
- #include <iostream>
- #include <algorithm>
- #include <stdlib.h>
- #include <stdio.h>
- using namespace std;
- struct sa
- {
- int a,b;
- double c;
- }data[];
- bool cmp(sa x,sa y)
- {
- return x.c>y.c;
- }
- int main()
- {
- int i,j,m,n;
- double sum;
- while (cin>>m>>n&&m!=-&&n!=-)
- {
- sum=;
- for (i=;i<n;i++)
- {
- cin>>data[i].a>>data[i].b;
- data[i].c=(double)data[i].a/(double)data[i].b;
- }
- sort(data,data+n,cmp);
- //for (i=0;i<n;i++)
- //cout<<data[i].a<<" "<<data[i].b<<" "<<data[i].c<<endl;
- for (i=;i<n;i++)
- {
- if (m>=data[i].b)
- {
- m-=data[i].b;
- sum+=data[i].a;
- }
- else if (m<data[i].b&&m>)
- {
- sum+=data[i].c*(double)m;
- m=;
- }
- else
- break;
- }
- printf("%.3lf\n",sum);
- }
- return ;
- }
HDU1009老鼠的旅行 (贪心算法)的更多相关文章
- 贪心算法:旅行商问题(TSP)
TSP问题(Traveling Salesman Problem,旅行商问题),由威廉哈密顿爵士和英国数学家克克曼T.P.Kirkman于19世纪初提出.问题描述如下: 有若干个城市,任何两个城市之间 ...
- 基于贪心算法求解TSP问题(JAVA)
概述 前段时间在搞贪心算法,为了举例,故拿TSP来开刀,写了段求解算法代码以便有需之人,注意代码考虑可读性从最容易理解角度写,没有优化,有需要可以自行优化! 详细 代码下载:http://www.de ...
- HDU 1009 FatMouse' Trade (贪心算法)
题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换, ...
- C/C++贪心算法解决TSP问题
贪心算法解决旅行商问题 TSP问题(Traveling Salesman Problem,旅行商问题),由威廉哈密顿爵士和英国数学家克克曼T.P.Kirkman于19世纪初提出.问题描述如下: 有若干 ...
- 贪心算法(Greedy Algorithm)
参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...
- 算法导论----贪心算法,删除k个数,使剩下的数字最小
先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- ACM_ICPC hdu-2111(简单贪心算法)
一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...
- 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题
1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...
随机推荐
- 【BZOJ-1452】Count 树状数组 套 树状数组
1452: [JSOI2009]Count Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1769 Solved: 1059[Submit][Stat ...
- 【poj2226】 Muddy Fields
http://poj.org/problem?id=2226 (题目链接) 题意 给出一个只包含‘.’和‘*’的矩阵,用任意长度的宽为1的木板覆盖所有的‘*’而不覆盖‘.’,木板必须跟矩形的长或宽平行 ...
- 【poj1012】 Joseph
http://poj.org/problem?id=1012 (题目链接) 半年前的考试题..任然清晰的记得那次差10分就AK... 题意 约瑟夫环,有前k个好人,后k个坏人,要求使得后k个坏人先死的 ...
- RIP、OSPF、BGP、动态路由选路协议、自治域AS
相关学习资料 tcp-ip详解卷1:协议.pdf http://www.rfc-editor.org/rfc/rfc1058.txt http://www.rfc-editor.org/rfc/rfc ...
- window自动切换ip的脚本
因为总要切换ip,所以百度了一下脚本 如下http://jingyan.baidu.com/article/d2b1d1029d21b95c7e37d4fa.html 动态ip netsh inter ...
- std::shared_ptr
在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...
- 连接ssql语句
- spring mvc实现修改+删除
1.在userController中添加修改的方法 a.首先点击修改,我们一般是到修改界面,并且上面有值,并且有提交按钮 b.修改后,提交到查看的页面 //进入修改界面 @RequestMapping ...
- C# Socket大文件上传
public sealed class SocketData { private SocketData() { } public static SendFileMode SendFile(Socket ...
- RedHat linux配置yum本地资源
今天安装个linux平台的oracle数据库,在oracle检测的时候发现有些系统补丁包没有安装,手动rpm安装报有些关联包也没有安装,想想如果一个一个rpm的话累死人了要,所以想起用yum来进行安装 ...