[HDU 4803]Poor Warehouse Keeper[贪心]

题目链接: [HDU 4803]Poor Warehouse Keeper[贪心]

题意分析:

有两个按钮,上面的按钮每按一次,个数增加1,下方的总数增加相应的单价,单价只显示整数部分;下方的按钮每按一次,总数增加1,不影响上方。现在从(1,1)开始,要到达(x,y),问:最少要按多少次按钮?没有答案则输出-1。

解题思路:

上方按钮不管怎么样都得按X - 1次,所以关键是下方的增长让它最快,并且保证最终结果不会超出Y。那么我们就可以事先求出该商品的最高单价:(Y + 1 - eps) / X。然后每一次都尽量让总价达到当前个数可以到达的总价,这样就能保证增长速度是最快的,而且不会超出Y。

个人感受:

知道是求最快增长,不过当时的思考方向是尽快到达最大单价,整个就失败告终。。。。。

具体代码如下:

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#define lowbit(x) (x & (-x))
#define root 1, n, 1
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1  1
#define ll long long
#define pr(x) cout << #x << " = " << (x) << '\n';
using namespace std;

const int INF = 0x7f7f7f7f;
const int MAXN = 1e6 + 111;
const double eps = 1e-5;

int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\apple\\Desktop\\in.txt", "r", stdin);
    #endif
    double x, y;
    while (~scanf("%lf%lf", &x, &y)) {
        if (x > y) {
            printf("-1\n");
            continue;
        }

        double mx = (y + 1 - eps) / x;
        double cur = 1;
        int ans = (int)x - 1;
        for (int i = 1; i <= (int)x; ++i) {
            double target = i * mx;
            int add = (int)(target - cur);
            cur += add;
            ans += add;
            cur = cur * (i + 1) / i;
        }
        printf("%d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值