HDU 4803 Poor Warehouse Keeper(贪心)
来源:互联网 发布:dnf辅助端口免费制卡 编辑:程序博客网 时间:2024/05/22 10:33
Poor Warehouse Keeper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5298 Accepted Submission(s): 1363
Problem Description
Jenny is a warehouse keeper. He writes down the entry records everyday. The record is shown on a screen, as follow:
There are only two buttons on the screen. Pressing the button in the first line once increases the number on the first line by 1. The cost per unit remains untouched. For the screen above, after the button in the first line is pressed, the screen will be:
The exact total price is 7.5, but on the screen, only the integral part 7 is shown.
Pressing the button in the second line once increases the number on the second line by 1. The number in the first line remains untouched. For the screen above, after the button in the second line is pressed, the screen will be:
Remember the exact total price is 8.5, but on the screen, only the integral part 8 is shown.
A new record will be like the following:
At that moment, the total price is exact 1.0.
Jenny expects a final screen in form of:
Where x and y are previously given.
What’s the minimal number of pressing of buttons Jenny needs to achieve his goal?
Input
There are several (about 50, 000) test cases, please process till EOF.
Each test case contains one line with two integers x(1 <= x <= 10) and y(1 <= y <= 109) separated by a single space - the expected number shown on the screen in the end.
Output
For each test case, print the minimal number of pressing of the buttons, or “-1”(without quotes) if there’s no way to achieve his goal.
Sample Input
1 1
3 8
9 31
Sample Output
0
5
11
Hint
For the second test case, one way to achieve is:
(1, 1) -> (1, 2) -> (2, 4) -> (2, 5) -> (3, 7.5) -> (3, 8.5)
题意
有一个显示屏上有两个按钮和显示,x代表商品数量,y代表总价(y只显示整数部分),总价=单价x商品数量,一开始x=y=1(商品单价为1),我们可以对按钮进行操作,当点击x时,商品数量+1,总价也会随着变化,此时商品单价不发生变化,当点击y时总价+1,此时商品数量保持不变,所以商品单价会增加
题目给你最终的x,y,问最少进行多少次操作能达到x,y
思路
可以利用贪心的思想,每次将单价增加趋近于目标单价,改变x时,单价不变,所以只有在调整y时才能改变单价,那么可以列出基于每次的i(数量)和j(总价)时j能增加的最大值
即
那么
max取比
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define esp 0.000001using namespace std;int main(){ int x,y; while(scanf("%d%d",&x,&y)!=EOF) { double i,j=1; double now=1; int step=0; int Max; for(i=1;i<=x;i++,step++) { j=now*i; if(j>=y&&j<y+1) break; for(;j<y+1;) { Max=(y+1)*i/x-j-esp; now=j/i; if((j+1)/i*x>=y+1) break; j+=Max; step+=Max; if(j>=y&&j<y+1) goto k; } } k: if(x>y) printf("-1\n"); else printf("%d\n",step); } return 0;}
- HDU 4803 Poor Warehouse Keeper(贪心)
- hdu 4803 Poor Warehouse Keeper(贪心+数学)
- HDU - 4803 Poor Warehouse Keeper(贪心)
- HDU 4803 Poor Warehouse Keeper(贪心)
- [HDU 4803]Poor Warehouse Keeper[贪心]
- hdu 4803 Poor Warehouse Keeper(贪心)
- hdu 4803 Poor Warehouse Keeper (贪心思维)
- hdu 4803 Poor Warehouse Keeper(贪心)
- HDU 4803 Poor Warehouse Keeper(贪心)
- HDU 4803 Poor Warehouse Keeper
- HDU 4803 Poor Warehouse Keeper
- HDU 4803 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(贪心+注意精度) 13区域赛
- java XML解析技术
- 【代码笔记】【MySQL】 合并两个结果集
- 菜鸟学习历程【15-6】堆排序、归并排序
- [vsim]vsim 不是内部或外部命令,也不是可运行的程序或批处理文件
- 判断是否微信访问
- HDU 4803 Poor Warehouse Keeper(贪心)
- idea 中使用lombok
- 记录下android推荐集合
- elasticsearch-java api之文档(document)各种操作
- vue-router 之命名路由
- 查询,修改 数据库
- SourceTree的基本使用
- 基础练习 杨辉三角形
- 特权同学的异步复位,同步处理方法总结