【2013南京现场赛】1002 hdu4803 Poor Warehouse Keeper 贪心

来源:互联网 发布:三星s5数据备份apk下载 编辑:程序博客网 时间:2024/05/16 06:28
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 13 89 31
 

Sample Output
0511
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)
 


题意:

给出两个只能显示整数(第二个牌子的实际数值是小数)的牌子,第一个是数量,第二个是总价,倍数即为单价

点击第一个牌子,

第一个牌子的数字+1,第二个牌子的即为原来数字+当前单价(单价可能是小数,但最终结果只显示整数部分)

点击第二个牌子

第一个牌子不变,第二个牌子加1(相当于加了单价)

 

现在给出x,y,问到达x,y需要的最少步骤x(1 <= x <= 10) and y(1 <= y <= 10 9),x,y是整数。


思路:
由于想要求最小的步骤,我们发现越早提升单价,消耗掉代价越小。所以我们需要尽早先提升单价,但是另一方面,由于单价只能升不能降,所以我们每一步得到的单价必须要小于最终的单价。因为只取整数部分,我们可以将最终的总价+0.999999

////  main.cpp//  B////  Created by zc on 2017/9/9.//  Copyright © 2017年 zc. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define ll long longusing namespace std;int x,y;int main(int argc, const char * argv[]) {    while(~scanf("%d%d",&x,&y))    {        if(y<x)        {            printf("-1\n");            continue;        }        int ans=0;        double last=1.0;        for(int i=1;i<=x;i++)        {            double j=(double)(y+0.999999)/x*(double)i;            ans+=(int)(j-last);            last+=(double)((int)(j-last));            if((int)(last/(double)i*(double)x)==y)            {                last=(double)y;                ans+=(x-i);                break;            }            if(i<x)            {                ans++;                last*=(double)(i+1)/i;            }        }        if((int)last!=y)    ans=-1;        printf("%d\n",ans);    }    }

原创粉丝点击