标签:des style blog http color io os ar for
Description
Input
Output
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,数量不变,但是单价会变;2.数量+1,总价也跟着变,单价不变,求到达指定X,Y的最小操作次数
思路:贪心的题目,首先,我们明确单价是一直在变大的,我们也可以求出最大的单价,然后贪心的去求每个不同数量对应要增加的总价次数
#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(贪心)
标签:des style blog http color io os ar for
原文地址:http://blog.csdn.net/u011345136/article/details/40300671