24h購物| | PChome| 登入
2012-06-05 06:54:36| 人氣780| 回應0 | 上一篇 | 下一篇

[ACM-ICPC][Greedy解] 5864 - Register Allocation

推薦 0 收藏 0 轉貼0 訂閱站台

A computer program stores the values of its variables in memory.For arithmetic computations, the values must be stored in easilyaccessed locations called registers. Registers areexpensive such that we need to use them efficiently. If twovariables are never used simultaneously, then we can allocate themto the same register. Suppose that there are n variables used ina computer program. For convenience, we useVar = {1, 2,..., n} to represent these n variables.For each variable i, we know the start time si andthe finish time fi when it is used. A variable i isactive during the time interval [si, fi], wheresi and fi are two positive integers with si < fi.Two variables i and j can be stored in the same register ifthe corresponding time intervals are disjoint, that is,[si, fi] $ cap$ [sj, fj] = $ emptyset$. Note that even whensi < fi = sj < fj, the intervals [si, fi] and[sj, fj] are not disjoint. Thus, such variables i and j cannot be placed in the same register. Given a set of n variablesand the corresponding start time and finish time, your task is towrite a computer program to compute the minimum number of usedregisters.

For example, consider Var = {1, 2, 3, 4, 5, 6, 7, 8} and thefollowing table shows the start time and finish time for eachvariable.


Variable si fi
1 1 3

2

2 6

3

4 8

4

5 11

5

7 9

6

10 14

7

12 15

8

13 16

Note that variables {1, 3, 6} can be stored in Register A;variables {2, 5, 7} can be stored in Register B; and variables{4, 8} can be stored in Register C. The minimum number of therequired registers equals 3.


Technical Specification

  • 1 $ leq$ n $ leq$ 10000.

  • For each variable i, 1$ le$si$ le$10000 and 2$ le$fi$ le$30000.

Input 

The first line of the input file contains an integer, denoting thenumber of test cases to follow. For each test case, the set ofregisters Var = {1, 2,..., n} is given with thefollowing format: The first line of each test case contains apositive integer n. In the following n lines, each linecontains two positive integers separated by a single space. Thefirst integer represents the start time and the second integerrepresents the finish time.The two positive integers in the i-th line of each test case represent si and fi of variable i.

Output 

For each test case, output the minimum number of the requiredregisters.

Sample Input 

281 23 45 67 89 1011 1213 1415 1661 22 33 44 55 66 7

Sample Output 

12


簡單得說, 找最少 LIS 覆蓋整個數列

#include <stdio.h>
#include <stdlib.h>
typedef struct {
    int st, ed;
}Work;
Work Job[10000];
int cmp(const void *i, const void *j) {
    Work *x, *y;
    x = (Work *)i, y = (Work *)j;
    if(x->st != y->st)
        return x->st - y->st;
    return x->ed - y->ed;
}
int main() {
    int t, n, i, j;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        int ans = 0, tn = n;
        for(i = 0; i < n; i++)
            scanf("%d %d", &Job[i].st, &Job[i].ed);
        qsort(Job, n, sizeof(Work), cmp);

        while(tn) {
            int led = -1;
            for(i = 0, j = 0; i < tn; i++) {
                if(led < Job[i].st) {
                    led = Job[i].ed;
                } else {
                    Job[j++] = Job[i];
                }
            }
            ans ++;
            tn = j;
        }
        printf("%d\n", ans);
    }
    return 0;
}


台長: Morris
人氣(780) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[ACM-ICPC][模擬] 5867 - Finding Feasible Paths
此分類上一篇:[ACM-ICPC][DP] 4867 - Maximum Square

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