题意:有n架飞机,它们有不同的降落区间,在这些闭区间的时间段内都可以降落,这些时间的单位都是分钟,问两个飞机最短间隔最大是多少,要精确到秒。
最大化最小值,肯定用二分,但是顺序不好弄,但是n最大为8,就直接全排列了。
(好像是蓝桥的题,总是这么暴力)
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
typedef long long ll;
const int maxn = 15;
struct pla
{
double x, y;
};
pla a[maxn];
int num[maxn],n;
const double eps = 1e-6;
bool check(double len)//判断这个长度是否可行
{
double pos = a[num[1]].x;//从第一个开始
for (int i = 2; i <= n; i++)
{
pos += len;
if (pos > a[num[i]].y)//如果下一个的最大距离太小,就返回0
return 0;
if (pos < a[num[i]].x)pos = a[num[i]].x;
}
return 1;
}
int main()
{
int i, j, cas = 0;
double l, r, mid, ans;
while (scanf("%d", &n) == 1)
{
if (n == 0)break;
ans = 0;
for (i = 1; i <= n; i++)
{
scanf("%lf%lf", &a[i].x, &a[i].y);
num[i] = i;
}
do
{
l = 0; r = 1440;
while ((r - l) > eps)
{
mid = (l + r) / 2;
if (check(mid))l = mid;
else r = mid;
}
if (ans < r)ans = r;
} while (next_permutation(num + 1, num + 1 + n));//排列顺序
ans *= 60;
printf("Case %d: ",++cas);
printf("%d:%02d\n", (int)ans / 60, (int)(fmod(ans, 60) + 0.5));
//fmod函数对浮点数取模。fmod(3.3213,2)=1.3213;
}
return 0;
}