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;
    }
    
  • 相关阅读:
    FileOutputStream
    FileInputStream
    IO 流
    Django之路第五篇:Admin
    Django之路目录
    Django之路第四篇:Models
    Django之路第三篇:第一个Templates
    Django之路第二篇:路由配置
    Django之路第一篇:Django初探
    Python之str内部功能的介绍
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11375491.html
Copyright © 2011-2022 走看看