K - 贪心 基础
The warehouse has N rooms. The i-th room contains Jii pounds of JavaBeans and requires Fii pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get Jii* a% pounds of JavaBeans if he pays Fii* 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.
InputThe 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 Jii and Fii respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
OutputFor each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
Sample Output
13.333
31.500
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<fstream>
#include<memory>
#include<string>
using namespace std;
typedef long long LL;
#define MAXN 1004
#define INF 1000000009
/*
已知每个选择的 收益和花费 而且每个选择不一定要完成全部,收益和花费成比例匹配 求最大收益
贪心算法 不断采用收益比例最大的
*/
int m, n;
struct node
{
double rate, profit, cost;
}a[MAXN];
bool cmp(node a, node b)
{
return a.rate > b.rate;
}
int main()
{
while (scanf("%d%d", &m, &n))
{
if (m == - && n == -)
break;
for (int i = ; i < n; i++)
{
cin >> a[i].profit >> a[i].cost;
a[i].rate = a[i].profit / a[i].cost;
}
sort(a, a + n, cmp);
double ans = 0.0, tmp = m;
for (int i = ; i < n; i++)
{
if (tmp >= a[i].cost)
{
ans += a[i].profit;
tmp -= a[i].cost;
}
else
{
ans += tmp*a[i].rate;
break;
}
}
printf("%.3lf\n", ans);
}
}
K - 贪心 基础的更多相关文章
- K贪心
<span style="color:#330099;">/* K - 贪心 基础 Time Limit:1000MS Memory Limit:32768KB 64b ...
- poj2709 贪心基础
D - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bi ...
- uva11292贪心基础题目
C - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bi ...
- hdu 1009 贪心基础题
B - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64bi ...
- 4. K线基础知识
1. K线基础知识 K线又叫阴阳线.蜡烛图.最早由日本米市商人发明,后来推广应用到金融行情价格的分析. K线图的构造主要包含四个价格因素:开盘价.收盘价.最高价.最低价 2. K线图例 收盘价高于开盘 ...
- L贪心基础
<span style="color:#330099;">/* L - 贪心 基础 Time Limit:1000MS Memory Limit:65536KB 64b ...
- Problem K: 搜索基础之棋盘问题
Problem K: 搜索基础之棋盘问题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 92 Solved: 53[Submit][Status][W ...
- BalkanOI 2018 Parentrises(贪心+基础DP)
题意 https://loj.ac/problem/2713 思路 对于 \(\text{P1}\) 的档,首先可以看出 \(O(n^3)\) 的方法,即用 \(O(n^3)\) 的 \(\text{ ...
- - > 贪心基础入门讲解二——活动安排问题
有若干个活动,第i个开始时间和结束时间是[Si,fi),只有一个教室,活动之间不能交叠,求最多安排多少个活动? 分析: 我们就是想提高教室地利用率,尽可能多地安排活动.考虑容易想到的几种贪心策略: ( ...
随机推荐
- layout 自适应详解
@{ ViewBag.Title = "人员查找"; ViewBag.LeftWidth = "200px"; ViewBag.MiddleW ...
- IOS上微信在输入框弹出键盘后,页面不恢复,下方有留白,有弹窗弹出时页面内容感应区域错位
问题说明: ios中,键盘的弹起,页面会往上挪动,使输入框展示在页面中间,键盘隐藏页面会下挪恢复原状. 在微信移动端,ios页面不恢复,下方有留白. 收起键盘的瞬间,如果有弹窗弹出,此时时页面内容应区 ...
- Flume NG基本架构与Flume NG核心概念
导读 Flume NG是一个分布式.可靠.可用的系统,它能够将不同数据源的海量日志数据进行高效收集.聚合.移动,最后存储到一个中心化数据存储系统中. 由原来的Flume OG到现在的Flume NG, ...
- [转]asp.net MVC 常见安全问题及解决方案
本文转自:http://www.cnblogs.com/Jessy/p/3539564.html asp.net MVC 常见安全问题及解决方案 一.CSRF (Cross-site request ...
- Tomcat发布项目,域名访问
域名访问项目 1,去掉访问路径的端口号: 找到 Tomcat 下的 conf 文件中的 server.xml,找到 8080 修改成 80, 2,项目绑定域名: <Host name=" ...
- MySQL关于存储过程
代码示例: 1.IN输入参数: delimiter // create PROCEDURE proc1(IN sid int) begin select * from student where id ...
- JavaScript:常用总结
● 语法要点 ● 引用类型 ● BOM/DOM ● 事件要点 一.语法要点 1.在声明变量时,如果省去了var关键字,那么这个变量为全局变量. 2.数据类型有5种原始类型和9种引用类型: 原始类型中: ...
- jQuery自适应倒计时插件
jQuery自适应倒计时插件 在线演示本地下载
- jQuery——自定义动画
动画方法:animate(json,1000, function (){}) 参数说明:json代表属性设置,1000是动画时间,最后一个是回调函数,其中动画时间可选 属性支持:http://www. ...
- java设计模式02观察者模式
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 这里主要讲一下学习内置观察者的记录,在JA ...