HONEYGRAHI AND ZOMBIES

0

0 votes
Problem

Prof. Honeygrahi has turned the entire CS department into zombies who do nothing but read Algorithms textbooks. There are N students (zombies). They are situated at various points on a long 1-D plank. Each zombie faces either left or right. All zombies are moving with a constant speed S. When a zombie reaches one of the ends of the plank, he/she falls down. When two zombies collide face to face, they reverse their directions and keep walking with the same speed.

Prof. Honeygrahi watches the entire escapade and thoroughly enjoys himself. However, he has to fix a meeting with the new director and hence, wants to know how much time it will take for all zombies to fall off the plank.

INPUT:

First line of input contains the number of test cases T. T test cases follow.

The first line of each test case contains the numbers L, S, N. These are the length of the plank(in m), the speed of the zombies(in m/s), and the number of zombies respectively.

Then N lines follow. Each line contains the position of the ith zombie on the plank, followed by one of two letters "L" or "R" (without quotes). The position is measured from the left end of the plank, and the letter represents the direction the zombie initially faces (left or right).

OUTPUT:

For each test case, print the time taken for all zombies to fall off the plank. If all zombies never fall off, print -1.

Output only the integer part. Example - if time taken is 4.4s, output 4. If time taken is 5.8s, then output 5.

CONSTRAINTS:

All numbers in the input are integers.

0 <= L <= 1000000

0 <= N <= 50000

1 <= S <= 1000

N <= L

NOTE: Use scanf/printf instead of cin/cout.

PROBLEM SETTER: Siddharth Ancha

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation
  1. In the 1st test case, zombie is at 3m and keeps moving right with a speed of 1m/s. He needs to cover 4m before he falls off. Hence time taken is 4.0s. Hence output is 4.

  2. In the 2nd test case, zombie 1 takes 0.25s to fall off the plank and zombie 2 takes 1.5s to fall off the plank. Total time taken=1.5s. Hence output is 1.

Editor Image

?