Artifact Content
Not logged in

Artifact 3fd802a4d545ca8ebd909eab60539a3efeb2cf09


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;

void gen( vector<int>& X, vector<LL>& R, int n )
{
	int j = 0;
	int m = X.size();
	for(int i=0; i<n; ++i)
	{
		R[i] = X[j];
		int s = (j+1)%m;
		X[j] = ( ( X[j] ^ X[s] ) + 13 ) % 49999;
		j = s;
	}
}

class RandomFights
{
public:
	double expectedNrOfPoints(vector <int> Aseed, vector <int> Bseed, int n) 
	{
		vector<LL> A(n), B(n);
		gen(Aseed, A, n);
		gen(Bseed, B, n);

		// expected gain by A[i] = sqSum{B[j] | B[j]<A[i]} - sqSum{B[j] | B[j]>A[i]}

		sort(B.begin(), B.end());
		vector<LL> B2(n);
		for(int i=0; i<n; ++i) B2[i] = B[i]*B[i];

		vector<LL> L1(n), R1(n), L2(n), R2(n);
		partial_sum( B.begin(),   B.end(),   L1.begin()  );
		partial_sum( B.rbegin(),  B.rend(),  R1.rbegin() );
		partial_sum( B2.begin(),  B2.end(),  L2.begin()  );
		partial_sum( B2.rbegin(), B2.rend(), R2.rbegin() );

		LL e = 0;
		for(int i=0; i<n; ++i)
		{
			int j = lower_bound(B.begin(), B.end(), A[i]) - B.begin();

			LL ln  = j;
			LL ls  = (j==0 ? 0 : L1[j-1]);
			LL ls2 = (j==0 ? 0 : L2[j-1]);
			LL rn  = n-j;
			LL rs  = (j==n ? 0 : R1[j]);
			LL rs2 = (j==n ? 0 : R2[j]);

			e += (ls2-rs2) - 2*A[i]*(ls-rs) + (ln-rn)*A[i]*A[i];
		}
		return double(e) / n;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

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(); }
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;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int A_[] = {6};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {4};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int n = 1; 
	double RetVal = 4.0; 
	return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); }
int Test_(Case_<1>) {
	int A_[] = {1,7};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {3,5};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int n = 2; 
	double RetVal = 0.0; 
	return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); }
int Test_(Case_<2>) {
	int A_[] = {3,7};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {1,5};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int n = 2; 
	double RetVal = 20.0; 
	return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); }
int Test_(Case_<3>) {
	int A_[] = {45,35,236,2342,5543,21,32,2,6,23,24,23,41,1};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {2345,45,2345,345};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int n = 50; 
	double RetVal = 1.2721986164E8; 
	return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); }
int Test_(Case_<4>) {
	int A_[] = {34,3245,2534,53,53,46,346,246,346,2,624,624,6,245,6324,6542,624,6};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {345,234,523,4624,6,2456,345,634,634,7,3457,376,34,6234,62,523,52,35,32};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int n = 7; 
	double RetVal = -9713701.714285715; 
	return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); }
int Test_(Case_<5>) {
	int A_[] = {1,2,3,4};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {1,2,3,4};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int n = 50000; 
	double RetVal = 0.0; 
	return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE