<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. Sybase安装后的配置工作

    1.配置数据库参数 配置sybase数据库使用的最大内存 用isql命令行实用工具登录sybase数据库服务器,其中的servername是$SYBASE/interfaces文件中配置的sybase ...

  2. [转]iOS hybrid App 的实现原理及性能监测

    转自:http://www.cocoachina.com/ios/20151118/14270.html iOS hybrid App 的实现原理及性能监测 2015-11-18 11:39 编辑:  ...

  3. ASP.net获取当前页面的文件名,参数,域名等方法

    ASP.net后台获取当前页面的文件名 System.IO.Path.GetFileName(Request.Path).ToString(); 获取当前页面文件名,参数,域名等方法 假设当前页完整地 ...

  4. .Net的PDF转图片

    用的是破解版的 O2S.Components.PDFRender4NET.dll 插件, 简单引用即可 public static class PdfToImage { , , ) { try { / ...

  5. Java数据结构漫谈-ArrayList

    ArrayList是一个基于数组实现的链表(List),这一点可以从源码中看出: transient Object[] elementData; // non-private to simplify ...

  6. java 静态文件使用注解

    spring框架为我们代码的编写带来了极大的便利,特别是注解的使用.但是有个问题,当我们在静态文件中使用注解的时候,这个时候就会报错了.如以下代码: @Autowired private UserSe ...

  7. ios面试汇总

    http://www.360doc.com/content/15/0707/01/26281448_483232245.shtml

  8. 定义block块

    一: 工程图 二: 代码区 AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <U ...

  9. (原)torch中显示nn.Sequential()网络的详细情况

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6065526.html 本部分多试几次就可以弄得清每一层具体怎么访问了. step1. 网络定义如下: ...

  10. [Mugeda HTML5技术教程之12]制作跨屏互动应用

    mugeda动画平台还可以用来制作跨屏互动的动画应用,比如在PC端的大屏幕上显示动画的主界面,同时会显示出供手机扫描的二维码,手机扫描后会在手机上显示手机端动画界面.通过手机就可以和PC端的显示界面跨 ...