贪心——HDU 1009
来源:互联网 发布:saas平台多租户源码 编辑:程序博客网 时间:2024/05/17 00:57
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1009分析:
给你M份猫粮,和N个房间,每个房间有J[i]份JB,并且需要F[i]份猫粮,如果你给出房间需求猫粮的a%,也将会获得房间内JB的a%,问,怎样才能获得最大份量的JB。题解:
虽然是一道很水的贪心,不过还是有几个坑点:首先我们都会想到用 J/F 得到每个房间的投入和产出比排序来选房间,不过着里需要注意的是F为0的情况,需要单独处理。
还有就是如果一开始M为0,这个时候需要考虑,有没有存在F为0的房间,如果有还是可以继续交换的。
AC代码:
/************************************************************************* > File Name: HDU1009.cpp > Author: Akira > Mail: qaq.febr2.qaq@gmail.com > Created Time: 2016年08月13日 星期六 10时08分21秒 ************************************************************************/#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cmath>#include <vector>#include <set>#include <list>typedef long long LL;typedef unsigned long long ULL;typedef long double LD;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define Sqr(a) ((a)*(a))using namespace std;#define MaxN 100000#define MaxM MaxN*10#define INF 1000000000int M,N;struct room{ int JB; int Food; double rank; bool operator < (const room &a) const { return rank > a.rank; }}R[2000];int main(){ while(~scanf("%d%d", &M, &N)&& ((M+N) != -2)) { for(int i=1;i<=N;i++) { scanf("%d%d", &R[i].JB, &R[i].Food); if(R[i].Food == 0) R[i].rank = INF; else R[i].rank = R[i].JB*1.0/R[i].Food*1.0; } sort(R+1, R+1+N); double ans = 0; int cnt = 1; while( (M || !R[cnt].Food) && cnt<=N) { if(R[cnt].Food == 0) { ans += R[cnt].JB; cnt++; continue; } if(M>=R[cnt].Food) { M -= R[cnt].Food; ans += R[cnt].JB; cnt++; } else { ans += M*R[cnt].rank; M = 0; } } printf("%.3f\n", ans); }}
0 0
- 贪心——HDU 1009
- 贪心—save-hdu
- 水一道贪心——hdu 1009
- 贪心——HDU 5360
- 贪心——HDU 4572
- 贪心——HDU 4415
- 贪心——HDU题目1009 FatMouse' Trade
- HDU-1009 贪心
- HDU 1009 贪心?
- hdu 1009 简单贪心
- HDU 1009 贪心
- HDU 1009(贪心)
- HDU 1009 贪心问题
- HDU 1009贪心
- hdu 1009 贪心
- HDU 1009 贪心
- hdu 1009 贪心
- HDU 1009(贪心)
- Synposys VCS 安装
- leetcode:Trapping Rain Water(JAVA)
- matlab修改m文件(如修改内部自带文件)调出m文件方法
- eclipse(java)常用快捷键
- 机器学习-Python中训练模型的保存和再使用
- 贪心——HDU 1009
- HTTPClient
- Android Mvp+Rxjava+Retrofit实战
- c/c++的时间
- POJ 3169-Layout(差分约束系统-入门裸题)
- xml实现shape,作为背景
- 关于项目部署到linux服务器上拍照图片无法预览问题
- 石油大oj 1825奇怪的电梯 bfs
- Java 循环结构 - for, while 及 do...while