二分+全排列next_premutation

题目

题意:有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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值