24h購物| | PChome| 登入
2013-09-22 21:19:43| 人氣3,557| 回應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,557) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][dp] 1250 - Robot Challenge
此分類上一篇:[UVA][二分&BFS] 10876 - Factory Robot

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文