poj 3863&&Gym - 101308B Business Center (水题/推公式)

题意:给你 m 个电梯,每个电梯有两个按钮, u 和 d ,分别代表上 u 层,和下 d 层,每一次你都从第0层开始做电梯,你可以按这个电梯按钮 m 次,假设楼层无限高,问你可以到达的最低楼层是多少,0层除外?

思路:

我们假设按 上走 x 次, 那么下走为 (n-x) 次

那么可以到达的楼层为 k = a*x - b*(n-x)

另上式等于0,我们可以得到当 x'= b*n/(a+b) 时为第0层

由于 x 必须为正整数,我们对 x' 向上取整,就得到可以到达的最低楼层

但是现在有一个漏洞,如果 x' 就是一个正整数,那么我们向上取整后还是x'本身 

遇到这种情况此时的x'=x'+1,也就是说我们就多上一层少下一层,就能避开到达0层了

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <bitset>
#include <stack>
#define lowbit(x) (x&-x)
using namespace std;
int main()
{
    freopen("business.in","r",stdin);
    freopen("business.out","w",stdout);
    long long n,m;
    while( cin>>n>>m)
    {
        long long ans = 0x3f3f3f3f3f;
        while(m--)
        {
            long long a,b;
            cin>>a>>b;
            double tmp = b*n*1.0/(a+b);
            long long x = ceil(tmp);
            long long fuck = a*x-b*(n-x);
            if(fuck==0) fuck = a*(x+1)-b*(n-x-1);
            ans=min(ans,fuck);
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

posted @ 2017-04-07 20:27  simpleknight  阅读(200)  评论(0编辑  收藏  举报