• 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;
    }
    
  • 相关阅读:
    OpenStack之Fuel架构及其工作原理
    Docker容器常用命令汇总
    openstack 5大组件之间的关系和基本架构思维导图
    LeetCode刷题实战4:寻找两个正序数组的中位数
    LeetCode刷题实战3:无重复字符的最长子串
    LeetCode刷题实战2:两数相加
    LeetCode刷题实战1:两数之和
    剑指Offer 05. 替换空格
    剑指Offer 03. 数组中重复的数字
    TensorFlow2.0(4):填充与复制
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11375491.html
Copyright © 2020-2023  润新知