hdu-4803 Poor Warehouse Keeper

题目连接 :http://acm.hdu.edu.cn/showproblem.php?pid=4803

 

思路

第一种操作数量加1,第二种操作单价加y/x,所以第一种操作的数量是肯定不变的,只能操作x-1次,所以只需要在每次进行操作一之前,先进行操作2,让其到达最大单价。那么如何得到最大单价呢,易得y1 = (y + 1 -eps)*i/x;然后重复进行此操作,相当于减而治之。

#include <iostream>
#include <cstdio>
using namespace std;

const double eps=1e-5;
using namespace std;
int main()
{
    double x,y;
    while(~scanf("%lf %lf",&x,&y))
    {
        if(y<x)
        {
            printf("-1\n");
            continue;
        }
        double y1 = 1;
        int ans = 0;
        for (double i = 1; i < x; i += 1)
        {
            double now = (y + 1 - eps)*i /x;
            int add  = floor(now - y1);
            ans += add;
            y1 += add;
            ans++;
            y1 += y1/i;
        }
        // double now = (y + 1 - eps)*i /x;
        ans += floor(y + 1 - eps - y1);
        printf("%d\n", ans);
    }


    return 0;
 }
posted @ 2020-01-12 19:39  hulian425  阅读(93)  评论(0编辑  收藏  举报