<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. AngularJS移动开发中的坑汇总

    使用AngualrJs开发移动App已经快半年了,逐渐积累了非常多AngularJS的问题,特别是对于用惯了Jquery的开发人员,转到AngularJS还是须要克服非常多问题的.不像Jquery那样 ...

  2. 安装sql server提示挂起报错

    在安装sql server时出现“以前的某个程序安装已在安装计算机上创建挂起的文件操作.运行安装程序之前必须重新启动计算机”错误.无法进行下去. 参考有关资料后,以下步骤基本可以解决: 1)添加/删除 ...

  3. 使用VTemplate模板引擎动态生成订单流程图

    1.VTemplate模板引擎的简介 VTemplate模板引擎也简称为VT,是基于.NET的模板引擎,它允许任何人使用简单的类似HTML语法的模板语言来引用.NET里定义的对象.当VTemplate ...

  4. 新闻滚动marquee标签

    先上代码: <marquee behavior="" direction="up" onMouseOver="this.stop()" ...

  5. scala中的implict

    1.作为隐式参数 object Test { def main(args: Array[String]) { import FruitColor._ Fruit.get("apple&quo ...

  6. HTMl5的sessionStorage和localStorage(转)

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...

  7. js获取浏览器滚动条距离顶端的距离

    最近在做项目的时候遇到需要用js获取滚动条距离窗口顶端的距离和js获取浏览器可视化窗口的大小,在这儿做一个整理保存:    一.jQuery获取的相关方法 jquery 获取滚动条高度 获取浏览器显示 ...

  8. Linux命令:TOP

    top命令 是Linux下常用的性能 分析工具 ,能够实时显示系统 中各个进程的资源占用状况,类似于Windows的任务管理 器.下面详细介绍它的使用方法. top - 02:53:32 up 16 ...

  9. (五)CodeMirror - 关于htmlmixed中包含script脚本

    最近发现个问题,场景如下: 当创建的mode类型为htmlmixed,且内容中包含javascript脚本,且是闭包立即执行: 如果内容是使用JQuery函数.html()插入到DOM中后再创建cod ...

  10. 视觉差效果 - jqyery scrollTop原理

    原理是用页面的滚动高度scrollTop()来控制背景图的位置 附上源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...