FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. 
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 - 贪心 基础的更多相关文章

  1. K贪心

    <span style="color:#330099;">/* K - 贪心 基础 Time Limit:1000MS Memory Limit:32768KB 64b ...

  2. poj2709 贪心基础

    D - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...

  3. uva11292贪心基础题目

    C - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...

  4. hdu 1009 贪心基础题

    B - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bi ...

  5. 4. K线基础知识

    1. K线基础知识 K线又叫阴阳线.蜡烛图.最早由日本米市商人发明,后来推广应用到金融行情价格的分析. K线图的构造主要包含四个价格因素:开盘价.收盘价.最高价.最低价 2. K线图例 收盘价高于开盘 ...

  6. L贪心基础

    <span style="color:#330099;">/* L - 贪心 基础 Time Limit:1000MS Memory Limit:65536KB 64b ...

  7. Problem K: 搜索基础之棋盘问题

    Problem K: 搜索基础之棋盘问题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 92  Solved: 53[Submit][Status][W ...

  8. BalkanOI 2018 Parentrises(贪心+基础DP)

    题意 https://loj.ac/problem/2713 思路 对于 \(\text{P1}\) 的档,首先可以看出 \(O(n^3)\) 的方法,即用 \(O(n^3)\) 的 \(\text{ ...

  9. - > 贪心基础入门讲解二——活动安排问题

    有若干个活动,第i个开始时间和结束时间是[Si,fi),只有一个教室,活动之间不能交叠,求最多安排多少个活动? 分析: 我们就是想提高教室地利用率,尽可能多地安排活动.考虑容易想到的几种贪心策略: ( ...

随机推荐

  1. Counterfeit Dollar

    http://poj.org/problem?id=1013 #include<stdio.h> #include<string.h> #include<math.h&g ...

  2. php的session

    来源:http://blog.163.com/lgh_2002/blog/static/4401752620105246517509/ http协议是WEB服务器与客户 端(浏览器)相互通信的协议,它 ...

  3. 【题解】动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393]

    [题解]动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393] 水一水QAQ 题目链接: \([P3157]\) \([BZOJ3295]\) [题目描述] 对于一个序 ...

  4. 不定长数组 Vector的 应用

    #include<cstdio> #include<vector> using namespace std; vector<int>a; int main() { ...

  5. UNIX环境高级编程--6

    系统数据文件和信息    数据文件都是ASCII文本文件,并且使用标准I/O库读这些文件,例如口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件.    口 ...

  6. java.net.URISyntaxException: Illegal character in query

    java使用httpclient爬取一个网站的时候,请求:String url3="http://sh.58.com/ershoufang/33562546149042x.shtml?amp ...

  7. 入门开发工具idea常见问题之选项中没有servlet

    1.在maven中如果创建不了servlet,在project Setting旁边的添加一个web选项,就可以创建servlet了. 初次接触这个陌生的工具还是不太好弄.

  8. CentOS6.5下简单的MySQL数据库操作

    1.登录成功之后退出的话,直接输入quit或者exit即可.

  9. Leetcode0143--Reorder List 链表重排

    [转载请注明]https://www.cnblogs.com/igoslly/p/9351564.html 具体的图示可查看 链接 代码一 /** * Definition for singly-li ...

  10. Appium Python API 汇总

    最近在学习Python自动化,网络搜集而来,留着备用, 方便自己也方便他人.感谢总结的人! 1.contexts contexts(self): Returns the contexts within ...