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;
    }
    
  • 相关阅读:
    李超线段树 (Li-Chao Segment Tree)
    NowCoder Contest 894
    AtCoder Beginning Contest 126
    华工软院IBM LinuxONE Community Cloud云计算实验文档
    Codeforces Round #561 (div. 2)
    Comet OJ Contest #3
    Codeforces Edu Round 65 (Rated for Div. 2)
    莫队算法 (Mo's Algorithm)
    Codeforces Round #559 (Div. 2)
    GDCPC2019 广东省赛总结
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11375491.html
Copyright © 2011-2022 走看看