Nowadays it is becoming increasingly difficult to park a car in cities successfully. Let's imagine a segment of a street as long as L meters along which a parking lot is located. Drivers should park their cars strictly parallel to the pavement on the right side of the street (remember that in the country the authors of the tasks come from the driving is right side!). Every driver when parking wants to leave for themselves some extra space to move their car freely, that's why a driver is looking for a place where the distance between his car and the one behind his will be no less than b meters and the distance between his car and the one in front of his will be no less than f meters (if there's no car behind then the car can be parked at the parking lot segment edge; the same is true for the case when there're no cars parked in front of the car). Let's introduce an axis of coordinates along the pavement. Let the parking lot begin at point 0 and end at point L. The drivers drive in the direction of the coordinates' increasing and look for the earliest place (with the smallest possible coordinate) where they can park the car. In case there's no such place, the driver drives on searching for his perfect peaceful haven. Sometimes some cars leave the street and free some space for parking. Considering that there never are two moving cars on a street at a time write a program that can use the data on the drivers, entering the street hoping to park there and the drivers leaving it, to model the process and determine a parking lot space for each car.
The first line contains three integers L, b и f (10 ≤ L ≤ 100000, 1 ≤ b, f ≤ 100). The second line contains an integer n (1 ≤ n ≤ 100) that indicates the number of requests the program has got. Every request is described on a single line and is given by two numbers. The first number represents the request type. If the request type is equal to 1, then in that case the second number indicates the length of a car (in meters) that enters the street looking for a place to park. And if the request type is equal to 2, then the second number identifies the number of such a request (starting with 1) that the car whose arrival to the parking lot was described by a request with this number, leaves the parking lot. It is guaranteed that that car was parked at the moment the request of the 2 type was made. The lengths of cars are integers from 1 to 1000.
For every request of the 1 type print number -1 on the single line if the corresponding car couldn't find place to park along the street. Otherwise, print a single number equal to the distance between the back of the car in its parked position and the beginning of the parking lot zone.
30 1 2
6
1 5
1 4
1 5
2 2
1 5
1 4
0
6
11
17
23
30 1 1
6
1 5
1 4
1 5
2 2
1 5
1 4
0
6
11
17
6
10 1 1
1
1 12
-1
Solution
模拟。
用pair<int,int>存空白区间,
用优先队列(priority queue)存(维护)所有空白区间。
这里有一个我遇到的问题:存(维护)何种空白区间。
显然有两种方案:
(1)存“实际”的空白区间,即(后车头/道路起点--前车尾/道路终点),停车时需考虑前后车距;
(2)存“可用”的空白区间,“可用”的含义是只要长度允许,车可在区间内任意停放,亦即不用考虑前后车距。
按方式(2),停车操作很方便实现,但离开操作就很麻烦(我在此处凌乱了,还没确认是否可做)。
按方式(1)则相反,但停车操作只是if-else,思路很清楚。
另外,还需要将当前活跃(active)区间用数组标记,将区间(a, b)记成 tail[a]=b, head[b]=a
1 #include<bits/stdc++.h> 2 #define X first 3 #define Y second 4 #define set1(a) memset(a, -1, sizeof(a)) 5 #define remove(a) head[tail[a]]=-1, tail[a]=-1 6 #define renew(a, b) tail[a]=b, head[b]=a 7 using namespace std; 8 typedef pair<int,int> pii; 9 pii r[110]; 10 int L, b, f, n; 11 void input(){ 12 scanf("%d%d%d%d", &L, &b, &f, &n); 13 for(int i=1; i<=n; i++) 14 scanf("%d%d", &r[i].X, &r[i].Y); 15 } 16 17 priority_queue<pii, vector<pii>, greater<pii> > q; 18 stack<pii> s; 19 const int MAX_L=1e5+10; 20 int head[MAX_L], tail[MAX_L]; 21 int ans[110]; 22 void park(int i){ 23 int len=r[i].Y; 24 ans[i]=-1; 25 while(!q.empty()){ 26 pii top=q.top(); 27 q.pop(); 28 if(tail[top.X]!=top.Y) continue; 29 if(top.X==0){ 30 if(top.Y==L){ 31 if(L>=len){ 32 ans[i]=0; 33 remove(0); 34 if(L>len){ 35 q.push(pii(len, L)); 36 //printf("%d %d ", len, L); 37 renew(len, L); 38 } 39 } 40 } 41 else if(top.Y>=len+f){ 42 ans[i]=0; 43 remove(0); 44 q.push(pii(len, top.Y)); 45 renew(len, top.Y); 46 } 47 } 48 else if(top.Y>=top.X+b+len){ 49 if(top.Y==L){ 50 remove(top.X); 51 renew(top.X, top.X+b); 52 ans[i]=top.X+b; 53 if(L>top.X+b+len){ 54 q.push(pii(top.X+b+len, L)); 55 renew(top.X+b+len, L); 56 } 57 } 58 else if(top.Y>=top.X+b+len+f){ 59 remove(top.X); 60 renew(top.X, top.X+b); 61 ans[i]=top.X+b; 62 q.push(pii(top.X+b+len, top.Y)); 63 renew(top.X+b+len, top.Y); 64 } 65 } 66 if(~ans[i]) break; 67 s.push(top); 68 } 69 while(!s.empty()) 70 q.push(s.top()), s.pop(); 71 } 72 73 void leave(int i){ 74 int lb=ans[i], rb=lb+r[i].Y; 75 int tmp; 76 if(~head[lb]) lb=head[lb], remove(lb); 77 if(~tail[rb]) tmp=tail[rb], remove(rb), rb=tmp; //error-prone 78 q.push(pii(lb, rb)); 79 renew(lb, rb); 80 } 81 82 void init(){ 83 set1(head); 84 set1(tail); 85 q.push(pii(0, L)); 86 renew(0, L); 87 } 88 89 90 int main(){ 91 //freopen("in", "r", stdin); 92 input(); 93 init(); 94 for(int i=1; i<=n; i++) 95 if(r[i].X==1) park(i), printf("%d ", ans[i]); 96 else leave(r[i].Y); 97 return 0; 98 }
P.S. 这道题的模拟也可以不用优先队列,用链表也行。