UVA - 1616 Caravan Robbers (二分搜索)

Caravan Robbers


题目描述:

   Long long ago in a far far away land there were two great cities and The Great Caravan Road between them. Many robber gangs “worked” on that road.
   By an old custom the i-th band robbed all merchants that dared to travel between ai and bi miles of The Great Caravan Road. The custom was old, but a clever one, as there were no two distinct i and j such that ai ≤ aj and bj ≤ bi . Still when intervals controlled by two gangs intersected, bloody fights erupted occasionally. Gang leaders decided to end those wars. They decided to assign each gang a new interval such that all new intervals do not intersect (to avoid bloodshed), for each gang their new interval is subinterval of the old one (to respect the old custom), and all new intervals are of equal length (to keep things fair).
   You are hired to compute the maximal possible length of an interval that each gang would control after redistribution.

Input

  The input will contain several test cases, each of them as described below.
  The first line contains n (1 ≤ n ≤ 100000) — the number of gangs. Each of the next n lines contains information about one of the gangs — two integer numbers ai and bi (0 ≤ ai < bi ≤ 1000000). Data provided in the input file conforms to the conditions laid out in the problem statement.

Output

    For each test case, write to the output on a line by itself. Output the maximal possible length of an interval in miles as an irreducible fraction p/q.
    Note for the sample:
    In the above example, one possible set of new intervals that each gang would control after redistribution is given below.
          • The first gang would control an interval between 7/2 = 3.5 and 12/2 = 6 miles which has length of 5/2 and is a subinterval of its original (2, 6).
          • The second gang would control an interval between 2/2 = 1 and 7/2 = 3.5 miles which has length of 5/2 and is a subinterval of its original (1, 4).
          • The third gang would control an interval between 16/2 = 8 and 21/2 = 10.5 miles which has length of 5/2 and is a subinterval of its original (8, 12).

Sample Input

3
2 6
1 4
8 12

Sample Output

5/2


大致意思是给你几个线段,你只能减小每条线段,使每条线段等长,而且不相交。求线段最长的情况。

#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const double MIN=1e-9;
struct node                     //结构体
{
    double l,r;      
}a[100010];

bool cmp(node n,node m)         //给结构体数组用的比较函数
{
    return n.r<m.r;
}

int main()
{
    int n;
    while(cin>>n&&n)
    {
        double l=0,r=1000000;
        for(int i=1;i<=n;i++)
            cin>>a[i].l>>a[i].r;

        sort(a+1,a+1+n,cmp);
        while((r-l)>MIN)                             //二分搜索
        {
            double s=0,m=(r+l)/2.0;
            bool f = true;
            for(int i=1;i<=n;i++)
            {
                if(s<a[i].l) s=a[i].l;
                if(s+m>a[i].r){f=false;break;}
                s+=m;
            }
            if(f) l=m;
            else r=m;
        }

        int p=0,q=1;                               //把答案转化成分数的形式输出。
        for(int i=1;i<=n;i++)
        {
            int k=round(l*i);
            if(fabs((double)k/i-l)<fabs((double)p/q-l))      //观察函数的增减性,找到极值就是答案
            {
                p=k;
                q=i;
            }
        }
        cout<<p<<"/"<<q<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/j1nab1n9/article/details/77949766
今日推荐