24h購物| | PChome| 登入
2012-06-05 15:39:32| 人氣522| 回應0 | 上一篇 | 下一篇

[UVA][priority_queue] 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 easily accessed locations called registers. Registers are expensive such that we need to use them efficiently. If two variables are never used simultaneously, then we can allocate them to the same register. Suppose that there are n variables used in a computer program. For convenience, we use Var = {1, 2,..., n} to represent these n variables. For each variable i, we know the start time si and the finish time fi when it is used. A variable i is active during the time interval [si, fi], where si and fi are two positive integers with si < fi. Two variables i and j can be stored in the same register if the corresponding time intervals are disjoint, that is, [si, fi] $ cap$ [sj, fj] = $ emptyset$. Note that even when si < 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 variables and the corresponding start time and finish time, your task is to write a computer program to compute the minimum number of used registers.

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


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 the required 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 the number of test cases to follow. For each test case, the set of registers Var = {1, 2,..., n} is given with the following format: The first line of each test case contains a positive integer n. In the following n lines, each line contains two positive integers separated by a single space. The first integer represents the start time and the second integer represents 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 required registers.

Sample Input 

2
8
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
6
1 2
2 3
3 4
4 5
5 6
6 7

Sample Output 

1
2

求並行執行最大數, 使用 priority_queue

#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
struct var {
int s, f;
};
struct cmp2 {
bool operator() (int x, int y) {
return x - y > 0;
}
};
bool cmp(var i, var j) {
if(i.s != j.s)
return i.s - j.s < 0;
return i.f - j.f < 0;
}
int main() {
int t, n, i;
var V[10000];
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(i = 0; i < n; i++) {
scanf("%d %d", &V[i].s, &V[i].f);
}
sort(V, V+n, cmp);
priority_queue<int, vector<int>, cmp2> pQ;
int ans = 0, end;
for(i = 0; i < n; i++) {
while(!pQ.empty()) {
end = pQ.top();
if(end < V[i].s) {
pQ.pop();
} else {
break;
}
}
pQ.push(V[i].f);
if(pQ.size() > ans)
ans = pQ.size();
}
printf("%d\n", ans);
}
return 0;
}
 


台長: Morris
人氣(522) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10260 - Soundex Time
此分類上一篇:[ACM-ICPC] 5862 - City Travel

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