原题链接:https://vjudge.net/problem/UVA-1609
分类:二分法
备注:精度问题,技巧
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
const double eps=1e-9;
int n;
struct Node{
double l,r;
bool operator < (const Node& rhs)const{
return l<rhs.l;
}
}p[maxn];
bool check(double len){
double st=0.0;
for(int i=0;i<n;i++){
if(st<p[i].l)st=p[i].l;
if(st+len>p[i].r)return false;
st+=len;
}
return true;
}
int main(void){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d",&n)){
for(int i=0;i<n;i++)
scanf("%lf%lf",&p[i].l,&p[i].r);
sort(p,p+n);
double l=0.0,r=1e6,mid=(l+r)/2.0;
while(r-l>eps){
if(check(mid))l=mid;
else r=mid;
mid=(l+r)/2.0;
}
double ans=l;
int ansp=0,ansq=1;
for(int y=1;y<=n;y++){
int x=round(y*ans);
if(fabs(1.0*x/y-ans)<fabs(1.0*ansp/ansq-ans)){
ansp=x; ansq=y;
}
}
printf("%d/%d\n",ansp,ansq);
}
return 0;
}