BNUOJ 34025 -Poor Warehouse Keeper(贪心)
来源:互联网 发布:中兴端口查询mac 编辑:程序博客网 时间:2024/05/18 01:32
题目:BNUOJ 34025 -Poor Warehouse Keeper(贪心)
题目大意:有一个商品的信息表,上面是数量,下面是总价,然后旁边各有一个按钮。上面的数量按钮按一下数量就加1,然后价格对应的也要在加上一个当前的单价。下面的按钮按一下的话,就对应的总价加1.初始状态是 1 1,然后给出终点状态,问能否得到。可以的话输出最少要按几次按钮,否则输出-1;总价每次输出都是下取整。
解题思路:如果想要尽快的得到总点状态的值,那么就应该按总价的按钮,因为只有总价变大了,商品对因的单价就上升了,这样总价格下次数量变动就能更快的到达要求的值。每次都增加总价,一直加到一个最大值(超过这个值后,数量到达要求的大小,总价值就会超过要求的总价值),然后再去变动商品的个数。
然后就是求这个最大值f, 由题意可得 f1(当前商品的总价) / i(当前商品的个数) * x(要求的商品个数) < y(要求的总价) + 1。 那么 f (最大值)* i / x = (y - 10^-9 + 1)(小于y - 1的近似最大值)。这样当前数量的极限的总价值也找出来了。这样避免去模拟,这题的数据量是10 ^ 9, 去模拟就会超时了。
然后找不到的情况,只有当初始给定的 x > y, 其余的都是可以找到的。
代码:
#include <stdio.h>const double esp = 1e-9;int main () {double x, y;while (scanf ("%lf%lf", &x, &y) == 2) {if (x > y) printf ("-1\n");else {double s, sum, max;int count;s = (y + 1 - esp) / x;sum = 1;count = (int)x - 1;int k;for (int i = 1; i <= (int) x; i++) {max = s * i;k = (int) (max - sum);sum += k;count += k;//printf ("%lf\n", sum);sum = sum * (i + 1) / i;}printf ("%d\n", count);}}return 0;}
0 0
- BNUOJ 34025 -Poor Warehouse Keeper(贪心)
- HDU 4803 Poor Warehouse Keeper(贪心)
- hdu 4803 Poor Warehouse Keeper(贪心)
- HDU4803 Poor Warehouse Keeper【贪心】
- HDU 4803 Poor Warehouse Keeper(贪心)
- hdu 4803 Poor Warehouse Keeper(贪心+数学)
- HDU - 4803 Poor Warehouse Keeper(贪心)
- 【HDU4803】Poor Warehouse Keeper 数学+贪心
- [HDU 4803]Poor Warehouse Keeper[贪心]
- 【HDU4803】Poor Warehouse Keeper(贪心 + 模拟)
- hdu 4803 Poor Warehouse Keeper (贪心思维)
- hdu 4803 Poor Warehouse Keeper(贪心)
- HDU 4803 Poor Warehouse Keeper(贪心)
- HDU 4803Poor Warehouse Keeper(数学题)
- HDU4803 Poor Warehouse Keeper
- HDU 4803 Poor Warehouse Keeper 贪心 二分搜索
- [hdu 4803]Poor Warehouse Keeper 进制相关 贪心
- 【2013南京现场赛】1002 hdu4803 Poor Warehouse Keeper 贪心
- 堆和栈的区别 (转贴)
- android API文档下载地址
- Fragment 选项卡
- 开发指南专题八:JEECG微云快速开发平台数据字典
- 长沙拓展训练基地
- BNUOJ 34025 -Poor Warehouse Keeper(贪心)
- Fragment 和 Fragment 之间的通讯
- XTU1176:I Love Military Chess
- Hive的安装和配置详解
- MFC控件(三)(进度条控件和滑块控件)
- 不要推荐到首页
- Regular Expression Matching
- Fragment FragmentActivity Activity 之间的区别
- 并发技术