hdu 4803 Poor Warehouse Keeper (贪心思维)
2017-10-31 10:55
513 查看
Poor Warehouse Keeper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5117 Accepted Submission(s): 1313
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;通过两个按钮来改变1、1,使其能够变成x,y;注意只要整数位变成即可(题目中有描述)。第一个按钮:将x增加1,y按照原先y/x比例来增加;第二个按钮,将y增加1。
起初是想模拟来着,但是借鉴了一下别人的思路,其实就是贪心,1到x必定会经过x-1步,至于y的变化,每当有新的x诞生,y也要根据一个上限来增加,上限就是((y+0.99999)/x);
详见代码:
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main() { int x,y,step; double x0,y0; while(~scanf("%d%d",&x,&y)) { if(x>y) { printf("-1\n"); continue; } x0=y0=1; double bei=(double)(y+0.99999)/x; step=(int)(x-x0); for(int i=1;i<=x;i++) { int dis=(int)(i*bei-y0); step+=dis; y0+=dis; y0=y0/i*(i+1); } 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(贪心+注意精度) 13区域赛
- Hdu 4803 Poor Warehouse Keeper(从1,1变为x,y,精度+思维)
- HDU 4803 Poor Warehouse Keeper(贪心)
- [hdu 4803]Poor Warehouse Keeper 进制相关 贪心
- hdu 4803 Poor Warehouse Keeper(贪心+数学)
- HDU 4803 Poor Warehouse Keeper(贪心)
- Poor Warehouse Keeper hdu 4803(思维,卡精度)
- HDU 4803 Poor Warehouse Keeper 模拟
- HDU 4803 Poor Warehouse Keeper
- hdu 4803B - Poor Warehouse Keeper
- hdu 4803_Poor Warehouse Keeper