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. NYOJ题目112指数运算

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs0AAAIICAIAAAAaCETRAAAgAElEQVR4nO3drW7jWtwv4PcmwnMhxb ...

  2. 2.3顺序容器-deque

    deque(双向队列) 1) *    :包含deque头文件 **  :deque也是一个可变长数组,适用于vector的操作都适用于deque ***:对比vector的优势在于在头部存取元素可以 ...

  3. Bitmap在Java中的实现和应用

    >>40亿数据排序问题 给定一个最多包含40亿个随机排列的32位整数的顺序文件,找出一个不在文件中的32位整数(在文件中至少缺失这样一个数——为什么?).在具有足够内存的情况下,如何解决该 ...

  4. Acdream 1111:LSS(水题,字符串处理)

    LSS Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Others) SubmitStati ...

  5. 开个坑, 写个阿里云开放储存服务(OSS)的C++版SDK以及客户端

    这应该是继我研究手册QQ协议后的第2个稍微正式一点的网络程序, 不只是Scoket套接字编程, 还涉及到更多的HTTP协议知识! 阿里云开放储存服务OSS官方已经提供了不少SDK, 包括PHP/Pyt ...

  6. android 入门-Activity及 字体

    private Button sButton; private TextView mTextView; private Button fButton; ,,,,}; @Override protect ...

  7. 二叉搜索树的实现及指针问题的一点思考(C++)

    今天实现二叉搜索树的时候因为指针的问题卡了一上午(实在不应该...),一直segmentation fault,个人感觉还是需要记录一下的. 首先贴一下做的题的意思: 输入一系列整数,建立二叉排序数, ...

  8. hdu 4035 2011成都赛区网络赛E 概率dp ****

    太吊了,反正我不会 /* HDU 4035 dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点 ...

  9. C泊车管理系统

    // //  main.c //  泊车管理系统 // //  Created by 丁小未 on 13-7-14. //  Copyright (c) 2013年 dingxiaowei. All ...

  10. 有关servlet初学者的资源和建议

    四天来学习servlet是很痛苦的经历,其实可以不必要这么痛苦,关键是一定要学会冷静的分析问题与解决问题,要不言学习也没有那么多的乐趣.初学java刚满15天. 首先对于资源来说建议先读一点点的PPT ...