#include<bits/stdc++.h>
#define endl '\n'
#define INF0x3f3f3f3f
#define int long long
using namespace std;
const int N =1e4 + 10;
int a[N],b[N];
int n;
//找左节点
bool check_min(int mid)
{
for(int i = 0;i < n ; i++)
{
if(b[i]<a[i]/mid)
return false;
}
return true;
}
//找右节点
bool check_max(int mid)
{
for(int i = 0 ; i < n ; i ++)
{
if(b[i] > a[i]/mid)
return false;
}
return true;
}
//---------------------------
void solve()
{
cin>>n;
for(int i = 0 ; i < n ; i ++)
cin>>a[i]>>b[i];
int lmin=1 ,rmin= 1e9;
//后面是二分模版
while(lmin<rmin)
{
int mid = lmin + rmin>>1;
if(check_min(mid))
rmin = mid;
else
lmin = mid +1;
}
int lmax = 1 , rmax = 1e9;
while(lmax<rmax)
{
int mid = lmax + rmax +1>>1;//这里如果写lmax+rmax 会陷入死循环,在最下面解释。
if(check_max(mid))
lmax=mid;
else
rmax= mid -1;
}
cout<<lmin<<" "<<lmax;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1 ;
while(t--)
solve();
}
//假设 mid = l+r/2
当 l = r -1 时,
mid = ((r-1)+r)/2 =r-0.5=r-1
由于c/c++是向下取整
接下来代码 lmax = mid(l)
就又会让 lmax = mid = lmax;从而陷入死循环
标签:二分,洛谷,int,rmax,mid,lmax,p9240,lmin,check From: https://www.cnblogs.com/ganl/p/18520258