HDU 4803 Poor Warehouse Keeper

题意:


可以给number++,也可以给total prices++。如果给number++,那么total prices+=total prices/number(这里的number是增长之前的)。求最少多少步,使得number=x,total prices=y,total prices只取整数部分。


思路:


知道x的话,就是知道了number会增加几次,每次增加prices也会增加。

令y=y-x,如果y<0那么是不能完成任务的。下面说的y都是指y-x。


那么在第i次准备增加number的时候,我们有两种决策:

1.直接把prices加到等于y,花费y-prices,第i次不增长了,结束过程。

2.增加t次prices,使得第i次number增加完后,prices恰好<=y,继续这个过程。

答案就1,2中取min


code:

#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;
    
#define N  1000100
#define ll long long
#define ALL(x)     x.begin(),x.end()
#define CLR(x,a) memset(x,a,sizeof(x))
typedef pair<int,int> PI;
const int INF=0x3fffffff;
const int MOD   =1000000007;
const double EPS=1e-7;

int x,y;
double now;

int dfs(int n){
    if(n==0 || int(floor(now))==y) return y-int(floor(now));
    int l=0,r=INF;
    while(l<r){
        int mid=(l+r)>>1;
        if( (ll)floor(1.0/(x-n+1)*mid*x+now)<=y ) l=mid+1;
        else r=mid;    
    }
    int _sum=y-int(floor(now));
    now+=1.0/(x-n+1)*(r-1)*x;
    int sum=dfs(n-1)+r-1;
    return min(sum,_sum);
}

int main(){
    while(~scanf("%d%d",&x,&y)){
        now=x;
        if(y<now) puts("-1");
        else printf("%d\n",dfs(x)+x-1);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值