Artifact Content
Not logged in

Artifact 937144173b305ccc0e25e6a79efd4f4e1f8f8412


#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 QuickSort { public:
	double getEval(vector <int> L) 
	{
		int m = *min_element(L.begin(), L.end());
		int M = *max_element(L.begin(), L.end());
		return rec(L, m, M+1);
	}
	map<pair<int,int>, double> memo;
	double rec(const vector<int>& L, int m, int M)
	{
		pair<int,int> key(m,M);
		if(memo.count(key))
			return memo[key];

		vector<int> c;
		for(int i=0; i<L.size(); ++i)
			if( m<=L[i] && L[i]<M )
				c.push_back(L[i]);

		if( c.size() <= 1 )
			return memo[key] = 0;

		double e = 0;
		for(int k=0; k<c.size(); ++k)
		{
			int p = c[k];
			int cnt = 0;
			for(int i=0; i<k; ++i)
				if( c[i] > p )
					++cnt;
			for(int i=k+1; i<c.size(); ++i)
				if( p > c[i] )
					++cnt;
			e += rec(L, m, p) + rec(L, p+1, M) + cnt;
		}

		return memo[key] = e / c.size();
	}
};

// 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) {
 bool ok = (abs(Expected - Received) < 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(_, QuickSort().getEval(L));}
int main(){

CASE(0)
	int L_[] = {1,2,3,4,5};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	double _ = 0.0; 
END
CASE(1)
	int L_[] = {1,2,4,3,5,6};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	double _ = 1.0; 
END
CASE(2)
	int L_[] = {3,2,1};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	double _ = 2.6666666666666665; 
END
CASE(3)
	int L_[] = {50,40,30,20,10,15,25,35,45};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	double _ = 11.07698412698413; 
END
CASE(4)
	int L_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
	31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	double _ = -1; 
END
CASE(5)
	int L_[] = {48,49,50,10,11,12,40,41,42,5,39,32,33,34,43,44,45,46,6,7,8,9,22,23,24,25,26,27,28,29,30,
	31,13,1,2,3,4,35,36,37,38,47,14,15,16,17,18,19,20,21};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	double _ = -1; 
END
}
// END CUT HERE