24h購物| | PChome| 登入
2012-11-02 17:37:04| 人氣1,148| 回應0 | 上一篇 | 下一篇

[ACM-ICPC] 4712 - Airplane Parking

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

During this economic crisis time, Jack has started an incredible new business related to air travel, a parking-lot for airplane. He bought a very large land to park airplanes. However the land is very narrow, so that the only way airplanes can go in or go out of the parking lot must be in the Last-In First-Out fashion (see picture below). He only has spaces in the parking lot so he cannot take some airplane at the end out so that other airplanes can move.

epsfbox{p4712.eps}

Because of the limitation of the parking lot, it is not possible to accommodate all requests for parking. Each request consists of the planned arrival time and planned departure time, which are the times the airplane arrives at the parking lot. An example below shows a request table for 4 planes.


Airplane Arrival Departure
1 1 10
2 2 5
3 3 7
4 6 9


In this case, it is possible to accommodate airplane 1, 2, and 4. But it is not possible to accommodate both airplanes 2 and 3.

It is possible that different planes plan to arrive or depart the parking lot at the same time. Jack has the best crews working with him, so that they will manage to arrange the plane to the parking lot in the best way that if it is possible to park and take out the planes they will be able to do it. Consider another example.


Airplane Arrival Departure
5 10 12
6 10 15
7 13 17


Although airplane 5 and 6 arrive at the same time, Jack's crews know that airplane 5 will have to be out before airplane 6, so when both airplanes arrive they put airplane 6 in first, following by airplane 5.

Given a list of parking requests, you want to find the maximum number of airplanes that can be parked in this parking lot, provided that they can only depart in the Last-In First-Out fashion.

Input 

The first line contains an integer T, the number of test cases (1$ le$T$ le$5). Each test case is in the following format.

The first line starts with an integer N (1$ le$N$ le$300) denoting the number of airplanes. The next N lines describe the request table. Each line 1 + i, for 1$ le$i$ le$N, contains two integer Si and Ti , (0$ le$Si < Ti$ le$1, 000, 000, 000) which are the planned arrival time and planned departing time for airplane i.

Output 

For each test case, you program must output a single line consisting of one integer, the maximum number of airplanes that can be parked in Jack's parking lot.

Sample Input 

2 
4
1 10
2 5
3 7
6 9
3
10 12
10 15
13 17

Sample Output 

3 
2

把時間換成括弧,但括弧可以重疊。
有點矩陣鏈乘積的做法,消耗 O(N^3),尚未學到四邊形不等式,暫無優化。

#include <cstdio>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int t, n, tn;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
int S[305], T[305], i, j, k;
map<int, int> r;
for(i = 0; i < n; i++) {
scanf("%d %d", &S[i], &T[i]);
r[S[i]] = 1;
r[T[i]] = 1;
}
i = 0;
for(map<int, int>::iterator it = r.begin();
it != r.end(); it++) {
it->second = i;
i++;
}
tn = i+1;
int dp[601][601] = {};
char has[601][601] = {};
for(i = 0; i < n; i++) {
has[r[S[i]]][r[T[i]]] = 1;
}
int ans = 1;
for(i = 0; i < tn; i++) {
for(j = 0; j+i < tn; j++) {
dp[j][j+i] = 0;
if(i)
dp[j][j+i] = max(dp[j+1][j+i], dp[j][j+i-1]);
for(k = j; k <= j+i; k++) {
dp[j][j+i] = max(dp[j][j+i], dp[j][k]+dp[k][j+i]);
}

dp[j][j+i] += has[j][j+i];
if(dp[j][j+i] > ans)
ans = dp[j][j+i];
}
}
cout << ans << endl;
}
return 0;
}
 

台長: Morris
人氣(1,148) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[ACM-ICPC] 4713 - Elias Omega Coding
此分類上一篇:[UVA] 759 - The Return of the Roman Empire

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