K贪心
<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贪心的更多相关文章
- K - 贪心 基础
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...
- CF623D birthday 贪心 概率期望
题意:n个人,玩抓人游戏,每抓住一个人都要猜这个人是谁.对于每一局,第i个人有$p_{i}$的概率被抓到.游戏结束当且仅当每个人都在某局中被抓到并且猜中自己的名字,求一个合适的策略来使得期望游戏局数最 ...
- 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 ...
- [Codeforces 10E] Greedy Change
Brief Introduction: 给你一些种类的硬币,用最少的硬币数表示X 求最小的使贪心算法错误的X Algorithm: 一道论文题,<A Polynomial-time Algori ...
- WHU 1537 Stones I
题目见: http://acm.whu.edu.cn/land/problem/detail?problem_id=1537 这个题相当无语,学长给的解法是:枚举取的个数k,然后对每个k贪心,取其中的 ...
- POJ 3276 枚举+差分?
题意: 思路: 先枚举一下k 贪心:如果当前是B那么就翻 差分一下序列 mod2 就OK了 //By SiriusRen #include <cstdio> #include <cs ...
- Codeforces #367 (Div. 2) D. Vasiliy's Multiset (trie 树)
http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/D 题目:就是有3种操作 + x向集合里添加 x - x 删除x元素,(保证存在 ...
- django模型操作
Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表
- 算法导论----贪心算法,删除k个数,使剩下的数字最小
先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...
随机推荐
- ListVeiw新增记录及 滚动条移动到指定位置
C# 自带的ListView控件的滚动条移动到指定位置. lvwList为ListView控件 lvwList.EnsureVisible(lvwList.Items.Count - 1); 新增记录 ...
- F# 越用越喜欢
F# 越用越喜欢 最近由于需要,把遗忘了几年的F#又捡了起来.说捡了起来,倒不如说是从头学习,原来学的早已经忘了!所谓学过,只不过看过一本<F# 语言程序设计> (郑宇军 凌海风 编著 - ...
- WebApi2官网学习记录---Tracing
安装追踪用的包 Install-Package Microsoft.AspNet.WebApi.Tracing Update-Package Microsoft.AspNet.WebApi.WebHo ...
- hdu 5671 矩阵变换
Matrix Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- 【JAVA编码】 JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
http://blog.csdn.net/qinysong/article/details/1179489 这两天抽时间又总结/整理了一下各种编码的实际编码方式,和在Java应用中的使用情况,在这里记 ...
- setTimeout与setInterval方法的区别
setTimeout与setInterval方法的区别 setTimeout()用于设定在指定的时间之后执行对应的函数或代码.,在全局作用域下执行 setTimeout(code,time[,args ...
- 关于 addEventListener 和 handleEvent 方法
使用 addEventListener 可以绑定事件,并传入回调函数. Mozilla 0.9.1 和 Netscape 6.1 之后的版本不但支持传递函数引用,也都允许直接把拥有 handleEve ...
- 利用Azure Redis Cache构建百万量级缓存读写
Redis是一个非常流行的基于内存的,低延迟,高吞吐量的key/value数据存储,被广泛用于数据库缓存,session的管理,热数据高速访问,甚至作为数据库方式提高应用程序可扩展性,吞吐量,和实施处 ...
- Linux 的系统运行级别
运行级别 说明 0 系统关机状态 1 单用户工作状态,用于root对系统进行维护,此时不予许其他用户使用主机.(类似于windows 的安全模式) 2 ...
- 关于Fragment与Activity的想法
View,Fragment,Activity,ListView等都会涉及到Layout文件 不要从Layout来考虑,而是从Activity,Fragment,来考虑,Layout只是他们的一个属性 ...