8166493378 2012-05-12 kinaba: #include <iostream> 8166493378 2012-05-12 kinaba: #include <sstream> 8166493378 2012-05-12 kinaba: #include <iomanip> 8166493378 2012-05-12 kinaba: #include <vector> 8166493378 2012-05-12 kinaba: #include <string> 8166493378 2012-05-12 kinaba: #include <map> 8166493378 2012-05-12 kinaba: #include <set> 8166493378 2012-05-12 kinaba: #include <algorithm> 8166493378 2012-05-12 kinaba: #include <numeric> 8166493378 2012-05-12 kinaba: #include <iterator> 8166493378 2012-05-12 kinaba: #include <functional> 8166493378 2012-05-12 kinaba: #include <complex> 8166493378 2012-05-12 kinaba: #include <queue> 8166493378 2012-05-12 kinaba: #include <stack> 8166493378 2012-05-12 kinaba: #include <cmath> 8166493378 2012-05-12 kinaba: #include <cassert> 8166493378 2012-05-12 kinaba: #include <cstring> 8166493378 2012-05-12 kinaba: #ifdef __GNUC__ 8166493378 2012-05-12 kinaba: #include <ext/hash_map> 8166493378 2012-05-12 kinaba: #define unordered_map __gnu_cxx::hash_map 8166493378 2012-05-12 kinaba: #else 8166493378 2012-05-12 kinaba: #include <unordered_map> 8166493378 2012-05-12 kinaba: #endif 8166493378 2012-05-12 kinaba: using namespace std; 8166493378 2012-05-12 kinaba: typedef long long LL; 8166493378 2012-05-12 kinaba: typedef complex<double> CMP; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: class AntsMeet { public: 8166493378 2012-05-12 kinaba: int countAnts(vector <int> x, vector <int> y, string direction) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: const int N = x.size(); 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: map< int, vector< pair<int,int> > > bye; 8166493378 2012-05-12 kinaba: for(int i=0; i<N; ++i) 8166493378 2012-05-12 kinaba: for(int j=i+1; j<N; ++j) { 8166493378 2012-05-12 kinaba: int t; 8166493378 2012-05-12 kinaba: if( meet(x[i], y[i], direction[i], x[j], y[j], direction[j], &t) ) 8166493378 2012-05-12 kinaba: bye[t].push_back(make_pair(i,j)); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: set<int> dead; 8166493378 2012-05-12 kinaba: for(map<int, vector< pair<int,int> > >::iterator it=bye.begin(); it!=bye.end(); ++it) { 8166493378 2012-05-12 kinaba: set<int> die; 8166493378 2012-05-12 kinaba: vector< pair<int,int> >& events = it->second; 8166493378 2012-05-12 kinaba: for(int i=0; i<events.size(); ++i) { 8166493378 2012-05-12 kinaba: int a = events[i].first; 8166493378 2012-05-12 kinaba: int b = events[i].second; 8166493378 2012-05-12 kinaba: if( dead.count(a)==0 && dead.count(b)==0 ) { 8166493378 2012-05-12 kinaba: die.insert(a); 8166493378 2012-05-12 kinaba: die.insert(b); 8166493378 2012-05-12 kinaba: // dead.insert(a); 8166493378 2012-05-12 kinaba: // dead.insert(b); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: dead.insert(die.begin(), die.end()); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: return N - dead.size(); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: bool meet(int x, int y, char d, int X, int Y, int D, int* t) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: if( d == D ) 8166493378 2012-05-12 kinaba: return false; 8166493378 2012-05-12 kinaba: int dx = dx_of(d); 8166493378 2012-05-12 kinaba: int dy = dy_of(d); 8166493378 2012-05-12 kinaba: int DX = dx_of(D); 8166493378 2012-05-12 kinaba: int DY = dy_of(D); 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: int tx; 8166493378 2012-05-12 kinaba: if( !meet(x, dx, X, DX, &tx) ) 8166493378 2012-05-12 kinaba: return false; 8166493378 2012-05-12 kinaba: int ty; 8166493378 2012-05-12 kinaba: if( !meet(y, dy, Y, DY, &ty) ) 8166493378 2012-05-12 kinaba: return false; 8166493378 2012-05-12 kinaba: *t = (tx==-1 ? ty : tx); 8166493378 2012-05-12 kinaba: return (tx==ty || tx==-1 || ty==-1); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: bool meet(int x, int dx, int X, int DX, int* t) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: if( x==X && dx==DX ) { 8166493378 2012-05-12 kinaba: *t = -1; 8166493378 2012-05-12 kinaba: return true; 8166493378 2012-05-12 kinaba: } else if( dx==DX ) { 8166493378 2012-05-12 kinaba: return false; 8166493378 2012-05-12 kinaba: } else { 8166493378 2012-05-12 kinaba: *t = (x-X)*2 / (DX-dx); 8166493378 2012-05-12 kinaba: return ( *t >= 0 ); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: int dx_of(char d) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: if(d=='E') return +1; 8166493378 2012-05-12 kinaba: if(d=='W') return -1; 8166493378 2012-05-12 kinaba: return 0; 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: int dy_of(char d) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: if(d=='N') return +1; 8166493378 2012-05-12 kinaba: if(d=='S') return -1; 8166493378 2012-05-12 kinaba: return 0; 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: }; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: // BEGIN CUT HERE 8166493378 2012-05-12 kinaba: #include <ctime> 8166493378 2012-05-12 kinaba: double start_time; string timer() 8166493378 2012-05-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 8166493378 2012-05-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 8166493378 2012-05-12 kinaba: { os << "{ "; 8166493378 2012-05-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 8166493378 2012-05-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 8166493378 2012-05-12 kinaba: void verify_case(const int& Expected, const int& Received) { 8166493378 2012-05-12 kinaba: bool ok = (Expected == Received); 8166493378 2012-05-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 8166493378 2012-05-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 8166493378 2012-05-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 8166493378 2012-05-12 kinaba: #define END verify_case(_, AntsMeet().countAnts(x, y, direction));} 8166493378 2012-05-12 kinaba: int main(){ 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: CASE(0) 8166493378 2012-05-12 kinaba: int x_[] = {0,10,20,30}; 8166493378 2012-05-12 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 8166493378 2012-05-12 kinaba: int y_[] = {0,10,20,30}; 8166493378 2012-05-12 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 8166493378 2012-05-12 kinaba: string direction = "NWNE"; 8166493378 2012-05-12 kinaba: int _ = 2; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(1) 8166493378 2012-05-12 kinaba: int x_[] = {-10,0,0,10}; 8166493378 2012-05-12 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 8166493378 2012-05-12 kinaba: int y_[] = {0,-10,10,0}; 8166493378 2012-05-12 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 8166493378 2012-05-12 kinaba: string direction = "NEWS"; 8166493378 2012-05-12 kinaba: int _ = 0; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(2) 8166493378 2012-05-12 kinaba: int x_[] = {-1,-1,-1,0,0,0,1,1,1}; 8166493378 2012-05-12 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 8166493378 2012-05-12 kinaba: int y_[] = {-1,0,1,-1,0,1,-1,0,1}; 8166493378 2012-05-12 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 8166493378 2012-05-12 kinaba: string direction = "ESEWNNEWW"; 8166493378 2012-05-12 kinaba: int _ = 4; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(3) 8166493378 2012-05-12 kinaba: int x_[] = {4,7,6,2,6,5,7,7,8,4,7,8,8,8,5,4,8,9,1,5,9,3,4,0,0,1,0,7,2,6,9,6,3,0,5,5,1,2,0,4,9,7,7,1,8,1,9,2,7,3}; 8166493378 2012-05-12 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 8166493378 2012-05-12 kinaba: int y_[] = {2,3,0,6,8,4,9,0,5,0,2,4,3,8,1,5,0,7,3,7,0,9,8,1,9,4,7,8,1,1,6,6,6,2,8,5,1,9,0,1,1,1,7,0,2,5,4,7,5,3}; 8166493378 2012-05-12 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 8166493378 2012-05-12 kinaba: string direction = "SSNWSWSENSWSESWEWSWSENWNNNESWSWSWWSSWEEWWNWWWNWENN" ; 8166493378 2012-05-12 kinaba: int _ = 25; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(4) 8166493378 2012-05-12 kinaba: int x_[] = {478,-664,759,434,-405,513,565,-396,311,-174,56,993,251,-341,993,-112,242,129,383,513,-78,-341,-148,129,423 8166493378 2012-05-12 kinaba: ,493,434,-405,478,-148,929,251,56,242,929,-78,423,-664,802,251,759,383,-112,-591,-591,-248,660,660,735,493}; 8166493378 2012-05-12 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 8166493378 2012-05-12 kinaba: int y_[] = {-186,98,948,795,289,-678,948,-170,-195,290,-354,-424,289,-157,-166,150,706,-678,684,-294,-234,36,36,-294,-216 8166493378 2012-05-12 kinaba: ,-234,427,945,265,-157,265,715,275,715,-186,337,798,-170,427,706,754,961,286,-216,798,286,961,684,-424,337}; 8166493378 2012-05-12 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 8166493378 2012-05-12 kinaba: string direction = "WNSNNSSWWWEENWESNSWSWSEWWEWEWWWNWESNSSNNSNNWWWNESE"; 8166493378 2012-05-12 kinaba: int _ = 44; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(5) 8166493378 2012-05-12 kinaba: int x_[] = {0, 1, 0, 10, 9, 11}; 8166493378 2012-05-12 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 8166493378 2012-05-12 kinaba: int y_[] = {0, 1, 2, 10, 11, 11}; 8166493378 2012-05-12 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 8166493378 2012-05-12 kinaba: string direction = "NWSNES"; 8166493378 2012-05-12 kinaba: int _ = 0; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: /* 8166493378 2012-05-12 kinaba: CASE(6) 8166493378 2012-05-12 kinaba: int x_[] = ; 8166493378 2012-05-12 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 8166493378 2012-05-12 kinaba: int y_[] = ; 8166493378 2012-05-12 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 8166493378 2012-05-12 kinaba: string direction = ; 8166493378 2012-05-12 kinaba: int _ = ; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: */ 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: // END CUT HERE