HDU 4803 Poor Warehouse Keeper(贪心)

来源:互联网 发布:ecshop大京东2.5源码 编辑:程序博客网 时间:2024/05/01 07:33

题意:

让你按如题所示的两个按钮,初始情况是1 1,问你最少多少次可以把他按成x y。
这个贪心策略还是略机智的,因为上面一个按钮会影响下面的,而下面的只会影响单价,所以我们要尽量是下面的变大,达到一个最大值,然后上面的只要按几次就能达到了。
但是下面的也不能太大,不然等上面的按钮达到要求之后,就会使得下面的溢出了,所以我们每次在按上面按钮之前让下面的达到尽量大就可以了。

代码:

////  Created by  CQU_CST_WuErli//  Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.//#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <string>#include <vector>#include <list>#include <map>#include <queue>#include <stack>#include <set>#include <algorithm>#include <sstream>#define CLR(x) memset(x,0,sizeof(x))#define OFF(x) memset(x,-1,sizeof(x))#define MEM(x,a) memset((x),(a),sizeof(x))#define BUG cout << "I am here" << endl#define lookln(x) cout << #x << "=" << x << endl#define SI(a) scanf("%d",&a)#define SII(a,b) scanf("%d%d",&a,&b)#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)#define rep(flag,start,end) for(int flag=start;flag<=end;flag++)#define Rep(flag,start,end) for(int flag=start;flag>=end;flag--)#define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1#define Root 1,n,1#define BigInteger bignconst int MAX_L=2005;// For BigIntegerconst int INF_INT=0x3f3f3f3f;const long long INF_LL=0x7fffffff;const int MOD=1e9+7;const double eps=1e-6;const double pi=acos(-1);typedef long long  ll;using namespace std;int x,y;int main(int argc, char const *argv[]) {#ifdef LOCAL    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);    // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);#endif    while(SII(x,y)==2) {        if (x>y) {            cout << -1 << endl;            continue;        }        if (x==1) {            cout << y-1 << endl;            continue;        }        int ans=x-1;        double Maxi=((double)y+1.0-eps)/(double)x;        double tmp=1.0;        rep(i,1,x) {            double now=i*Maxi;            int tt=(int)(now-tmp);            tmp+=tt;            tmp=tmp*(i+1)/i;            ans+=tt;        }        cout << ans << endl;    }    return 0;}
0 0
原创粉丝点击