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. php 简易购物习题

    1.货物界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  2. WPA: 4-Way Handshake failed - pre-shared key may be incorrect

    生成psk网址: https://www.wireshark.org/tools/wpa-psk.html 相关 bug: 重点 关注 : https://en.community.sonos.com ...

  3. Delphi的面向对象编程基础笔记

    1.面向对象.一门面向对象的编程语言至少要实现以下三个OOP的概念 封装:把相关的数据和代码结合在一起,并隐藏细节.封装的好处是利用程序的模块化,并把代码和其他代码分开 继承:是指一个新的类能够从父类 ...

  4. oracle使用dbms_metadata包取得所有对象DDL语句

    当我们想要查看某个表或者是表空间的DDL的时候,可以利用dbms_metadata.get_ddl这个包来查看. dbms_metadata包中的get_ddl函数详细参数 GET_DDL函数返回创建 ...

  5. Solr入门之(3)常用概念说明(持续补充):

    由于solr底层使用lucene,所以很多概念与lucene相同,下面是几个常用的概念: * Document:一个要进行索引的单元,相当于数据库的一行纪录,任何想要被索引的数据,都必须转化为Docu ...

  6. 使用zookeeper实现分布式锁

    简介: 核心是解决资源竞争的问题 分布式系统中经常需要协调多进程或者多台机器之间的同步问题,得益于zookeeper,实现了一个分布式的共享锁,方便在多台服务器之间竞争资源时,来协调各系统之间的协作和 ...

  7. JavaScript - BOM

    window 对象 window 对象是BOM的核心对象,也是ECMAScript规定的Global对象. 无法跨浏览精确获得窗口左边和上边的精确值,同样也无法确定浏览器窗口本身的大小,但是可以取得页 ...

  8. MySQL级联删除的问题

    一.FOREIGN KEY 的定义分为两种:列级约束和表级约束 .列及约束的话,可以在列定义的同时,定义外键约束.比如 如果有2张表,主表:T1(A1 )) 要在从表T2中定义外键列这可以: Crea ...

  9. css中box-shadow阴影效果的使用

    text-shadow是给文本添加阴影效果,box-shadow是给元素块添加周边阴影效果.随着HTML5和CSS3的普及,这一特殊效果使用越来越普遍. 基本语法是{box-shadow:[inset ...

  10. Java利用Preferences设置个人偏好

    Java利用Preferences设置个人偏好 Preferences的中文意思即偏好或喜好的意思,也就是说同一个程序在每次运行完后,可以通过Preferences来记录用户的偏好,下次启动时,程序会 ...