hdu 4803 Poor Warehouse Keeper(贪心)
来源:互联网 发布:win7电脑打不开软件 编辑:程序博客网 时间:2024/05/22 06:51
Poor Warehouse Keeper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5285 Accepted Submission(s): 1356
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?
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.
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 13 89 31
Sample Output
0511HintFor the second test case, one way to achieve is:(1, 1) -> (1, 2) -> (2, 4) -> (2, 5) -> (3, 7.5) -> (3, 8.5)
Source
2013ACM/ICPC亚洲区南京站现场赛——题目重现
思路:有两种情况,一种是给的prices的值小于number的值,这个时候一定是达不到的。输出-1。
而另一种情况:
要使得单件物品价格最快的接近最终要得到的价格,一定要在number的数量还小的时候按第二个按钮,这样单件价格才能加的最快。所以,当最后将此时单件价格累加起来小于等于y和按下第二个按钮之后累加起来的值仍小于等于y,则这时候可以按下第二个按钮。否则只能按下第一个按钮将单件价格降低。如果按完了第一个按钮,还达不到最终结果那么只能按第二个按钮。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){ int x,y; while(~scanf("%d%d",&x,&y)) { int t=x-1; double per=1; double total=1; double g=y*1.0/x; int cnt=0,now=1,flag=0; if(y<x) printf("-1\n"); else { while(t) { if(now==x&&(int)total==y) { flag=1; break; } if((int)(total/now*x)==y) { cnt+=x-now; flag=1; break; } if((int)(total/now*x)<=y&&(int)((total+1)/now*x)<=y)//可以按第二个按钮 { if((int)per<(int)g)//不考虑小数部分 { total+=(int)g-(int)per; cnt+=(int)g-(int)per; per=total/now; } else//考虑小数部分 { total++; cnt++; per=total/now; } } else//只能按第一个按钮 { t--; now++; total+=per; cnt++; } } if(flag==0&&(int)total<y)//now达到了x值但是total没有达到y值,此时就要接着按第二个按钮直到totao达到y值。 { cnt+=y-(int)total; } printf("%d\n",cnt); } } return 0;}
阅读全文
0 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区域赛
- 写一个老是忘记的linux进程pid是哪个?
- 请移步新博客
- 利用asio实现了一个服务器,多个客户端连接,并异常断开连接,发现后面再也连接不上服务器了,不能建立新连接了。原因分析
- linux实验四
- python---之np.newaxis
- hdu 4803 Poor Warehouse Keeper(贪心)
- 玩一玩STM32f103c8t6接SIM900a去连接MQTT服务器(一)免费的MQTT
- 链式栈:从头节点插入和删除
- fabric源码解析22——Orderer服务的初始化
- Java开发快递物流项目(2)
- Hbase入门
- Hive文件存储格式
- linux vim编辑器之常用指令
- 地铁修建