藍橋杯 A Careful Approach (枚舉+貪心+二分)

CHUAN SONG MEN
 

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#define eps (1e-6)
using namespace std;
const int maxn = 10;
int num[maxn],pos[maxn];
struct node{
	int a,int b;
}s[maxn];
int n;
bool check(double len){
	int x = s[pos[0]].a ;
	for(i = 2;i <= n;i++){
		x = x + len;
		if(x > s[pos[i].b])
			return false;
		else
			x = max(s[pos[i-1]].a,x);
	}
	return true;
}
int main(){
    int t = 0;
	while(~scanf("%d",&n)!= EOF){
		t++;
		if(!n)
			break;
		for(int i = 1;i <= n;i++){
			int l,r;
			cin>>s[i].a>>s[i].b;
		}
		int ans = 0;
		do{
			for(int i = 1 ;i <= n;i++)
				pos[num[i]] = 1;
				
			int l = 0,r = 1440;
			int mid = (l+r)/2;
			while(r - l > eps){
				mid = (l+r)/2;
				if(check(mid)){
					l = mid;
				}else{
				  	r = mid;	
				}
			}
		ans = max(ans,mid);
		}while(next_permutation(num + 1,num + n + 1));
		int f = int(ans);
		printf("Case %d: %d:%02d",t,f,int(fmod(ans*60,60)+0.5);
	}	
	return 0;
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章