[POJ3863]Business Center

来源:互联网 发布:碧然德 倍世 知乎 编辑:程序博客网 时间:2024/06/01 10:17

Time Limit: 1000MS Memory Limit: 65536K

Description

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

The first line of the input file contains two integer numbers n and m (1 <= n <= 1 000 000, 1 <= m <= 2 000) - 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 <= 1 000).

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

10 315 1215 47 12

Sample Output

13

Source
Northeastern Europe 2009

题意:
有一个无限高的楼,你开始在第0层,有m台电梯供你选择,一旦选择一个电梯就不能再更换。第i台电梯内有两个按钮,第一个按钮能让电梯上升ui层,第二个按钮能让电梯下降di层。电梯不能下降到0层以下。求问选择哪台电梯可以使得经过n次按按钮的操作后,电梯停在的楼层离第0层最近。(不能是第0层)

题解:
我们设一共按了x次上升按钮,那么下降按钮的被按次数为n-x,那么停留的楼层就是 ui * x-di * (n-x) 。那么我们就可以二分这个x。

#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<cstdlib>#include<algorithm>#define LiangJiaJun main#define INF 1999122700using namespace std;int n,m,ans=INF;struct data{int u,d;}a[2004];int calc(int U,int D){    int mid,l=0,r=n+1,ret=-1,oev;    while(l<=r){        mid=(l+r)>>1;        oev=U*mid-D*(n-mid);        if(oev>0)ret=oev,r=mid-1;        else l=mid+1;    }    return (ret==-1)?INF:ret;}int LiangJiaJun(){    scanf("%d%d",&n,&m);    for(int i=1;i<=m;i++)scanf("%d%d",&a[i].u,&a[i].d);    for(int i=1;i<=m;i++)ans=min(calc(a[i].u,a[i].d),ans);    printf("%d\n",ans);    return 0;}
原创粉丝点击