HDU4803 Poor Warehouse Keeper

来源:互联网 发布:计算机培训内容c语言 编辑:程序博客网 时间:2024/05/17 18:48

题意:有一个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|1using 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;}


0 0