Problem:
Problem B: The Last Ant

A straight tunnel without branches is crowded with busy ants coming and going. Some ants walk left to right and others right to left. All ants walk at a constant speed of 1 cm/s. When two ants meet, they try to pass each other. However, some sections of the tunnel are narrow and two ants cannot pass each other. When two ants meet at a narrow section, they turn around and start walking in the opposite directions. When an ant reaches either end of the tunnel, it leaves the tunnel.

The tunnel has an integer length in centimeters. Every narrow section of the tunnel is integer centimeters distant from the both ends. Except for these sections, the tunnel is wide enough for ants to pass each other. All ants start walking at distinct narrow sections. No ants will newly enter the tunnel. Consequently, all the ants in the tunnel will eventually leave it. Your task is to write a program that tells which is the last ant to leave the tunnel and when it will.

Figure B.1 shows the movements of the ants during the first two seconds in a tunnel 6 centimeters long. Initially, three ants, numbered 1, 2, and 3, start walking at narrow sections, 1, 2, and 5 centimeters distant from the left end, respectively. After 0.5 seconds, the ants 1 and 2 meet at a wide section, and they pass each other. Two seconds after the start, the ants 1 and 3 meet at a narrow section, and they turn around.

Figure B.1 corresponds to the first dataset of the sample input.

Figure B.1. Movements of ants

Input

The input consists of one or more datasets. Each dataset is formatted as follows.

n l
d1 p1
d2 p2
...
dn pn

The first line of a dataset contains two integers separated by a space. n (1 ≤ n ≤ 20) represents the number of ants, and l (n + 1 ≤ l ≤ 100) represents the length of the tunnel in centimeters. The following n lines describe the initial states of ants. Each of the lines has two items, di and pi, separated by a space. Ants are given numbers 1 through n. The ant numbered i has the initial direction di and the initial position pi. The initial direction di (1 ≤ i ≤ n) is L (to the
left) or R (to the right). The initial position pi (1 ≤ i ≤ n) is an integer specifying the distance from the left end of the tunnel in centimeters. Ants are listed in the left to right order, that is, 1 ≤ p1 < p2 < ... < pn ≤ l - 1.

The last dataset is followed by a line containing two zeros separated by a space.

Output

For each dataset, output how many seconds it will take before all the ants leave the tunnel, and which of the ants will be the last. The last ant is identified by its number. If two ants will leave at the same time, output the number indicating the ant that will leave through the left end of the tunnel.

Sample Input

3 6
R 1
L 2
L 5
1 10
R 1
2 10
R 5
L 7
2 10
R 3
L 8
2 99
R 1
L 98
4 10
L 1
R 2
L 8
R 9
6 10
R 2
R 3
L 4
R 6
L 7
L 8
0 0

Output for the Sample Input

5 1
9 1
7 1
8 2
98 2
8 2
8 3

Solution in JAVA (judge verdict: Accepted)


import java.io.*;
import java.util.*;

public class Main {
	FastScanner in = new FastScanner(System.in);
	PrintWriter out = new PrintWriter(System.out);

	class Ant {
		char d;
		int p;

		Ant(char d, int p) {
			this.d = d;
			this.p = p;
		}
	}
	
	public void run() {
		outer : 
		while (true) {
			int n = in.nextInt(), l = in.nextInt();
			if (n == 0) break;
			
			Ant[] ant = new Ant[n];
			for (int i = 0; i < n; i++) {
				ant[i] = new Ant(in.next().charAt(0), in.nextInt());
			}
			
			int cnt = 0;
			for (int t = 1; ; t++) {
				for (int i = 0; i < n; i++) {
					if (ant[i].p == 0 || ant[i].p == l) continue;
					
					if (ant[i].d == 'R') {
						ant[i].p++;
					} 
					
					if (ant[i].p == 0 || ant[i].p == l) {
						cnt++;
						if (cnt == n) {
							System.out.println(t + " " + (i+1));
							continue outer;
						}
					}
				}
				
				for (int i = 0; i < n; i++) {
					if (ant[i].p == 0 || ant[i].p == l) continue;
					
					if (ant[i].d == 'L') {
						ant[i].p--;
					} 
					
					if (ant[i].p == 0 || ant[i].p == l) {
						cnt++;
						if (cnt == n) {
							System.out.println(t + " " + (i+1));
							continue outer;
						}
					}
				}
				
				for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) {
					if (ant[i].p == ant[j].p) {
						char temp = ant[i].d;
						ant[i].d = ant[j].d;
						ant[j].d = temp;
					}
				}
			}
		}
		out.close();
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");

		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}

		System.out.println("----------------------------");
		System.out.println();
	}

	public void debug(Object... obj) {
		System.out.println(Arrays.deepToString(obj));
	}

	class FastScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FastScanner(InputStream stream) {
			this.stream = stream;
			//stream = new FileInputStream(new File("dec.in"));

		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();

			return array;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();

			return array;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();

			return array;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}