<span style="color:#330099;">/*
K - 贪心 基础
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
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 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. Input
The 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 J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000. Output
For 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
BY Grant Yuan
2014.7.14
贪心
*/
/*
描写叙述
有一个投资人。他有金钱m,可选投资项目n个。对于每一个投资项目,投入金钱c则获得收益f。要求对每一个项目所投入的资金r应该在0到c之间(即0<=r<=c),获得的收益为r*f/c。如今你的任务就是求出投资人能获得的最大收益。 输入
包含多个測例。每一个測例第一行为两个整数m,n,分别表示金钱数和项目个数。接下来n行,每行两个整数为收益f和投入c。输入最后以两个-1结尾。 输出
包含n行,每行为相应測例的最大收益。保留三位小数printf("%.3lf\n",result);。 输入例子
3 3
4 2
2 1
3 1
4 3
4 2
5 3
4 3
-1 -1 输出例子
7.000
7.333
By yuan.c
20146/22 提示
*/
#include<stdio.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
int N;
int M; double p[1002];
int m[1002];
int s[1002];
double sum=0; void sort()
{int t;double l;
for(int i=0;i<N-1;i++)
for(int j=i;j<N;j++)
{
if(p[i]<p[j]){
l=p[i],p[i]=p[j],p[j]=l;
t=m[i],m[i]=m[j],m[j]=t;
t=s[i],s[i]=s[j],s[j]=t;
}
}
} int main()
{
while(1){
scanf("%d %d",&M,&N);
if(N==-1&&M==-1)
break;
for(int i=0;i<N;i++)
scanf("%d %d",&s[i],&m[i]);
for(int i=0;i<N;i++)
p[i]=s[i]*1.0/m[i]*1.0;
sort();
for(int i=0;i<N;i++)
if(M>=m[i])
sum+=s[i],M-=m[i];
else {sum+=M*p[i];
break;
}
printf("%.3lf\n",sum);
sum=0;
memset(s,0,100);
memset(m,0,100);
memset(p,0,100);
}
return 0;
}
</span>

K贪心的更多相关文章

  1. K - 贪心 基础

    FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...

  2. CF623D birthday 贪心 概率期望

    题意:n个人,玩抓人游戏,每抓住一个人都要猜这个人是谁.对于每一局,第i个人有$p_{i}$的概率被抓到.游戏结束当且仅当每个人都在某局中被抓到并且猜中自己的名字,求一个合适的策略来使得期望游戏局数最 ...

  3. Codeforces Round #466 (Div. 2) B. Our Tanya is Crying Out Loud[将n变为1,有两种方式,求最小花费/贪心]

    B. Our Tanya is Crying Out Loud time limit per test 1 second memory limit per test 256 megabytes inp ...

  4. [Codeforces 10E] Greedy Change

    Brief Introduction: 给你一些种类的硬币,用最少的硬币数表示X 求最小的使贪心算法错误的X Algorithm: 一道论文题,<A Polynomial-time Algori ...

  5. WHU 1537 Stones I

    题目见: http://acm.whu.edu.cn/land/problem/detail?problem_id=1537 这个题相当无语,学长给的解法是:枚举取的个数k,然后对每个k贪心,取其中的 ...

  6. POJ 3276 枚举+差分?

    题意: 思路: 先枚举一下k 贪心:如果当前是B那么就翻 差分一下序列 mod2 就OK了 //By SiriusRen #include <cstdio> #include <cs ...

  7. Codeforces #367 (Div. 2) D. Vasiliy's Multiset (trie 树)

    http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/D 题目:就是有3种操作 + x向集合里添加 x - x 删除x元素,(保证存在 ...

  8. django模型操作

    Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表        

  9. 算法导论----贪心算法,删除k个数,使剩下的数字最小

    先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...

随机推荐

  1. 英文长单词断行 word-break VS word-wrap

    你真的了解word-wrap和word-break的区别吗? 这两个东西是什么,我相信至今还有很多人搞不清,只会死记硬背的写一个word-wrap:break-word;word-break:brea ...

  2. 可用与禁用 E:enabled { sRules }

    <!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...

  3. 拉姆达表达式(Lambda Expressions)

    上面两种写法是一样的 ,拉姆达表达式也是一种委托, 但引用的是匿名方法

  4. c# 取得扩展名

    string KZM=files[0].FileName.Substring(files[0].FileName.LastIndexOf(".") + 1);

  5. Lucene文件扩展名

    名称 文件后缀 描述 段文件(Segments File) segments.gen segments_N 存储提交点信息 锁文件(Lock File) write.lock 用来阻止多个indexW ...

  6. mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in

    从字面的意思上说:这个函数要被弃用,请使用 mysqlli 或者是 PDO 代替 然后就查手册发现没说,大家一定要查官方最新的手册

  7. php中的MVC模式运用

    [size=5][color=Red]php中的MVC模式运用[/color][/size] 首先我来举个例子: 一个简单的文章显示系统 简单期间,我们假定这个文章系统是只读的,也就是说这个例子将不涉 ...

  8. [转载]移动终端浏览器初始设置apple-mobile-web-app-capable

    这两句话的确很有用,有了它,手机访问的时候像样了. 原文地址:移动终端浏览器初始设置apple-mobile-web-app-capable作者:素水凌心 移动终端浏览器默认设置视口的宽度和初始规模. ...

  9. Scala学习笔记--List、ListBuffer

    ListBuffer(列表缓冲) ListBuffer类位于  scala.collection.mutable.ListBuffer val buf = new ListBuffer[Int] va ...

  10. 《Programming WPF》翻译 第7章 4.转换

    原文:<Programming WPF>翻译 第7章 4.转换 支持高分辨率显示是WPF中的重要样式.这是被部分地支持--强调了可伸缩的向量图,而不是图像.但是,正如使用GDI+和GDI3 ...