Artifact Content
Not logged in

Artifact dd4898e19fbf1df80da752a97eafdf2071c7715e


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

class BouncingBalls { public:
	double expectedBounces(vector <int> x, int T) 
	{
		sort(x.begin(), x.end());

		double  total = 0;

		int N = x.size();
		for(int m=0; m<(1<<N); ++m)
		{
			vector<bool> goLeft(N);
			for(int i=0; i<N; ++i)
				goLeft[i] = ((1<<i)&m) != 0;

			total += num(x, goLeft, T);
		}

		return total / (1<<N);
	}

	double num( const vector<int>& x, const vector<bool>& goLeft, int T )
	{
		int cnt = 0;
		for(int i=0; i<x.size(); ++i)
			for(int j=i+1; j<x.size(); ++j)
				if( !goLeft[i] && goLeft[j] && x[j]-x[i]<=2*T )
					++cnt;
		return cnt;
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const double& Expected, const double& Received) {
 double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff < 1e-9);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, BouncingBalls().expectedBounces(x, T));}
int main(){

CASE(0)
	int x_[] = {5, 8};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int T = 2; 
	double _ = 0.25; 
END
CASE(1)
	int x_[] = {5, 8};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int T = 1; 
	double _ = 0.0; 
END
CASE(2)
	int x_[] = {91, 857, 692, 54, 8679, 83, 792, 86, 9537, 913, 64, 592};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int T = 458; 
	double _ = 11.5; 
END
CASE(3)
	int x_[] = {75432};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int T = 386; 
	double _ = 0.0; 
END
CASE(4)
	int x_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int T = 3; 
	double _ = 12.75; 
END
CASE(5)
	int x_[] = {1};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int T = 100000000; 
	double _ = 0; 
END
CASE(6)
	int x_[] = {10000000};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int T = 100000; 
	double _ = 0; 
END

}
// END CUT HERE