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),只有一个教室,活动之间不能交叠,求最多安排多少个活动? 分析: 我们就是想提高教室地利用率,尽可能多地安排活动.考虑容易想到的几种贪心策略: ( ...
随机推荐
- PCB Genesis脚本 C#调用Javascript
曾经用node.js测试写Genesis脚本失败了,这次借助开发PCB规则引擎的机会(基于JS V8引擎与.net深度交互性), 验证一下Javascript是否可用于写Genesis脚本. 一.测试 ...
- Linux基础命令第二波
第1章 Linux启动过程 开机自检(BIOS)##硬件检查 MBR引导 GRUB菜单(选择不同的内核) 加载内核 运行init进程(Linux系统里面第一个进程) 读取/etc/inittab配置文 ...
- ACM_Ruin of Titanic(简单贪心)
Ruin of Titanic Time Limit: 2000/1000ms (Java/Others) Problem Description: 看完Titanic后,小G做了一个梦.梦见当泰坦尼 ...
- .net framework 3.5 安装报错 0x800F0954问题
windows Server 2019 .net framework 3.5 安装报错 0x800F0954问题 .net framework 3.5的安装教程:但是安装出现0x800F0954这个错 ...
- javascript特殊值常量
Infinity 表示无穷大的特殊值.当数字运算结果超出javascript能表示的数字范围时. Nan 特殊的非数字值(not a number).0除0.数字运算符的操作数为字符等情况. Numb ...
- 精美对UI设计界面赏析
最美的UI设计界面赏析 . 喜欢就关注我吧
- Ubuntu14.4安装mysql
一.安装 apt-get install mysql-server mysql-client 设置用户名和密码 二.检查 sudo service mysql restart 三.支持 1.apach ...
- 【sqli-labs】 less60 GET -Challenge -Double Query -5 queries allowed -Variation3 (GET型 挑战 双查询 只允许5次查询 变化3)
http://192.168.136.128/sqli-labs-master/Less-60/?id=1")%23 http://192.168.136.128/sqli-labs-mas ...
- C# 获得固定年月日
/// <summary> /// 获得固定年月日,时和分不固定 : 2019-01-01 00:00:00 /// </summary> /// <returns> ...
- 小程序 之picker-view省市县
代码地址:https://github.com/yangsphp/area-picker