Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >HDU 1009 FatMouse' Trade(贪心)

HDU 1009 FatMouse' Trade(贪心)

作者头像
Cell
发布于 2022-02-25 08:41:24
发布于 2022-02-25 08:41:24
21200
代码可运行
举报
文章被收录于专栏:Cell的前端专栏Cell的前端专栏
运行总次数:0
代码可运行

题目大意:

题目链接 老鼠有 M 磅猫食 , 有 N 个房间 , 每个房间前有一只猫 , 房间里有老鼠最喜欢的食品 J[i] , 若要得到房间的食物 , 必须付出相应的猫食 F[i] , 当然这只老鼠没必要每次都付出所有的 F[i],若它付出 F[i] 的 a%, 则得到 J[i] 的 a%,求老鼠能吃到的最多的食物。

Sample Input

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

Sample Output

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
13.333
31.500

分析

老鼠要用最少的猫粮来换取最多的食物 , 也就是 J[i]/F[i] 越大越好 , 所以按照 J[i]/F[i] 进行降序排列 , 然后依次用猫粮来换取食物 , 当所剩下的猫粮不足以完全换取食物 , 能换多少是多少。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

#include<stdio.h> #include<algorithm> using namespace std; struct node{ double j; double f; double s; }a[1005]; int cmp(node x,node y){ return x.s>y.s; } int main(){ int m,n,i; while(scanf("%d%d",&m,&n)&&(m!=-1&&n!=-1)){ memset(a,0,sizeof(a)); for(i=0;i<n;i++){ scanf("%lf%lf",&a[i].j,&a[i].f); a[i].s=a[i].j/a[i].f; } sort(a,a+n,cmp); double sum=0; for(i=0;i<n;i++){ if(m>=a[i].f){ sum+=a[i].j; m-=a[i].f; }else{ sum+=a[i].s*m; m=0; } if(m<=0) break; } printf("%.3lf\n",sum); } return 0; }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【HDU 1009】FatMouse' Trade
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.
饶文津
2020/05/31
7190
FatMouse&#39; Trade(杭电1009)
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.
全栈程序员站长
2022/07/07
2440
贪心-HDU 1009
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.
ACM算法日常
2018/08/07
2890
hdu1009
#include <stdio.h> #include <algorithm> using namespace std; struct Node { double j,f,p; } node[10000]; int cmp(Node x,Node y) { return x.p>y.p; } int main() { int m,n; while(~scanf("%d%d",&n,&m) && (m!=-1 || n!=-1)) { double su
@坤的
2018/06/04
4970
HDOJ 1009(贪心)
Problem 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.
dejavu1zz
2020/10/23
3140
Hdu 1009 FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 43156    Accepted Submission(s): 14417
熙世
2019/07/15
5420
hdu 4033Regular Polygon(二分+余弦定理)
Regular Polygon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3274    Accepted Submission(s): 996 Problem Description In a 2_D plane, there is a point strictly in a regular polygon with N side
Gxjun
2018/03/26
6410
你听过算法也是可以贪心的吗?
贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解。 贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关。 基本思路 1、建立数学模型来描述问题; 2、把求解的问题分成若干个子问题; 3、对每一子问题求解,得到子问题的局部最优解; 4、把子问题的解局部最优解合成原来解问题的一个解。 算法实现 1、从问题的某个初
用户1332428
2018/03/08
1.2K0
你听过算法也是可以贪心的吗?
浙大版《C语言程序设计(第3版)》题目集 11~20
本题要求编写程序,计算序列 1 + 1/3 + 1/5 + ... 的前N项之和。
C you again
2022/08/22
1.2K1
浙大版《C语言程序设计(第3版)》题目集 11~20
ZOJ 3194 Coverage(贪心)
题意:对于题目给的点,x固定,而与x组合的y可以任意交换,求如何安置y可使这些点组成线段下面的面积最大,最大面积是多少 分析:可以发现Xn-Xn-1的越大那么乘以y越大,所以我们只需求出,然后ΔX越大的数和y越大的数相乘在除以2就是结果,通过画图很容易得出结论          但是还有一个问题就是,对于i=0,i=n-1,Yi只乘以了一遍,而对于0<i<n的区间,每个Yi都乘以了两遍          所以在求ΔX时候,当i=0,ΔX=X1-X0,当i=n-1时,ΔX=Xn-1-Xn-2,而对于0<i<n
用户1624346
2018/04/17
4720
HDU3853
Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help her friend Madoka
attack
2018/04/11
5800
HDU3853
HDU 2689 Sort it【树状数组】
Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4672    Accepted Submission(s): 3244 Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent se
Angel_Kitty
2018/04/09
9070
数据结构期末52道OnlineJudge机试题目考核-大二上
<!--more--> <center> <a href="https://s2.ax1x.com/2019/12/28/lmEb9K.png" class="highslide" onclick="return hs.expand(this,{slideshowGroup:'images'})"><img src="https://s2.ax1x.com/2019/12/28/lmEb9K.png" alt="lmEb9K.png" border="0" /></a></center> **所有题目全部在杭电OJ完成:**[http://acm.hdu.edu.cn/](http://acm.hdu.edu.cn/) ```c //1000 A + B Problem #include<iostream> using namespace std; int main(){ int a,b,sum; while(cin>>a>>b){ sum = 0; sum = a + b; cout<<sum<<endl; } } ``` ```c //1005 Number Sequence #include <iostream> int A,B; int f(int n){ if( n == 1 || n == 2){ return 1; } return (A*f(n-1)+B*f(n-2))%7; } int main(){ int n; while(scanf("%d%d%d",&A,&B,&n)!=EOF,A||B||n){ int a = f(n%49); printf("%d\n",a); } } ``` ```c //1008 Elevator #include<iostream> using namespace std; int main(){ int n,a[100],i,sum; while(cin>>n&&n!=0){ sum = 0; for(i=0;i<n;i++){ cin>>a[i]; } if(n==1){ sum = a[0]*6+5; } else{ sum = n*5+a[0]*6; for(i=0;i<n-1;i++){ if(a[i+1]>a[i]){ sum += (a[i+1]-a[i])*6; } else{ sum += (a[i]-a[i+1])*4; } } } cout<<sum<<endl; } } ``` ```c //1012 u Calculate e #include<stdio.h> int main() { double e; int i, tmp; e = 1; tmp = 1; printf("n e\n"); printf("- -----------\n"); printf("0 1\n"); for(i = 1; i < 10; i++) { tmp *= i; e += 1.0/tmp; if(i>=3) printf("%d %.9lf\n", i, e); else if(2 == i) printf("%d %.1lf\n", i, e); else printf("%d %.0lf\n",i, e); } } ``` ```c //1032 The 3n + 1 problem #include <iostream> using namespace std; int main() { int a,b,t,i,max; while(cin >> a >> b) { cout << a << " " << b << " "; if(a>b)//大小不确定 { t = a; a = b; b = t; } max = 0;
王荣胜
2020/03/12
7030
算法修炼之练气篇——练气十三层
公式:求a的平方根的迭代公式为: X[n+1]=(X[n]+a/X[n])/2 要求前后两次求出的差的绝对值少于0.00001。 输出保留3位小数
命运之光
2024/03/20
1370
Hdu 1789 Doing Homework again
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6538    Accepted Submission(s): 3900
熙世
2019/07/15
4410
hdu 3853LOOPS (概率DP)
LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2552    Accepted Submission(s): 1041 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help her
Gxjun
2018/03/26
6500
hdu 3853LOOPS (概率DP)
Applications (ZOJ 3705)
题解:就是题目有点小长而已,可能会不想读题,但是题意蛮好理解的,就是根据条件模拟,计算pts。(送给队友zm、 qsh,你们不适合训练了。)
Lokinli
2023/03/09
2670
HDU-1394 Minimum Inversion Number(线段树求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15675 Accepted Submission(s): 9569 Problem Description The inversion number of a given number sequence a1, a2, …, an is
ShenduCC
2018/04/25
1K0
HDU 2955 Robberies
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117347.html原文链接:https://javaforall.cn
全栈程序员站长
2022/07/06
1950
Max Sum(优化)- HDU 1003
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
ACM算法日常
2018/08/07
3830
Max Sum(优化)- HDU 1003
相关推荐
【HDU 1009】FatMouse' Trade
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验