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;
    }
    
  • 相关阅读:
    Ubuntu Linux markdown编辑工具 typora 安装
    ref以及传值传址的理解
    3. 无重复字符的最长子串
    30. 串联所有单词的子串 (哈希+滑动窗口)
    525. 连续数组 (哈希表)
    438. 找到字符串中所有字母异位词 (滑动窗口)
    451、根据字符出现频率排序(哈希 加优先队列)
    743. 网络延迟时间
    310. 最小高度树
    8皇后问题
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11375491.html
Copyright © 2011-2022 走看看