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 <functional> 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: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class QuickSort { public: 4fd800b3a8 2011-02-23 kinaba: double getEval(vector <int> L) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int m = *min_element(L.begin(), L.end()); 4fd800b3a8 2011-02-23 kinaba: int M = *max_element(L.begin(), L.end()); 4fd800b3a8 2011-02-23 kinaba: return rec(L, m, M+1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: map<pair<int,int>, double> memo; 4fd800b3a8 2011-02-23 kinaba: double rec(const vector<int>& L, int m, int M) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: pair<int,int> key(m,M); 4fd800b3a8 2011-02-23 kinaba: if(memo.count(key)) 4fd800b3a8 2011-02-23 kinaba: return memo[key]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> c; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<L.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( m<=L[i] && L[i]<M ) 4fd800b3a8 2011-02-23 kinaba: c.push_back(L[i]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( c.size() <= 1 ) 4fd800b3a8 2011-02-23 kinaba: return memo[key] = 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double e = 0; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<c.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int p = c[k]; 4fd800b3a8 2011-02-23 kinaba: int cnt = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<k; ++i) 4fd800b3a8 2011-02-23 kinaba: if( c[i] > p ) 4fd800b3a8 2011-02-23 kinaba: ++cnt; 4fd800b3a8 2011-02-23 kinaba: for(int i=k+1; i<c.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( p > c[i] ) 4fd800b3a8 2011-02-23 kinaba: ++cnt; 4fd800b3a8 2011-02-23 kinaba: e += rec(L, m, p) + rec(L, p+1, M) + cnt; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return memo[key] = e / c.size(); 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() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const double& Expected, const double& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, QuickSort().getEval(L));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int L_[] = {1,2,3,4,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int L_[] = {1,2,4,3,5,6}; 4fd800b3a8 2011-02-23 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 1.0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int L_[] = {3,2,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 2.6666666666666665; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int L_[] = {50,40,30,20,10,15,25,35,45}; 4fd800b3a8 2011-02-23 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 11.07698412698413; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: 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, 4fd800b3a8 2011-02-23 kinaba: 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50}; 4fd800b3a8 2011-02-23 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 4fd800b3a8 2011-02-23 kinaba: double _ = -1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: 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, 4fd800b3a8 2011-02-23 kinaba: 31,13,1,2,3,4,35,36,37,38,47,14,15,16,17,18,19,20,21}; 4fd800b3a8 2011-02-23 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 4fd800b3a8 2011-02-23 kinaba: double _ = -1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE