2a8b395e2e 2012-03-07 kinaba: #include <iostream> 2a8b395e2e 2012-03-07 kinaba: #include <sstream> 2a8b395e2e 2012-03-07 kinaba: #include <iomanip> 2a8b395e2e 2012-03-07 kinaba: #include <vector> 2a8b395e2e 2012-03-07 kinaba: #include <string> 2a8b395e2e 2012-03-07 kinaba: #include <map> 2a8b395e2e 2012-03-07 kinaba: #include <set> 2a8b395e2e 2012-03-07 kinaba: #include <algorithm> 2a8b395e2e 2012-03-07 kinaba: #include <numeric> 2a8b395e2e 2012-03-07 kinaba: #include <iterator> 2a8b395e2e 2012-03-07 kinaba: #include <functional> 2a8b395e2e 2012-03-07 kinaba: #include <complex> 2a8b395e2e 2012-03-07 kinaba: #include <queue> 2a8b395e2e 2012-03-07 kinaba: #include <stack> 2a8b395e2e 2012-03-07 kinaba: #include <cmath> 2a8b395e2e 2012-03-07 kinaba: #include <cassert> 2a8b395e2e 2012-03-07 kinaba: #include <cstring> 2a8b395e2e 2012-03-07 kinaba: #ifdef __GNUC__ 2a8b395e2e 2012-03-07 kinaba: #include <ext/hash_map> 2a8b395e2e 2012-03-07 kinaba: #define unordered_map __gnu_cxx::hash_map 2a8b395e2e 2012-03-07 kinaba: #else 2a8b395e2e 2012-03-07 kinaba: #include <unordered_map> 2a8b395e2e 2012-03-07 kinaba: #endif 2a8b395e2e 2012-03-07 kinaba: using namespace std; 2a8b395e2e 2012-03-07 kinaba: typedef long long LL; 2a8b395e2e 2012-03-07 kinaba: typedef complex<double> CMP; 2a8b395e2e 2012-03-07 kinaba: 2a8b395e2e 2012-03-07 kinaba: class FoxAndBusiness { public: 2a8b395e2e 2012-03-07 kinaba: double minimumCost(int K, int totalWork, vector <int> a, vector <int> p) 2a8b395e2e 2012-03-07 kinaba: { 2a8b395e2e 2012-03-07 kinaba: double L=0, R=1e+60; 2a8b395e2e 2012-03-07 kinaba: for(int _=0; _<10000; ++_) 2a8b395e2e 2012-03-07 kinaba: { 2a8b395e2e 2012-03-07 kinaba: double C = (L+R)/2; 2a8b395e2e 2012-03-07 kinaba: (possible(C, K, a, p) ? R : L) = C; 2a8b395e2e 2012-03-07 kinaba: } 2a8b395e2e 2012-03-07 kinaba: return R * totalWork; 2a8b395e2e 2012-03-07 kinaba: } 2a8b395e2e 2012-03-07 kinaba: 2a8b395e2e 2012-03-07 kinaba: bool possible(double C, int K, const vector<int>& a, const vector<int>& p) 2a8b395e2e 2012-03-07 kinaba: { 2a8b395e2e 2012-03-07 kinaba: vector<double> v; 2a8b395e2e 2012-03-07 kinaba: for(int i=0; i<a.size(); ++i) 2a8b395e2e 2012-03-07 kinaba: v.push_back( (C-p[i])*a[i] ); 2a8b395e2e 2012-03-07 kinaba: sort(v.rbegin(), v.rend()); 2a8b395e2e 2012-03-07 kinaba: double r = accumulate(v.begin(), v.begin()+K, 0.0); 2a8b395e2e 2012-03-07 kinaba: return 3600*double(K) <= r; 2a8b395e2e 2012-03-07 kinaba: } 2a8b395e2e 2012-03-07 kinaba: }; 2a8b395e2e 2012-03-07 kinaba: 2a8b395e2e 2012-03-07 kinaba: // BEGIN CUT HERE 2a8b395e2e 2012-03-07 kinaba: #include <ctime> 2a8b395e2e 2012-03-07 kinaba: double start_time; string timer() 2a8b395e2e 2012-03-07 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2a8b395e2e 2012-03-07 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2a8b395e2e 2012-03-07 kinaba: { os << "{ "; 2a8b395e2e 2012-03-07 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2a8b395e2e 2012-03-07 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2a8b395e2e 2012-03-07 kinaba: void verify_case(const double& Expected, const double& Received) { 2a8b395e2e 2012-03-07 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 2a8b395e2e 2012-03-07 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2a8b395e2e 2012-03-07 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2a8b395e2e 2012-03-07 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2a8b395e2e 2012-03-07 kinaba: #define END verify_case(_, FoxAndBusiness().minimumCost(K, totalWork, a, p));} 2a8b395e2e 2012-03-07 kinaba: int main(){ 2a8b395e2e 2012-03-07 kinaba: 2a8b395e2e 2012-03-07 kinaba: CASE(0) 2a8b395e2e 2012-03-07 kinaba: int K = 1; 2a8b395e2e 2012-03-07 kinaba: int totalWork = 10; 2a8b395e2e 2012-03-07 kinaba: int a_[] = {10}; 2a8b395e2e 2012-03-07 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 2a8b395e2e 2012-03-07 kinaba: int p_[] = {20}; 2a8b395e2e 2012-03-07 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 2a8b395e2e 2012-03-07 kinaba: double _ = 3800.0; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(1) 2a8b395e2e 2012-03-07 kinaba: int K = 1; 2a8b395e2e 2012-03-07 kinaba: int totalWork = 100; 2a8b395e2e 2012-03-07 kinaba: int a_[] = {50, 60}; 2a8b395e2e 2012-03-07 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 2a8b395e2e 2012-03-07 kinaba: int p_[] = {1000, 2000}; 2a8b395e2e 2012-03-07 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 2a8b395e2e 2012-03-07 kinaba: double _ = 107200.0; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(2) 2a8b395e2e 2012-03-07 kinaba: int K = 2; 2a8b395e2e 2012-03-07 kinaba: int totalWork = 300; 2a8b395e2e 2012-03-07 kinaba: int a_[] = {10, 20, 47}; 2a8b395e2e 2012-03-07 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 2a8b395e2e 2012-03-07 kinaba: int p_[] = {15, 20, 98765}; 2a8b395e2e 2012-03-07 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 2a8b395e2e 2012-03-07 kinaba: double _ = 77500.0; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(3) 2a8b395e2e 2012-03-07 kinaba: int K = 4; 2a8b395e2e 2012-03-07 kinaba: int totalWork = 1000; 2a8b395e2e 2012-03-07 kinaba: int a_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 2a8b395e2e 2012-03-07 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 2a8b395e2e 2012-03-07 kinaba: int p_[] = {20, 30, 40, 58, 60, 70, 80, 90, 100, 150}; 2a8b395e2e 2012-03-07 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 2a8b395e2e 2012-03-07 kinaba: double _ = 531764.705882353; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(4) 2a8b395e2e 2012-03-07 kinaba: int K = 25; 2a8b395e2e 2012-03-07 kinaba: int totalWork = 100000; 2a8b395e2e 2012-03-07 kinaba: int a_[] = {17016,47415,83045,39455,34491,97616,91719,95101,85384,68562,36351,24654,36129,18257,87966,10190,67335,56121,20172,16228,16294,69902,83879,76475,81100,55688,5409,16764,33750,98625,56617,76728,50765,48650,58685,85248,46583,22525,8200,32280,59445,3381,85440,51313,26000,21507,39766,61104,76355,92950}; 2a8b395e2e 2012-03-07 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 2a8b395e2e 2012-03-07 kinaba: int p_[] = {59966,49102,35349,70206,65668,60145,10705,28056,9582,11619,8537,42449,30488,14162,1272,9064,817,15046,99637,16513,94277,42777,28196,34907,20309,47607,41378,33097,36974,74425,46978,13157,15027,12723,66607,26639,59698,37250,28554,78762,19543,55908,47550,42978,74731,4218,69250,16558,66512,12939}; 2a8b395e2e 2012-03-07 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 2a8b395e2e 2012-03-07 kinaba: double _ = -1; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(5) 2a8b395e2e 2012-03-07 kinaba: int K = 1; 2a8b395e2e 2012-03-07 kinaba: int totalWork = 100000; 2a8b395e2e 2012-03-07 kinaba: int a_[] = {1}; 2a8b395e2e 2012-03-07 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 2a8b395e2e 2012-03-07 kinaba: int p_[] = {100000}; 2a8b395e2e 2012-03-07 kinaba: vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 2a8b395e2e 2012-03-07 kinaba: double _ = -1; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: } 2a8b395e2e 2012-03-07 kinaba: // END CUT HERE