HDU4803 Poor Warehouse Keeper

题意:有一个numbei和一个total prices,还有一个价格,价格就等于total prices/number,每次可以增加两个其中的一个,增加number价格不变,所以对应total price也会增加,增加total price,numbei不变,价格会增加,给出一个x(numbei),y(total,price),问从1,1,开始最少需要增加多少次(每次只能增加1),能达到最大值。

贪心可以做,首先,如果x>y,直接输出-1.。

然后就只看价格了,如果要达到x和y,价格应该是在y/x和(y+1)/x之间,令r=(y+1)/x,在x=1的时候,每次增加total price,相应的价格就会增加1,比如:(1,2)->(1,3),价格增加了1,x=2,价格增加0.5,(2,3)->(2,4),价格增加了0.5,所以对于每个x增加total price价格都是增加1/x,所以就直接贪心,因为x=1的时候增幅最大,所以在1的时候增加y,增加的不能再增加的时候(<r),再换成x=2,继续增加,一直到价格已经在那个范围之内了(y/x-(y+1)/x)..

无解的情况只有x>y。。。

/*************************************************************************
    > File Name: 4803.cpp
    > Author: tjw
    > Mail: 
    > Created Time: 2014年11月09日 星期日 15时02分05秒
 ************************************************************************/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#define ll long long
#define ls k<<1
#define rs k<<1|1
using namespace std;
int main()
{
   int x,y,i;
    while(scanf("%d%d",&x,&y)==2)
    {
        if(x>y)
        {
            printf("-1\n");
            continue;
        }
        double a=x,b=y;
        int ans=x-1;
        double l=b/a;
        double r=(b+1)/a-0.00000001;
        ans+=(int)l-1;
        int flag=0;
        double temp=(int)l;
        if(temp>=l&&temp<=r)
            flag=1;
        for(i=2;i<=x&&!flag;i++)
        {
            double res=1.0/i;
            while(res+temp<r)
            {
                ans++;
                temp=res+temp;
                if(temp>=l&&temp<r)
                {
                    flag=1;
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值