df56ee954d 2011-12-29 kinaba: #include <iostream> df56ee954d 2011-12-29 kinaba: #include <sstream> df56ee954d 2011-12-29 kinaba: #include <iomanip> df56ee954d 2011-12-29 kinaba: #include <vector> df56ee954d 2011-12-29 kinaba: #include <string> df56ee954d 2011-12-29 kinaba: #include <map> df56ee954d 2011-12-29 kinaba: #include <set> df56ee954d 2011-12-29 kinaba: #include <algorithm> df56ee954d 2011-12-29 kinaba: #include <numeric> df56ee954d 2011-12-29 kinaba: #include <iterator> df56ee954d 2011-12-29 kinaba: #include <functional> df56ee954d 2011-12-29 kinaba: #include <complex> df56ee954d 2011-12-29 kinaba: #include <queue> df56ee954d 2011-12-29 kinaba: #include <stack> df56ee954d 2011-12-29 kinaba: #include <cmath> df56ee954d 2011-12-29 kinaba: #include <cassert> df56ee954d 2011-12-29 kinaba: #include <cstring> df56ee954d 2011-12-29 kinaba: #ifdef __GNUC__ df56ee954d 2011-12-29 kinaba: #include <ext/hash_map> df56ee954d 2011-12-29 kinaba: #define unordered_map __gnu_cxx::hash_map df56ee954d 2011-12-29 kinaba: #else df56ee954d 2011-12-29 kinaba: #include <unordered_map> df56ee954d 2011-12-29 kinaba: #endif df56ee954d 2011-12-29 kinaba: using namespace std; df56ee954d 2011-12-29 kinaba: typedef long long LL; df56ee954d 2011-12-29 kinaba: typedef complex<double> CMP; df56ee954d 2011-12-29 kinaba: df56ee954d 2011-12-29 kinaba: class Mosquitoes { public: df56ee954d 2011-12-29 kinaba: int getMaximum(vector <int> xInit, vector <int> v, int R) df56ee954d 2011-12-29 kinaba: { df56ee954d 2011-12-29 kinaba: vector< pair<double,double> > tx; df56ee954d 2011-12-29 kinaba: for (int i = 0; i < xInit.size(); i++) df56ee954d 2011-12-29 kinaba: for (int j = i+1; j < xInit.size(); j++) df56ee954d 2011-12-29 kinaba: critical_points(xInit[i], v[i], xInit[j], v[j], R, &tx); df56ee954d 2011-12-29 kinaba: df56ee954d 2011-12-29 kinaba: int best = 1; df56ee954d 2011-12-29 kinaba: for(int i=0; i<tx.size(); ++i) { df56ee954d 2011-12-29 kinaba: double t = tx[i].first; df56ee954d 2011-12-29 kinaba: double x = tx[i].second; df56ee954d 2011-12-29 kinaba: if( t >= 0 ) { df56ee954d 2011-12-29 kinaba: int cnt = 0; df56ee954d 2011-12-29 kinaba: for (int k = 0; k < xInit.size(); k++) df56ee954d 2011-12-29 kinaba: if( is_in(xInit[k], v[k], t, x, R) ) df56ee954d 2011-12-29 kinaba: ++cnt; df56ee954d 2011-12-29 kinaba: best = max(best, cnt); df56ee954d 2011-12-29 kinaba: } df56ee954d 2011-12-29 kinaba: } df56ee954d 2011-12-29 kinaba: return best; df56ee954d 2011-12-29 kinaba: } df56ee954d 2011-12-29 kinaba: df56ee954d 2011-12-29 kinaba: void critical_points(double x1, double v1, double x2, double v2, double R, df56ee954d 2011-12-29 kinaba: vector< pair<double,double> >* tx) df56ee954d 2011-12-29 kinaba: { df56ee954d 2011-12-29 kinaba: // Compute t such that | (x1 + t v1) - (x2 - t v2) | = 2R df56ee954d 2011-12-29 kinaba: double x = x1 - x2; df56ee954d 2011-12-29 kinaba: double v = v1 - v2; df56ee954d 2011-12-29 kinaba: if( v == 0 ) { df56ee954d 2011-12-29 kinaba: if( abs(x) <= 2*R ) df56ee954d 2011-12-29 kinaba: tx->push_back( make_pair(0, (x1+x2)/2) ); df56ee954d 2011-12-29 kinaba: } else { df56ee954d 2011-12-29 kinaba: double t1 = (2*R - x) / v; df56ee954d 2011-12-29 kinaba: double t2 = (2*R + x) / (-v); df56ee954d 2011-12-29 kinaba: tx->push_back( make_pair(t1, (func(x1,v1,t1)+func(x2,v2,t1))/2) ); df56ee954d 2011-12-29 kinaba: tx->push_back( make_pair(t2, (func(x1,v1,t2)+func(x2,v2,t2))/2) ); df56ee954d 2011-12-29 kinaba: } df56ee954d 2011-12-29 kinaba: } df56ee954d 2011-12-29 kinaba: df56ee954d 2011-12-29 kinaba: double func(double x, double v, double t) df56ee954d 2011-12-29 kinaba: { df56ee954d 2011-12-29 kinaba: return x + v*t; df56ee954d 2011-12-29 kinaba: } df56ee954d 2011-12-29 kinaba: df56ee954d 2011-12-29 kinaba: bool is_in(double x, double v, double t, double c, double R) df56ee954d 2011-12-29 kinaba: { df56ee954d 2011-12-29 kinaba: return abs(func(x,v,t) - c) <= R + 1e-8; df56ee954d 2011-12-29 kinaba: } df56ee954d 2011-12-29 kinaba: }; df56ee954d 2011-12-29 kinaba: df56ee954d 2011-12-29 kinaba: // BEGIN CUT HERE df56ee954d 2011-12-29 kinaba: #include <ctime> df56ee954d 2011-12-29 kinaba: double start_time; string timer() df56ee954d 2011-12-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } df56ee954d 2011-12-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) df56ee954d 2011-12-29 kinaba: { os << "{ "; df56ee954d 2011-12-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) df56ee954d 2011-12-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } df56ee954d 2011-12-29 kinaba: void verify_case(const int& Expected, const int& Received) { df56ee954d 2011-12-29 kinaba: bool ok = (Expected == Received); df56ee954d 2011-12-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; df56ee954d 2011-12-29 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } df56ee954d 2011-12-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); df56ee954d 2011-12-29 kinaba: #define END verify_case(_, Mosquitoes().getMaximum(xInit, v, R));} df56ee954d 2011-12-29 kinaba: int main(){ df56ee954d 2011-12-29 kinaba: df56ee954d 2011-12-29 kinaba: CASE(0) df56ee954d 2011-12-29 kinaba: int xInit_[] = {1, -1}; df56ee954d 2011-12-29 kinaba: vector <int> xInit(xInit_, xInit_+sizeof(xInit_)/sizeof(*xInit_)); df56ee954d 2011-12-29 kinaba: int v_[] = {1, -1}; df56ee954d 2011-12-29 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); df56ee954d 2011-12-29 kinaba: int R = 10; df56ee954d 2011-12-29 kinaba: int _ = 2; df56ee954d 2011-12-29 kinaba: END df56ee954d 2011-12-29 kinaba: CASE(1) df56ee954d 2011-12-29 kinaba: int xInit_[] = {100, -100}; df56ee954d 2011-12-29 kinaba: vector <int> xInit(xInit_, xInit_+sizeof(xInit_)/sizeof(*xInit_)); df56ee954d 2011-12-29 kinaba: int v_[] = {1, -1}; df56ee954d 2011-12-29 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); df56ee954d 2011-12-29 kinaba: int R = 10; df56ee954d 2011-12-29 kinaba: int _ = 1; df56ee954d 2011-12-29 kinaba: END df56ee954d 2011-12-29 kinaba: CASE(2) df56ee954d 2011-12-29 kinaba: int xInit_[] = {0, -1, 10, -11, 99, -99}; df56ee954d 2011-12-29 kinaba: vector <int> xInit(xInit_, xInit_+sizeof(xInit_)/sizeof(*xInit_)); df56ee954d 2011-12-29 kinaba: int v_[] = {1, -1, -3, 3, 47, -47}; df56ee954d 2011-12-29 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); df56ee954d 2011-12-29 kinaba: int R = 3; df56ee954d 2011-12-29 kinaba: int _ = 4; df56ee954d 2011-12-29 kinaba: END df56ee954d 2011-12-29 kinaba: CASE(3) df56ee954d 2011-12-29 kinaba: int xInit_[] = {5}; df56ee954d 2011-12-29 kinaba: vector <int> xInit(xInit_, xInit_+sizeof(xInit_)/sizeof(*xInit_)); df56ee954d 2011-12-29 kinaba: int v_[] = {2}; df56ee954d 2011-12-29 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); df56ee954d 2011-12-29 kinaba: int R = 8; df56ee954d 2011-12-29 kinaba: int _ = 1; df56ee954d 2011-12-29 kinaba: END df56ee954d 2011-12-29 kinaba: CASE(4) df56ee954d 2011-12-29 kinaba: int xInit_[] = {12,34,56,78,90}; df56ee954d 2011-12-29 kinaba: vector <int> xInit(xInit_, xInit_+sizeof(xInit_)/sizeof(*xInit_)); df56ee954d 2011-12-29 kinaba: int v_[] = {-1,2,-3,4,-5}; df56ee954d 2011-12-29 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); df56ee954d 2011-12-29 kinaba: int R = 6; df56ee954d 2011-12-29 kinaba: int _ = 3; df56ee954d 2011-12-29 kinaba: END df56ee954d 2011-12-29 kinaba: CASE(5) df56ee954d 2011-12-29 kinaba: int xInit_[] = {-29, -27}; df56ee954d 2011-12-29 kinaba: vector <int> xInit(xInit_, xInit_+sizeof(xInit_)/sizeof(*xInit_)); df56ee954d 2011-12-29 kinaba: int v_[] = {25, 61}; df56ee954d 2011-12-29 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); df56ee954d 2011-12-29 kinaba: int R = 7; df56ee954d 2011-12-29 kinaba: int _ = 2; df56ee954d 2011-12-29 kinaba: END df56ee954d 2011-12-29 kinaba: /* df56ee954d 2011-12-29 kinaba: CASE(6) df56ee954d 2011-12-29 kinaba: int xInit_[] = ; df56ee954d 2011-12-29 kinaba: vector <int> xInit(xInit_, xInit_+sizeof(xInit_)/sizeof(*xInit_)); df56ee954d 2011-12-29 kinaba: int v_[] = ; df56ee954d 2011-12-29 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); df56ee954d 2011-12-29 kinaba: int R = ; df56ee954d 2011-12-29 kinaba: int _ = ; df56ee954d 2011-12-29 kinaba: END df56ee954d 2011-12-29 kinaba: */ df56ee954d 2011-12-29 kinaba: } df56ee954d 2011-12-29 kinaba: // END CUT HERE