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[];
scanf("%d" ,&tt);
while(tt--)
{
scanf("%d %d %d" ,&n ,&t ,&m);
n_l = n_r = ;
for(i = ;i <= m; i ++)
{
scanf("%d %s" ,&time ,str);
if(str[] == '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 = ,now_time = ;
int l = ,r = ;
while(l <= n_l || r <= n_r)
{
if(l <= n_l && node_l[l] < node_r[r] || r > n_r)
{ if(now_fx != ) now_time = maxx(now_time + t,node_l[l] + t);
else now_time = maxx(now_time ,node_l[l]); if(now_fx != )
{
for(i = ;i <= n && r <= n_r;i ++)
{
if(node_r[r] <= now_time - t)
{
ans[id_r[r++]] =now_time;
}
else break;
}
} for(i = ;i <= n && l <= n_l ;i ++)
{
if(node_l[l] <= now_time)
{
ans[id_l[l++]] =now_time + t;
}
else break;
}
now_fx = ;
now_time += t;
}
else
{
if(now_fx != ) now_time = maxx(now_time + t,node_r[r] + t);
else now_time = maxx(now_time ,node_r[r]); if(now_fx != )
{
for(i = ;i <= n && l <= n_l;i ++)
{
if(node_l[l] <= now_time - t)
{
ans[id_l[l++]] =now_time;
}
else break;
}
} for(i = ;i <= n && r <= n_r;i ++)
{
if(node_r[r] <= now_time)
{
ans[id_r[r++]] =now_time + t;
}
else break;
}
now_fx = ;
now_time += t;
}
}
for(i = ;i <= m ;i ++)
printf("%d\n" ,ans[i]);
if(tt) printf("\n");
}
return ;
}

Ferry Loading III[HDU1146]的更多相关文章

  1. 【HDOJ】1406 Ferry Loading III

    模拟,注意需要比较队头与当前时间的大小关系. #include <cstdio> #include <cstring> #include <cstdlib> #de ...

  2. POJ-2336 Ferry Loading II(简单DP)

    Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Desc ...

  3. POJ 2609 Ferry Loading(双塔DP)

    Ferry Loading Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1807   Accepted: 509   Sp ...

  4. poj 2336 Ferry Loading II ( 【贪心】 )

    Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3704   Accepted: 1884 ...

  5. poj-2336 Ferry Loading II(dp)

    题目链接: Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: ...

  6. TOJ 2419: Ferry Loading II

    2419: Ferry Loading II  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Subm ...

  7. Ferry Loading II_贪心

    Description Before bridges were common, ferries were used to transport cars across rivers. River fer ...

  8. [POJ2336]Ferry Loading II

    题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...

  9. Ferry Loading||

    uva10440:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...

随机推荐

  1. Mysql基于GTIDs的复制

    通过GTIDs[global transaction identifiers],可以标识每一个事务,并且可以在其一旦提交追踪并应用于任何一个Slave上:这样 就不需要像BinaryLog复制依赖Lo ...

  2. INNODB

    INNODB,是Mysql5.7的默认存储引擎,是事务安全的,支持ACID,具有提交,回滚和crash-recovery[灾备]能力,以保护用户数据. 优势:一旦Server崩溃,Innodb会自动保 ...

  3. Ubuntu自定义服务

    1.准备脚本 准备好一个bash服务脚本,包括start|stop|restart等参数,将脚本文件命名为“服务名”,拷贝到/etc/init.d/目录下. 2.添加服务sudo update-rc. ...

  4. AngularJS 控制器 ng-controller

    AngularJS 控制器 控制 AngularJS 应用程序的数据. AngularJS 控制器是常规的 JavaScript 对象. AngularJS 应用程序被控制器控制. ng-contro ...

  5. ARPPING

    http://www.tuicool.com/articles/M7B3umj http://lixcto.blog.51cto.com/4834175/1571838/

  6. Oracle数据库备份与还原操作具体步骤

    Oracle数据库导出操作 导入导出都要进行目录创建与授权. 在pl/sql里面编写也可以 select * from dba_directories(这个是查看创建的目录) drop directo ...

  7. SQLServer基本查询

    条件查询 --1.比较运算符 --2.确定集合谓词 --3.确定范围谓词 , ) --4.字符匹配谓词 select * from dbo.DepartMent where dName like 'C ...

  8. 聊聊SOA面向服务架构

    什么是SOA SOA(Service-Oriented Architecture),即面向服务的架构.SOA是一种粗粒度.松耦合服务架构,服务之间通过简单.精确定义接口进行通讯,不涉及底层编程接口和通 ...

  9. [javascript] 使用闭包编写模块

    这是一篇[javascript the good parts]的读书笔记. 我们知道可以利用javascript 的prototype 特性为原始类型编写拓展模块.利用如下方法: Object.pro ...

  10. 华为Mate8 NFC 时好时坏,怎么解决呢?

    拿起手机朝桌子上磕几下,nfc就好用了.这是花粉总结的,我也试过,很灵.注意要带套,摄像头朝下,头部低一点往下磕.因为nfc芯片在头部,估计是接触不良.