标签:
依然是二分思路,但是精度要求很高需要用long double,结果要输出分数,那么就枚举一下分母,然后求出分子,在判断一下和原来的数的误差。
#include<bits/stdc++.h> using namespace std; typedef long double ld; const int maxn = 1e5+5; const ld eps = 1e-11; struct Seg { int l,r; bool operator < (const Seg& x) const { return l<x.l || (l == x.l && r < x.r); } }S[maxn]; #define bug(x) cout<<#x<<‘=‘<<x<<endl #define Pld(x) printf("%Lf\n",x) int n; bool P(ld x) { // Pld(x); ld cur = 0; for(int i = 0; i < n; i++){ cur = max(cur,(ld)S[i].l); cur += x; if(cur-S[i].r>eps) return false; } return true; } int main() { //freopen("in.txt","r",stdin); while(~scanf("%d",&n)){ for(int i = 0; i < n; i++){ scanf("%d%d",&S[i].l,&S[i].r); } sort(S,S+n); ld L,R,mid; for(L = 0,R = S[n-1].r; R-L>eps; P(mid)?L=mid:R=mid)mid = (L+R)/2; int p,q; for(q = 1; q <= n+1; q++){ p = round(L*q); ld t = (ld)p/q; if(fabs(t-L)<eps) { break;} } printf("%d/%d\n",p,q); } return 0; }
UVA 1616 Caravan Robbers 商队抢劫者(二分)
标签:
原文地址:http://www.cnblogs.com/jerryRey/p/4716840.html