Business Center

B - Business Center
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 3
15 12
15 4
7 12

Sample Output

13
题意:有一栋高楼,有m个电梯,每个电梯分别可以上升a层和下降b层,让你在这m个电梯里选一个,经过n次上升下降后所在的楼层是最小 的并且大于最底层(就是0),这里要注意。
分析:列个方程就可以得出解题思路。设上升x次,那么下降(n-x)次。得出 a*x-b*(n-x)=0,
#include<iostream>
#include<stdio.h>
using namespace  std;
int main()
{
	int n,m,i,j,sum,a,b,x,dd,min;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		min=sum=9999999;
		for(i=0;i<m;i++)
		{
			scanf("%d %d",&a,&b);
			dd=b*n;
		
				x=dd/(a+b);
				x++;//因为题目的意思是要求结果大于0层,所以这里必须加1
			min=x*a-(n-x)*b;
			if(min<sum)
			{
				sum=min;
			}
		}
		cout<<sum<<endl;
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值