d477545505 2016-01-13 kinaba: #include <iostream> d477545505 2016-01-13 kinaba: #include <sstream> d477545505 2016-01-13 kinaba: #include <iomanip> d477545505 2016-01-13 kinaba: #include <vector> d477545505 2016-01-13 kinaba: #include <string> d477545505 2016-01-13 kinaba: #include <map> d477545505 2016-01-13 kinaba: #include <set> d477545505 2016-01-13 kinaba: #include <algorithm> d477545505 2016-01-13 kinaba: #include <numeric> d477545505 2016-01-13 kinaba: #include <iterator> d477545505 2016-01-13 kinaba: #include <functional> d477545505 2016-01-13 kinaba: #include <complex> d477545505 2016-01-13 kinaba: #include <queue> d477545505 2016-01-13 kinaba: #include <stack> d477545505 2016-01-13 kinaba: #include <cmath> d477545505 2016-01-13 kinaba: #include <cassert> d477545505 2016-01-13 kinaba: #include <tuple> d477545505 2016-01-13 kinaba: using namespace std; d477545505 2016-01-13 kinaba: typedef long long LL; d477545505 2016-01-13 kinaba: typedef complex<double> CMP; d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: class ReturnOfTheJedi { public: d477545505 2016-01-13 kinaba: vector <double> minimalExpectation(vector <int> x, vector <int> p) d477545505 2016-01-13 kinaba: { d477545505 2016-01-13 kinaba: vector<pair<double,double>> item; d477545505 2016-01-13 kinaba: for(int i=0; i<x.size(); ++i) d477545505 2016-01-13 kinaba: item.emplace_back(p[i]/100000.0, x[i]); d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: vector<double> ans(x.size()); d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: vector<pair<double,double>> iA = item; d477545505 2016-01-13 kinaba: double pA = 1.0; d477545505 2016-01-13 kinaba: double xA = 0.0; d477545505 2016-01-13 kinaba: vector<pair<double,double>> iB = item; d477545505 2016-01-13 kinaba: double pB = 1.0; d477545505 2016-01-13 kinaba: double xB = 0.0; d477545505 2016-01-13 kinaba: { d477545505 2016-01-13 kinaba: vector<pair<double,double>>::iterator b1 = iB.begin(); d477545505 2016-01-13 kinaba: double best_score = 1e+300; d477545505 2016-01-13 kinaba: for(vector<pair<double,double>>::iterator it = iB.begin(); it != iB.end(); ++it) { d477545505 2016-01-13 kinaba: double score = it->first * it->second; d477545505 2016-01-13 kinaba: if(best_score > score) d477545505 2016-01-13 kinaba: {b1 = it, best_score = score;} d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: pB = b1->first; d477545505 2016-01-13 kinaba: xB = b1->second; d477545505 2016-01-13 kinaba: iB.erase(b1); d477545505 2016-01-13 kinaba: ans[0] = (pB*xB); d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: for(int k=1; k<ans.size(); ++k) { d477545505 2016-01-13 kinaba: vector<pair<double,double>>::iterator b1 = iB.begin(); d477545505 2016-01-13 kinaba: double best_score = 1e+300; d477545505 2016-01-13 kinaba: for(vector<pair<double,double>>::iterator it = iB.begin(); it != iB.end(); ++it) { d477545505 2016-01-13 kinaba: double score = pB*it->first * (xB+it->second); d477545505 2016-01-13 kinaba: if(best_score > score) d477545505 2016-01-13 kinaba: {b1 = it, best_score = score;} d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: vector<pair<double,double>>::iterator b2 = iA.begin(); d477545505 2016-01-13 kinaba: vector<pair<double,double>>::iterator b3 = iA.begin(); d477545505 2016-01-13 kinaba: bool A_mode = false; d477545505 2016-01-13 kinaba: for(vector<pair<double,double>>::iterator it = iA.begin(); it != iA.end(); ++it) d477545505 2016-01-13 kinaba: for(vector<pair<double,double>>::iterator kt = it; kt != iA.end(); ++kt) if(kt!=it){ d477545505 2016-01-13 kinaba: double score = pA*it->first*kt->first * (xA+it->second+kt->second); d477545505 2016-01-13 kinaba: if(best_score > score) d477545505 2016-01-13 kinaba: {b2 = it, b3 = kt, best_score = score, A_mode = true;} d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: if(!A_mode) { d477545505 2016-01-13 kinaba: pA = pB; d477545505 2016-01-13 kinaba: xA = xB; d477545505 2016-01-13 kinaba: iA = iB; d477545505 2016-01-13 kinaba: pB *= b1->first; d477545505 2016-01-13 kinaba: xB += b1->second; d477545505 2016-01-13 kinaba: iB.erase(b1); d477545505 2016-01-13 kinaba: } else { d477545505 2016-01-13 kinaba: pA *= b2->first * b3->first; d477545505 2016-01-13 kinaba: xA += b2->second + b3->second; d477545505 2016-01-13 kinaba: auto xxx = *b3; d477545505 2016-01-13 kinaba: iA.erase(b2); d477545505 2016-01-13 kinaba: iA.erase(std::find(iA.begin(), iA.end(), xxx)); d477545505 2016-01-13 kinaba: swap(pA, pB); d477545505 2016-01-13 kinaba: swap(xA, xB); d477545505 2016-01-13 kinaba: swap(iA, iB); d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: ans[k] = pB*xB; d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: return ans; d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: }; d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: // BEGIN CUT HERE d477545505 2016-01-13 kinaba: #include <ctime> d477545505 2016-01-13 kinaba: double start_time; string timer() d477545505 2016-01-13 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d477545505 2016-01-13 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d477545505 2016-01-13 kinaba: { os << "{ "; d477545505 2016-01-13 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d477545505 2016-01-13 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } d477545505 2016-01-13 kinaba: void verify_case(const vector <double>& Expected, const vector <double>& Received) { d477545505 2016-01-13 kinaba: bool ok = true; d477545505 2016-01-13 kinaba: for(int i=0; i<Expected.size(); ++i) d477545505 2016-01-13 kinaba: if( abs(Expected[i]-Received[i]) >= 1e-9 ) d477545505 2016-01-13 kinaba: ok = false; d477545505 2016-01-13 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d477545505 2016-01-13 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } d477545505 2016-01-13 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); d477545505 2016-01-13 kinaba: #define END verify_case(_, ReturnOfTheJedi().minimalExpectation(x, p));} d477545505 2016-01-13 kinaba: int main(){ d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: CASE(0) d477545505 2016-01-13 kinaba: int x_[] = {100,200,300}; d477545505 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); d477545505 2016-01-13 kinaba: int p_[] = {50000, 20000, 20000}; d477545505 2016-01-13 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); d477545505 2016-01-13 kinaba: double __[] = {40.0, 20.000000000000004, 12.000000000000002 }; d477545505 2016-01-13 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(1) d477545505 2016-01-13 kinaba: int x_[] = {200,100,500,300,400}; d477545505 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); d477545505 2016-01-13 kinaba: int p_[] = {100000, 100000, 100000, 100000, 100000}; d477545505 2016-01-13 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); d477545505 2016-01-13 kinaba: double __[] = {100.0, 300.0, 600.0, 1000.0, 1500.0 }; d477545505 2016-01-13 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(2) d477545505 2016-01-13 kinaba: int x_[] = {2,2,100,100}; d477545505 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); d477545505 2016-01-13 kinaba: int p_[] = {100000,100000,10000,10000}; d477545505 2016-01-13 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); d477545505 2016-01-13 kinaba: double __[] = {2.0, 2.0000000000000004, 2.0200000000000005, 2.0400000000000005 }; d477545505 2016-01-13 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(3) d477545505 2016-01-13 kinaba: int x_[] = {1,1,200,200}; d477545505 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); d477545505 2016-01-13 kinaba: int p_[] = {100000,100000,10000,10000}; d477545505 2016-01-13 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); d477545505 2016-01-13 kinaba: double __[] = {1.0, 2.0, 4.010000000000001, 4.0200000000000005 }; d477545505 2016-01-13 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(4) d477545505 2016-01-13 kinaba: int x_[] = {1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000}; d477545505 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); d477545505 2016-01-13 kinaba: int p_[] = {90000,90000,90000,90000,90000,90000,90000,90000,90000,90000}; d477545505 2016-01-13 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); d477545505 2016-01-13 kinaba: double __[] = {9.0E8, 1.62E9, 2.1870000000000005E9, 2.6244000000000005E9, 2.952450000000001E9, 3.188646000000001E9, 3.348078300000001E9, 3.4437376800000014E9, 3.4867844010000014E9, 3.4867844010000014E9 }; d477545505 2016-01-13 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: /* d477545505 2016-01-13 kinaba: CASE(5) d477545505 2016-01-13 kinaba: int x_[] = ; d477545505 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); d477545505 2016-01-13 kinaba: int p_[] = ; d477545505 2016-01-13 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); d477545505 2016-01-13 kinaba: double __[] = ; d477545505 2016-01-13 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(6) d477545505 2016-01-13 kinaba: int x_[] = ; d477545505 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); d477545505 2016-01-13 kinaba: int p_[] = ; d477545505 2016-01-13 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); d477545505 2016-01-13 kinaba: double __[] = ; d477545505 2016-01-13 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: */ d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: // END CUT HERE