Ferry Loading III[HDU1146]
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]的更多相关文章
- 【HDOJ】1406 Ferry Loading III
模拟,注意需要比较队头与当前时间的大小关系. #include <cstdio> #include <cstring> #include <cstdlib> #de ...
- POJ-2336 Ferry Loading II(简单DP)
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Desc ...
- POJ 2609 Ferry Loading(双塔DP)
Ferry Loading Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1807 Accepted: 509 Sp ...
- poj 2336 Ferry Loading II ( 【贪心】 )
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3704 Accepted: 1884 ...
- poj-2336 Ferry Loading II(dp)
题目链接: Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3946 Accepted: ...
- TOJ 2419: Ferry Loading II
2419: Ferry Loading II Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Subm ...
- Ferry Loading II_贪心
Description Before bridges were common, ferries were used to transport cars across rivers. River fer ...
- [POJ2336]Ferry Loading II
题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...
- Ferry Loading||
uva10440:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...
随机推荐
- zipArchive
ZipArchive *unZip = [[ZipArchive alloc]init]; if ([unZip unzipOpenFile:savePath]) { BOOL ret = [unZi ...
- 浅析 - 提高xib(Interface Builder)高效工作的几个小技巧
本文译自:8 Tips for working effectively with Interface Builder(需FQ)先来看看目录:介绍使view的Size与view中的Content相适应按 ...
- 有关Oracle数据库
创建数据库(DCA):http://jingyan.baidu.com/article/cbcede07cf42ef02f40b4dc2.html 创建表(连接数据库,sql创建数据表):http:/ ...
- php 租房子练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 昨天在公司加班,上午好像就是弄一个ftp的linux服务问题
在网上找了一些方法,可是其中有通过匿名方式登陆,但是在root的权限下才能存放文件,可是把匿名用户登陆取消之后又不能登陆,就是没有列出怎么来添加一个ftp的用户,今天打算直接装一个linux系统在虚拟 ...
- MVC – 7.Razor 语法
7.1 Razor视图引擎语法 Razor通过理解标记的结构来实现代码和标记之间的顺畅切换. @核心转换字符,用来 标记-代码 的转换字符串. 语境A: @{ string rootName=&quo ...
- wifi diplasy流程介绍
转自:http://blog.csdn.net/dnfchan/article/details/8558552/ 另外一篇不错的参考文章:http://www.360doc.com/content/ ...
- 【转载】 Pyqt 利用QDataStream对文件进行存取
# -*- coding: utf-8 -*- from PyQt4.QtGui import * from PyQt4.QtCore import * import sys QTextCodec.s ...
- python多线程之Event(事件)
#!/usr/bin/env python # -*- coding: utf-8 -*- import time from threading import Thread, Event import ...
- 无废话ExtJs 入门教程五[文本框:TextField]
无废话ExtJs 入门教程五[文本框:TextField] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在表单里加了个两个文本框.如下所示代码区的第42行位置,items: ...