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 C++11 (judge verdict: Accepted)

//  aoj-VolumeICPCOOC2015-D  / 2015-11-30
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef ll int__;
#define rep(i,j) for(int__ i=0;i<(int__)(j);i++)
#define repeat(i,j,k) for(int__ i=(j);i<(int__)(k);i++)
#define all(v) v.begin(),v.end()
const int INF = 1 << 30;
//vector
template<typename T>ostream& operator << (ostream &os , const vector<T> &v){
    rep(i,v.size()) os << v[i] << (i!=v.size()-1 ? " " : "\n"); return os;
}
template<typename T>istream& operator >> (istream &is , vector<T> &v){
    rep(i,v.size()) is >> v[i]; return is;
}
//debug
#ifdef DEBUG
void debug(){ cerr << "\033[0m" << endl; }
#endif
template<class F,class...R> void debug(const F &car,const R&... cdr){
#ifdef DEBUG
    cerr << "\033[33m" << car << " "; debug(cdr...);
#endif
}

struct Point{
    int x,y;
    Point():x(0),y(0){}
    Point(int x,int y):x(x),y(y){};
    Point& operator +=(const Point &p){x += p.x; y += p.y; return *this;}
};
ostream &operator << (ostream& os, const Point& p){ return os << "(" << p.x << "," << p.y << ")";}

struct ClosedRange{
    int l,r; //[l,r]
    ClosedRange(){}
    ClosedRange(int l,int r):l(l),r(r){}
    bool operator < (const ClosedRange &a) const { return make_pair(r,l) < make_pair(a.r, a.l); }
};
ostream& operator << (ostream &os, const ClosedRange &r){ return os << "[" << r.l << " " << r.r << "]"; }


// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //


// 2d to 1d
int lower_dimension(Point p, int w, int d){
    return  p.y == 0 ? p.x :
            p.x == w ? w + p.y :
            p.y == d ? w + d + (w - p.x) :
            p.x == 0 ? w*2 + d + (d - p.y) : -1;
}

Point find_cross_point(Point p, Point dp, int w, int d){
    while(0 < p.x and p.x < w and 0 < p.y and p.y < d) p += dp;
    return p;
}

ClosedRange calc_range(Point p, char dir, int w, int d){
    ClosedRange ret;
    Point dp[] = {Point(-1, 1), Point(1, 1), Point(1, -1), Point(-1, -1)};
    map<char, int> dtoi = {{'N', 0}, {'E', 1}, {'S', 2}, {'W', 3}};
    Point dp1 = dp[ ( dtoi[ dir ] + 1 ) % 4 ];
    Point dp2 = dp[ dtoi[ dir ] ];
    Point l = find_cross_point(p, dp1, w, d);
    Point r = find_cross_point(p, dp2, w, d);
    return ClosedRange(lower_dimension(l, w, d), lower_dimension(r, w, d));
}

bool check_used(set<int> &clock, ClosedRange &r){
    if(r.l <= r.r){
        auto itr = clock.lower_bound(r.l);
        return itr != clock.end() ? *itr <= r.r : false;
    } else {
        auto itr = clock.lower_bound(r.l);
        auto itr2 = clock.upper_bound(r.r);
        return  itr  != clock.end()   ? true :
                itr2 != clock.begin() ? *--itr <= r.r : false;
    }
}

int trial(vector<ClosedRange> &range, int start){
    debug("start at ",start);
    int n = range.size();
    set<int> clock;
    for(int i = start; i < start + n; i++){
        if( check_used(clock,range[ i%n ]) ) continue;
        clock.insert(range[ i%n ].r);
        debug("clock at ",range[ i%n ].r);
    }
    return clock.size();
}

bool solve(){
    int n, w, d; cin >> n >> w >> d;
    vector<ClosedRange> range(n);
    rep(i,n){
        int x,y;
        char f; cin >> x >> y >> f;
        range[i] = calc_range(Point(x,y), f, w, d);
        debug(range[i]);
    }   
    sort( all( range ) );
    int ans = INF;
    rep(i, n){
        ans = min(ans, trial(range, i));
    }
    cout << ans << endl;
    return false;
}

int main()
{
    ios::sync_with_stdio(false);
    while(solve());
    return 0;
}