poj 3863 Business Center

来源:互联网 发布:天国王朝知乎 编辑:程序博客网 时间:2024/05/21 06:11
International Cyber Police Corporation (ICPC) had built a new mega-tall business center to host its
headquarters and to lease some space for extra profit. It has so many floors, that it is impractical to have
a separate button in each of its m elevator cars for each individual floor. Instead, each elevator car has
just two buttons. One button in i-th elevator car makes it move up ui floors, the other makes it move
down di floors. The business center is so high, that we can ignore its height for this problem (you will
never reach the top floor), but you cannot go below the ground floor. All floors are numbered by integer
numbers starting from zero, zero being the ground floor.
You start on the ground floor of the business center. You have to choose one elevator car out of m to
ride on. You cannot switch elevators cars after that. What is the lowest floor above the ground floor you
can get to after you press elevator car buttons exactly n times?
Input
Thefirstlineoftheinputfilecontainstwointegernumbers nandm(1 ≤ n ≤ 1000000, 1 ≤ m ≤ 2000)—
the number of button presses and the number of elevator cars to choose from. The following m lines
describe elevator cars. Each line contains two integer numbers ui and di (1 ≤ ui, di ≤ 1000).
Output
Write to the output file a single positive integer number — the number of the lowest floor above ground

floor that can be reached by one of m elevators after pressing its buttons exactly n times.


Sample input and output
business.in business.out
10 3 13
15 12
15 4
7 12


题意:给你好多电梯,每个电梯只有两个按钮,上a层和下b层,问选一个电梯,经过n次按按钮,能到达的最低楼层

思路:刚开始写的时候一直在第八组wa,后来随便改了改竟然过了,赛后有好好看了一下题意,原来是不能到达0层楼的,比赛的时候竟然改过了也是神奇,这个题直接就可以得答案不需要二分查找,

题目分析:设上升次数w为x ,则x*u-(n-x)*d>=0 解得:x>=n*d/(u+d);当能够整除时:在最终停在u+d,不过能够时停在

(n*d/(u+d)+1)*(u+d)-n*d;

ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;


int main()
{
    //freopen("business.in","r",stdin);
    //freopen("business.out","w",stdout);
    int n,m,a,b,minn,t;
    while(cin>>n>>m)
    {
        minn=1000000000;
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b;
            if((b*n)%(a+b)==0)
            t=a+b;//由于不能到0层所以需要加一层往上的减一层往下的正好多了a+b
            else
            {
                int k=(b*n)/(a+b)+1;
                t=a*k-b*(n-k);
            }
            minn=min(t,minn);
        }
        cout<<minn<<endl;
    }
    return 0;
}

0 0
原创粉丝点击