<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. [置顶] ./build_native 时出现please define NDK_ROOT

    在一次帮朋友弄cygwin交叉编译时出现了这个问题 cygwin是按照成功了,make-v,以及gcc-v都没出现问题,就是在./build_native 时出现please define NDK_R ...

  2. 用SNMP协议实现系统信息监控--Windows Server 2008

    简单了解: SNMP简单网络管理协议,是一种属于应有层的协议,主要有三个部分组成,被管理部分.代理部分和网络管理系统. 被管理部分是一个网络节点,也称为网络单元.SNMP代理是被管理设备上的一个网络管 ...

  3. AngularJs练习Demo8 自定义过滤器

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  4. 简单描述一下XIB与Storyboards,简述它们的优缺点。

    参考答案: 我倾向于纯代码开发,因此所提供的参考答案可能具有一定的个人感情,不过还是给大家说说自己的想法. 优点: XIB:在编译前就提供了可视化界面,可以直接拖控件,也可以直接给控件添加约束,更直观 ...

  5. 利用java Base64 实现加密、解密

    Base64加密解密 package com.stone.util; import java.io.UnsupportedEncodingException; import sun.misc.*; p ...

  6. CentOS磁盘分区、格式化并挂载外置存储的方法

    1.划分外置存储主分区: 假设该外置存储在linux系统中被识别为/dev/sdb,使用fdisk /dev/sdb进入分区工具操作界面. 按p键打印当前外置存储分区情况,如果显示为空,则表示此外置存 ...

  7. 分布式Session共享(二):tomcat+memcached实现session共享

    一.前言 本文主要测试memcached实现session共享的实现方式,不讨论如何让nginx参与实现负载均衡等. 二.环境配置 本测试在Window下进行 name version port To ...

  8. HLS(HTTP Live Streaming)协议之m3u8文件生成方式

    HLS(HTTP Live Streaming)是Apple的动态码率自适应技术.主要用于PC和Apple终端的音视频服务.包括一个m3u(8)的索引文件,TS媒体分片文件和key加密串文件. HLS ...

  9. 关于extern对变量的使用

    extern 是声明全局的变量的意思. 例如在一个工程中有两个cpp,一个是test.cpp一个是main.cpp . 我们在test.cpp中定义了一个int num;但是我们在main.cpp中想 ...

  10. vc2015编译protobuf

    下载地址:https://github.com/google/protobuf 1.编译通过cmake生成sln文件来编译用cmd命令 cd 到3.0.0-beta-4\cmake mkdir bui ...