zoukankan      html  css  js  c++  java
  • HDU-4803-Poor Warehouse Keeper(贪心)

    链接:

    https://vjudge.net/problem/HDU-4803

    题意:

    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?

    思路:

    因为肯定要增加x-1次x,所以算出最大的平均值.对于每一步,取能取到最大值.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    
    double x, y;
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int t;
        while (cin >> x >> y)
        {
            if (x > y)
            {
                puts("-1");
                continue;
            }
            double per = (y+0.99999)/x;
            double sum = 1.0;
            int cnt = x-1;
            for (int i = 1;i <= (int)x;i++)
            {
                double tmp = i*per;
                int sub = int(tmp-sum);
                sum += sub;
                cnt += sub;
                sum = sum*(i+1)/i;
            }
    //        cout << cnt << endl;
            printf("%d
    ", cnt);
        }
    
        return 0;
    }
    
  • 相关阅读:
    JSP学习-10-EL表达式
    深入浅出Mybatis(一)
    第10章—开启事务
    第09章—使用Lombok插件
    第08章—整合Spring Data JPA
    第06章—热部署
    第05章—Swagger2打造在线接口文档
    第03章—打造RESTful风格API
    第04章—整合Mybatis
    第01章—快速构建
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11375491.html
Copyright © 2011-2022 走看看