Ferry Loading III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 463 Accepted Submission(s): 110
Problem Description
Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long it is carrying a car or there is at least one car waiting at either bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up to n cars that are waiting to cross. If there are more than n, those that have been waiting the longest are loaded. If there are no cars waiting on either bank, the ferry waits until one arrives, loads it (if it arrives on the same bank of the ferry), and crosses the river. At what time does each car reach the other side of the river?
Input
The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day), and the bank at which the car arrives ("left" or "right"). For each test case, output one line per car, in the same order as the input, giving the time at which that car is unloaded at the opposite bank. Output an empty line between cases.
Output
You may assume that 0 < n, t, m ≤ 10000. The arrival times for each test case are strictly increasing. The ferry is initially on the left bank. Loading and unloading time may be considered to be 0.
Sample Input
2
2 10 10
0 left
10 left
20 left
30 left
40 left
50 left
60 left
70 left
80 left
90 left
2 10 3
10 right
25 left
40 left
Sample Output
10
30
30
50
50
70
70
90
90
110
30
40
60
#include<stdio.h>
#include<string.h>
#define N 10000 + 50
int node_l[N] ,node_r[N];
int id_l[N] ,id_r[N];
int ans[N];
int n_l ,n_r;
int maxx(int x ,int y)
{
return x > y ? x : y;
}
int main ()
{
int n ,y ,m ,tt ,t;
int i ,j ,time;
char str[10];
scanf("%d" ,&tt);
while(tt--)
{
scanf("%d %d %d" ,&n ,&t ,&m);
n_l = n_r = 0;
for(i = 1 ;i <= m; i ++)
{
scanf("%d %s" ,&time ,str);
if(str[0] == 'l')
{
node_l[++n_l] = time;
id_l[n_l] = i;
}
else
{
node_r[++n_r] = time;
id_r[n_r] = i;
}
}
int now_fx = 1 ,now_time = 0;
int l = 1 ,r = 1;
while(l <= n_l || r <= n_r)
{
if(l <= n_l && node_l[l] < node_r[r] || r > n_r)
{
if(now_fx != 1) now_time = maxx(now_time + t,node_l[l] + t);
else now_time = maxx(now_time ,node_l[l]);
if(now_fx != 1)
{
for(i = 1 ;i <= n && r <= n_r;i ++)
{
if(node_r[r] <= now_time - t)
{
ans[id_r[r++]] =now_time;
}
else break;
}
}
for(i = 1 ;i <= n && l <= n_l ;i ++)
{
if(node_l[l] <= now_time)
{
ans[id_l[l++]] =now_time + t;
}
else break;
}
now_fx = 2;
now_time += t;
}
else
{
if(now_fx != 2) now_time = maxx(now_time + t,node_r[r] + t);
else now_time = maxx(now_time ,node_r[r]);
if(now_fx != 2)
{
for(i = 1 ;i <= n && l <= n_l;i ++)
{
if(node_l[l] <= now_time - t)
{
ans[id_l[l++]] =now_time;
}
else break;
}
}
for(i = 1 ;i <= n && r <= n_r;i ++)
{
if(node_r[r] <= now_time)
{
ans[id_r[r++]] =now_time + t;
}
else break;
}
now_fx = 1;
now_time += t;
}
}
for(i = 1 ;i <= m ;i ++)
printf("%d
" ,ans[i]);
if(tt) printf("
");
}
return 0;
}