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:
Where X and Y are previusly 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 <= 10 9) 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)
There are two values, X and Y, which respectively represent the quantity and total price. There are two operations: 1. total Price + 1, quantity unchanged, but unit price changed; 2. number + 1, the total price also changes, the unit price remains unchanged, and the minimum number of operations to reach the specified X and Y
Idea: greedy question. First, we make it clear that the unit price is always getting bigger. We can also find the maximum unit price, and then greedy to find the number of total prices to be increased for each different number.
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const double eps = 1e-9;double x, y;int main() {while (scanf("%lf%lf", &x, &y) != EOF) {if (x > y) {printf("-1\n");continue;}double k = (y+1-eps) / x;double tmp = 1;int cnt = (int) x - 1;for (int i = 1; i <= (int)x; i++) {double t = i * k;int u = (int) (t - tmp);tmp += u;tmp = tmp * (i+1) / i;cnt += u;}printf("%d\n", cnt);}return 0;}
HDU-4803 poor warehouse keeper (Greedy)