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),只有一个教室,活动之间不能交叠,求最多安排多少个活动? 分析: 我们就是想提高教室地利用率,尽可能多地安排活动.考虑容易想到的几种贪心策略: ( ...
随机推荐
- Mysql 数据库编码问题
数据库建表后,插入数据,如果数据位汉子,将提示错误:“incorrect string value....”. 解决方法:改变数据库编码 第一种方法:改变database 编码: alter data ...
- linux下打开windows txt文件中文乱码问题 (转载)
转自:http://blog.csdn.net/imyang2007/article/details/7448177 在linux操作系统下,我们有时打开在windows下的txt文件,发现在wind ...
- P4046 [JSOI2010]快递服务
传送门 很容易想出\(O(n^3m)\)的方程,三维分别表示某个快递员现在在哪里,然后直接递推即可 然而这样会T,考虑怎么优化.我们发现每一天的时候都有一个快递员的位置是确定的,即在前一天要到的位置. ...
- akka监控
使用akka系统时间就了,你就一定会想着监控的事儿.比如某个actor发送了多少消息.接收了多少消息.消息平均处理时间是多少,当前有多少个actor等等.本来我都用bytebuddy写了个简单的akk ...
- Kafka详解与总结(三)
Kafka分片存储机制 几个kafka重要概念: Broker:消息中间件处理结点,一个Kafka节点就是一个broker,多个broker可以组成一个Kafka集群. Topic:一类消息,例如pa ...
- 如何读取 Json 格式文件
Json 源文件代码: [ { "Id": "0", "Name": "书籍", "Detail": ...
- Unicode ,UTF-8,assic, gbk, latin1编码 的区别
1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...
- Zookeeper概念学习系列之zookeeper的数据模型
1.层次化的目录结构,命名符合常规文件系统规范. 2.每个节点在zookeeper中叫做znode,并且有其有一个唯一的路径标识. 3.znode中的数据可以有多个版本,比如某一路径下存有多个数据版本 ...
- 使用adb命令提示端口被占用
图是我的65535端口被占用了,一般adb默认使用的是5037端口##方式一 5037为adb默认端口,若5037端口被占用,查看占用端口的进程(使用命令netstat -aon|findstr ...
- js获取某年某月一共多少天
const getDaysInMonth = (year, month) => { let date = new Date(year, month, 1); return new Date(da ...