4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void gen( vector<int>& X, vector<LL>& R, int n ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int j = 0; 4fd800b3a8 2011-02-23 kinaba: int m = X.size(); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: R[i] = X[j]; 4fd800b3a8 2011-02-23 kinaba: int s = (j+1)%m; 4fd800b3a8 2011-02-23 kinaba: X[j] = ( ( X[j] ^ X[s] ) + 13 ) % 49999; 4fd800b3a8 2011-02-23 kinaba: j = s; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class RandomFights 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: double expectedNrOfPoints(vector <int> Aseed, vector <int> Bseed, int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<LL> A(n), B(n); 4fd800b3a8 2011-02-23 kinaba: gen(Aseed, A, n); 4fd800b3a8 2011-02-23 kinaba: gen(Bseed, B, n); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // expected gain by A[i] = sqSum{B[j] | B[j]<A[i]} - sqSum{B[j] | B[j]>A[i]} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: sort(B.begin(), B.end()); 4fd800b3a8 2011-02-23 kinaba: vector<LL> B2(n); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) B2[i] = B[i]*B[i]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<LL> L1(n), R1(n), L2(n), R2(n); 4fd800b3a8 2011-02-23 kinaba: partial_sum( B.begin(), B.end(), L1.begin() ); 4fd800b3a8 2011-02-23 kinaba: partial_sum( B.rbegin(), B.rend(), R1.rbegin() ); 4fd800b3a8 2011-02-23 kinaba: partial_sum( B2.begin(), B2.end(), L2.begin() ); 4fd800b3a8 2011-02-23 kinaba: partial_sum( B2.rbegin(), B2.rend(), R2.rbegin() ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL e = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int j = lower_bound(B.begin(), B.end(), A[i]) - B.begin(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ln = j; 4fd800b3a8 2011-02-23 kinaba: LL ls = (j==0 ? 0 : L1[j-1]); 4fd800b3a8 2011-02-23 kinaba: LL ls2 = (j==0 ? 0 : L2[j-1]); 4fd800b3a8 2011-02-23 kinaba: LL rn = n-j; 4fd800b3a8 2011-02-23 kinaba: LL rs = (j==n ? 0 : R1[j]); 4fd800b3a8 2011-02-23 kinaba: LL rs2 = (j==n ? 0 : R2[j]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: e += (ls2-rs2) - 2*A[i]*(ls-rs) + (ln-rn)*A[i]*A[i]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return double(e) / n; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const double &Expected, const double &Received) { double diff = Expected - Received; if (diff < 0) diff = -diff; if (diff < 1e-9) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int A_[] = {6}; 4fd800b3a8 2011-02-23 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 4fd800b3a8 2011-02-23 kinaba: int B_[] = {4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 4fd800b3a8 2011-02-23 kinaba: int n = 1; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 4.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int A_[] = {1,7}; 4fd800b3a8 2011-02-23 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 4fd800b3a8 2011-02-23 kinaba: int B_[] = {3,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 4fd800b3a8 2011-02-23 kinaba: int n = 2; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int A_[] = {3,7}; 4fd800b3a8 2011-02-23 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 4fd800b3a8 2011-02-23 kinaba: int B_[] = {1,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 4fd800b3a8 2011-02-23 kinaba: int n = 2; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 20.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int A_[] = {45,35,236,2342,5543,21,32,2,6,23,24,23,41,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 4fd800b3a8 2011-02-23 kinaba: int B_[] = {2345,45,2345,345}; 4fd800b3a8 2011-02-23 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 4fd800b3a8 2011-02-23 kinaba: int n = 50; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 1.2721986164E8; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int A_[] = {34,3245,2534,53,53,46,346,246,346,2,624,624,6,245,6324,6542,624,6}; 4fd800b3a8 2011-02-23 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 4fd800b3a8 2011-02-23 kinaba: int B_[] = {345,234,523,4624,6,2456,345,634,634,7,3457,376,34,6234,62,523,52,35,32}; 4fd800b3a8 2011-02-23 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 4fd800b3a8 2011-02-23 kinaba: int n = 7; 4fd800b3a8 2011-02-23 kinaba: double RetVal = -9713701.714285715; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: int A_[] = {1,2,3,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 4fd800b3a8 2011-02-23 kinaba: int B_[] = {1,2,3,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 4fd800b3a8 2011-02-23 kinaba: int n = 50000; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: