[UVA][&二分&婪] 1079 - A Careful Approach@Morris' Blog|PChome Online 人新台
2013-09-22 21:19:43| 人3,667| 回0 | 上一篇 | 下一篇

[UVA][&二分&婪] 1079 - A Careful Approach

0 收藏 0 0 站台

If you think participating in a programming contest is stressful, imagine being an air traffic controller. With human lives at stake, an air traffic controller has to focus on tasks while working under constantly changing conditions as well as dealing with unforeseen events.

Consider the task of scheduling the airplanes that are landing at an airport. Incoming airplanes report their positions, directions, and speeds, and then the controller has to devise a landing schedule that brings all airplanes safely to the ground. Generally, the more time there is between successive landings, the ``safer" a landing schedule is. This extra time gives pilots the opportunity to react to changing weather and other surprises.

Luckily, part of this scheduling task can be automated - this is where you come in. You will be given scenarios of airplane landings. Each airplane has a time window during which it can safely land. You must compute an order for landing all airplanes that respects these time windows. Furthermore, the airplane landings should be stretched out as much as possible so that the minimum time gap between successive landings is as large as possible. For example, if three airplanes land at 10:00am, 10:05am, and 10:15am, then the smallest gap is five minutes, which occurs between the first two airplanes. Not all gaps have to be the same, but the smallest gap should be as large as possible.

Input 

The input file contains several test cases consisting of descriptions of landing scenarios. Each test case starts with a line containing a single integer n (2$ le$n$ le$8), which is the number of airplanes in the scenario. This is followed by n lines, each containing two integers ai, bi, which give the beginning and end of the closed interval [ai, bi] during which the i-th plane can land safely. The numbers ai and bi are specified in minutes and satisfy 0$ le$ai$ le$bi$ le$1440.

The input is terminated with a line containing the single integer zero.

Output 

For each test case in the input, print its case number (starting with 1) followed by the minimum achievable time gap between successive landings. Print the time split into minutes and seconds, rounded to the closest second. Follow the format of the sample output.

Sample Input 

3 0 10 5 15 10 15 2 0 10 10 20 0 

Sample Output 

Case 1: 7:30 Case 2: 20:00

目描述:

在有 n (<= 8)台在上旋,每台著一 [a, b] (位:分)

人希望它著隔最小值最大化。

四五入到秒,出格式  分:秒

目解法:

每台著的序,二分隔的,使用 Greedy 查。

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
int n, a[10], b[10], p[10];
int check(double gap) {
    double now = a[p[0]]+gap;
    int i;
    for(i = 1; i < n; i++) {
        if(b[p[i]] < now)
            return 0;
        if(a[p[i]] > now)
            now = a[p[i]];
        now += gap;
    }
    return 1;
}
double sol() {
    double l, r, mid;
    l = 0, r = 1440*60;//seconds
    // binary search max gap
    while(fabs(l-r) > 1e-2) {
        mid = (l+r)/2;
        if(check(mid))
            l = mid;
        else
            r = mid;
    }
    return mid;
}
int main() {
    int i, cases = 0;
    while(scanf("%d", &n) == 1 && n) {
        for(i = 0; i < n; i++) {
            scanf("%d %d", &a[i], &b[i]);
            a[i] *= 60, b[i] *= 60;
        }
        for(i = 0; i < n; i++)
            p[i] = i;
        double ret = 0;
        do {
            double tmp = sol();
            ret = max(ret, tmp);
        } while(next_permutation(p, p+n));
        printf("Case %d: ", ++cases);
        int secOnds= (int)round(ret);
        printf("%d:%02d\n", seconds/60, seconds%60);
    }
    return 0;
}

台: Morris
人(3,667) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: UVA |
此分下一篇:[UVA][dp] 1250 - Robot Challenge
此分上一篇:[UVA][二分&BFS] 10876 - Factory Robot

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
站台人
  • 累人:4,159,789
  • 日人:40
站搜
Quote
「能者,不能者徒」
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86