hdu 4803 贪心
来源:互联网 发布:中国网络发展史插画 编辑:程序博客网 时间:2024/05/23 05:07
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 Sample Output Hint
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?
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.
1 13 89 31
0511
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变为x1,y1,由题意知,每次变化单价不变或增加,因此先设y1/x1为目标单价,先对目前单价进行增加处理,一直到小于目标单价,然后对x进行增加1,再对单价进行增加,看能最多获取多少次增长,每次增长为当前x的倒数(想一下就行),知道当x边为x1,因为最后的sum多加了一次,答案为sum-1;具体看代码,这题精度要把握好,掌握好几种控制精度的方法;
#include <iostream>#include <queue>#include <algorithm>#include <math.h>using namespace std;const double eps=1e-9;int main(){double y1,x,y,i,v;int add;while (scanf("%lf%lf",&x,&y)!=EOF){if (x>y)printf("-1\n");else{int sum=0;y1=1;for (i=1;i<=x;i++){v=(y+1-eps)*i/x;//控制精度add=floor(v-y1);//向下取整y1+=add;sum+=add;//本次单价涨了多少次sum++;//x要加一,所以总步数加一y1+=y1/i;}printf("%d\n",sum-1);}}return 0;}
阅读全文
0 0
- hdu 4803 贪心
- hdu 4803 贪心
- hdu 4803 贪心/思维题
- HDU 贪心
- 【贪心】hdu
- 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 2111 Saving HDU(贪心)
- HDU 2111贪心--【Saving HDU】
- HDU 2111 Saving HDU (贪心)
- HDU Saving HDU (贪心)
- 算法爱好者——背包问题升级版 ? 待解决
- 一台电脑同时连接多个git
- 蒜头君的排序(树状数组维护逆序对)
- Redis之Codis集群的搭建与使用-yellowcong
- android 实现男女按钮选择
- hdu 4803 贪心
- 实验:实现基于mysql的ftp虚拟用户
- Struts2+Spring+Hibernate的整合
- 网络工程师-海明码
- inotify通知机制
- 用malloc申请一个二维数组
- java字符串操作
- Java 并发 --- 阻塞队列之DelayQueue源码分析
- 学员状态转换器(简易)