Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem D
Wall Clocks

You are the manager of a chocolate sales team. Your team customarily takes tea breaks every two hours, during which varieties of new chocolate products of your company are served. Everyone looks forward to the tea breaks so much that they frequently give a glance at a wall clock.

Recently, your team has moved to a new office. You have just arranged desks in the office. One team member asked you to hang a clock on the wall in front of her desk so that she will not be late for tea breaks. Naturally, everyone seconded her.

You decided to provide an enough number of clocks to be hung in the field of view of everyone. Your team members will be satisfied if they have at least one clock (regardless of the orientation of the clock) in their view, or, more precisely, within 45 degrees left and 45 degrees right (both ends inclusive) from the facing directions of their seats. In order to buy as few clocks as possible, you should write a program that calculates the minimum number of clocks needed to
meet everyone's demand.

The office room is rectangular aligned to north-south and east-west directions. As the walls are tall enough, you can hang clocks even above the door and can assume one's eyesight is not blocked by other members or furniture. You can also assume that each clock is a point (of size zero), and so you can hang a clock even on a corner of the room.

For example, assume that there are two members. If they are sitting facing each other at positions shown in Figure D.1(A), you need to provide two clocks as they see distinct sections of the wall. If their seats are arranged as shown in Figure D.1(B), their fields of view have a common point on the wall. Thus, you can meet their demands by hanging a single clock at the point. In Figure D.1(C), their fields of view have a common wall section. You can meet their demands with a single clock by hanging it anywhere in the section. Arrangements (A), (B), and (C) in Figure D.1 correspond to Sample Input 1, 2, and 3, respectively.

Input

The input consists of a single test case, formatted as follows.

$n$ $w$ $d$

$x_1$ $y_1$ $f_1$

...

$x_n$ $y_n$ $f_n$

Figure D.1. Arrangements of seats and clocks. Gray area indicates field of view.

All numbers in the test case are integers. The first line contains the number of team members $n$ $(1 \leq n \leq 1,000)$ and the size of the office room $w$ and $d$ $(2 \leq w, d \leq 100,000)$. The office room has its width $w$ east-west, and depth $d$ north-south. Each of the following $n$ lines indicates the position and the orientation of the seat of a team member. Each member has a seat at a distinct position $(x_i, y_i)$ facing the direction $f_i$, for $i = 1, ..., n$. Here $1 \leq x_i \leq w - 1, 1 \leq y_i \leq d - 1$, and $f_i$ is one of N, E, W, and S, meaning north, east, west, and south, respectively. The position $(x, y)$ means $x$ distant from the west wall and $y$ distant from the south wall.

Output

Print the minimum number of clocks needed.

Sample Input 1

2 10 6
4 4 E
6 4 W

Sample Output 1

2

Sample Input 2

2 10 6
2 4 E
6 4 W

Sample Output 2

1

Sample Input 3

2 10 6
3 2 S
6 4 W

Sample Output 3

1

Sample Input 4

6 10 6
1 5 N
7 1 N
8 2 E
9 1 S
4 4 S
3 3 W

Sample Output 4

3

Sample Input 5

4 10 6
4 3 W
2 4 N
4 4 W
3 3 S

Sample Output 5

2

Sample Input 6

4 100000 40000
25000 25000 S
20000 30000 S
75000 25000 S
80000 30000 S

Sample Output 6

1

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;


public class Main {
	
	public static int[][] dirs = {{1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
	
	public static Map<String, Integer> dirToIndex = new HashMap<String, Integer>();
	static {
		dirToIndex.put("E", 0);
		dirToIndex.put("S", 1);
		dirToIndex.put("W", 2);
		dirToIndex.put("N", 3);
	}
	
	// clock-wise
	public static int convertTo1D(final int x, final int y, final int w, final int h){
		if(x == 0){
			return y;
		}else if(y == h){
			return h + x;
		}else if(x == w){
			return h + w + (h - y);
		}else {
			return h + w + h + (w - x);
		}
	}
	
	public static int convertLeft(final int x, final int y, final int w, final int h, final int dir){
		final int x_sign = dirs[dir][0];
		final int y_sign = dirs[dir][1];
		
		final int x_lim = x_sign > 0 ? w : 0;
		final int y_lim = y_sign > 0 ? h : 0;
		
		final int x_diff = Math.abs(x_lim - x);
		final int y_diff = Math.abs(y_lim - y);
		
		final int min_diff = Math.min(x_diff, y_diff);
		
		return convertTo1D(x + min_diff * x_sign, y + min_diff * y_sign, w, h);
	}
	
	public static int convertRight(final int x, final int y, final int w, final int h, final int dir){
		return convertLeft(x, y, w, h, (dir + 1) % dirs.length);
	}
	
	public static class Pair implements Comparable<Pair> {
		int position, value;
		
		public Pair(int position, int value){
			this.position = position;
			this.value = value;
		}

		@Override
		public String toString(){
			return this.position + " " + this.value;
		}
		
		@Override
		public int compareTo(Pair o) {
			if(Integer.compare(this.position, o.position) != 0){
				return Integer.compare(this.position, o.position);
			}else{
				return Integer.compare(o.value, this.value);
			}
		}
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int n = sc.nextInt();
		final int w = sc.nextInt();
		final int h = sc.nextInt();
		
		int[] ls = new int[n];
		int[] rs = new int[n];
		
		for(int i = 0; i < n; i++){
			final int x = sc.nextInt();
			final int y = sc.nextInt();
			final String d = sc.next();
			
			ls[i] = convertLeft(x, y, w, h, dirToIndex.get(d));
			rs[i] = convertRight(x, y, w, h, dirToIndex.get(d));
			if(ls[i] > rs[i]){ rs[i] += 2 * (h + w); }
		}
		
		//System.out.println(Arrays.toString(ls));
		//System.out.println(Arrays.toString(rs));
		
		int min = Integer.MAX_VALUE;
		
		for(int i = 0; i < n; i++){
			int[] n_ls = new int[n];
			int[] n_rs = new int[n];
			System.arraycopy(ls, 0, n_ls, 0, n);
			System.arraycopy(rs, 0, n_rs, 0, n);
			
			final int origin = ls[i];
			for(int j = 0; j < n; j++){
				n_ls[j] -= origin;
				while(n_ls[j] < 0){ n_ls[j] += 2 * (w + h); }
				while(n_ls[j] >= 2 * (w + h)){ n_ls[j] -= 2 * (w + h); }
				n_rs[j] -= origin;
				while(n_rs[j] < 0){ n_rs[j] += 2 * (w + h); }
				while(n_rs[j] >= 2 * (w + h)){ n_rs[j] -= 2 * (w + h); }
				
				if(n_rs[j] < n_ls[j]){
					int tmp = n_ls[j];
					n_ls[j] = n_rs[j];
					n_rs[j] = tmp;
				}
				//System.out.println(n_ls[j] + " " + n_rs[j]);
			}
			
			PriorityQueue<Pair> queue = new PriorityQueue<Pair>();
			for(int j = 0; j < n; j++){
				if(n_ls[j] <= n_rs[i] && n_rs[i] <= n_rs[j]){
					//System.out.println(ls[j] + " " + origin + " " + rs[i]);
					continue;
				}
				
				queue.add(new Pair(n_ls[j], +1));
				queue.add(new Pair(n_rs[j], -1));
			}
			
			int score = 1;
			int count = 0;
			//System.out.println(queue);
			while(!queue.isEmpty()){
				final Pair p = queue.poll();
				
				//System.out.println(p.position + " " + p.value);
				
				if(p.value >= 1){
					count++;
				}else if(count >= 1){
					count = 0;
					score++;
				}
			}
			//System.out.println();
			
			//if(count == 0){
				min = Math.min(min, score);
			//}
		}
		
		System.out.println(min);
		
		sc.close();
	}	
}