2da323b3ad 2016-01-13 kinaba: #include <iostream> 2da323b3ad 2016-01-13 kinaba: #include <sstream> 2da323b3ad 2016-01-13 kinaba: #include <iomanip> 2da323b3ad 2016-01-13 kinaba: #include <vector> 2da323b3ad 2016-01-13 kinaba: #include <string> 2da323b3ad 2016-01-13 kinaba: #include <map> 2da323b3ad 2016-01-13 kinaba: #include <set> 2da323b3ad 2016-01-13 kinaba: #include <algorithm> 2da323b3ad 2016-01-13 kinaba: #include <numeric> 2da323b3ad 2016-01-13 kinaba: #include <iterator> 2da323b3ad 2016-01-13 kinaba: #include <functional> 2da323b3ad 2016-01-13 kinaba: #include <complex> 2da323b3ad 2016-01-13 kinaba: #include <queue> 2da323b3ad 2016-01-13 kinaba: #include <stack> 2da323b3ad 2016-01-13 kinaba: #include <cmath> 2da323b3ad 2016-01-13 kinaba: #include <cassert> 2da323b3ad 2016-01-13 kinaba: #include <tuple> 2da323b3ad 2016-01-13 kinaba: using namespace std; 2da323b3ad 2016-01-13 kinaba: typedef long long LL; 2da323b3ad 2016-01-13 kinaba: typedef complex<double> CMP; 2da323b3ad 2016-01-13 kinaba: 2da323b3ad 2016-01-13 kinaba: class WaterTank { public: 2da323b3ad 2016-01-13 kinaba: double minOutputRate(vector <int> t, vector <int> x, int C) 2da323b3ad 2016-01-13 kinaba: { 2da323b3ad 2016-01-13 kinaba: double L=0, R=1000000; // (L,R] 2da323b3ad 2016-01-13 kinaba: for(int _=0; _<10000; ++_) { 2da323b3ad 2016-01-13 kinaba: double mid = (L+R)/2; 2da323b3ad 2016-01-13 kinaba: (can(t, x, C, mid) ? R : L) = mid; 2da323b3ad 2016-01-13 kinaba: } 2da323b3ad 2016-01-13 kinaba: return R; 2da323b3ad 2016-01-13 kinaba: } 2da323b3ad 2016-01-13 kinaba: 2da323b3ad 2016-01-13 kinaba: bool can(const vector<int>& t, const vector<int>& x, int C, double R) 2da323b3ad 2016-01-13 kinaba: { 2da323b3ad 2016-01-13 kinaba: double v = 0; 2da323b3ad 2016-01-13 kinaba: for(int i=0; i<t.size(); ++i) { 2da323b3ad 2016-01-13 kinaba: v = max(0.0, v+(x[i]-R)*t[i]); 2da323b3ad 2016-01-13 kinaba: if(v > C) 2da323b3ad 2016-01-13 kinaba: return false; 2da323b3ad 2016-01-13 kinaba: } 2da323b3ad 2016-01-13 kinaba: return true; 2da323b3ad 2016-01-13 kinaba: } 2da323b3ad 2016-01-13 kinaba: }; 2da323b3ad 2016-01-13 kinaba: 2da323b3ad 2016-01-13 kinaba: // BEGIN CUT HERE 2da323b3ad 2016-01-13 kinaba: #include <ctime> 2da323b3ad 2016-01-13 kinaba: double start_time; string timer() 2da323b3ad 2016-01-13 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2da323b3ad 2016-01-13 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2da323b3ad 2016-01-13 kinaba: { os << "{ "; 2da323b3ad 2016-01-13 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2da323b3ad 2016-01-13 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2da323b3ad 2016-01-13 kinaba: void verify_case(const double& Expected, const double& Received) { 2da323b3ad 2016-01-13 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 2da323b3ad 2016-01-13 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2da323b3ad 2016-01-13 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2da323b3ad 2016-01-13 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2da323b3ad 2016-01-13 kinaba: #define END verify_case(_, WaterTank().minOutputRate(t, x, C));} 2da323b3ad 2016-01-13 kinaba: int main(){ 2da323b3ad 2016-01-13 kinaba: 2da323b3ad 2016-01-13 kinaba: CASE(0) 2da323b3ad 2016-01-13 kinaba: int t_[] = {3,3}; 2da323b3ad 2016-01-13 kinaba: vector <int> t(t_, t_+sizeof(t_)/sizeof(*t_)); 2da323b3ad 2016-01-13 kinaba: int x_[] = {1,2}; 2da323b3ad 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 2da323b3ad 2016-01-13 kinaba: int C = 3; 2da323b3ad 2016-01-13 kinaba: double _ = 0.9999999999999999; 2da323b3ad 2016-01-13 kinaba: END 2da323b3ad 2016-01-13 kinaba: CASE(1) 2da323b3ad 2016-01-13 kinaba: int t_[] = {1,2,3,4,5}; 2da323b3ad 2016-01-13 kinaba: vector <int> t(t_, t_+sizeof(t_)/sizeof(*t_)); 2da323b3ad 2016-01-13 kinaba: int x_[] = {5,4,3,2,1}; 2da323b3ad 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 2da323b3ad 2016-01-13 kinaba: int C = 10; 2da323b3ad 2016-01-13 kinaba: double _ = 1.9999999999999996; 2da323b3ad 2016-01-13 kinaba: END 2da323b3ad 2016-01-13 kinaba: CASE(2) 2da323b3ad 2016-01-13 kinaba: int t_[] = {5949,3198,376,3592,4019,3481,5609,3840,6092,4059}; 2da323b3ad 2016-01-13 kinaba: vector <int> t(t_, t_+sizeof(t_)/sizeof(*t_)); 2da323b3ad 2016-01-13 kinaba: int x_[] = {29,38,96,84,10,2,39,27,76,94}; 2da323b3ad 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 2da323b3ad 2016-01-13 kinaba: int C = 1000000000; 2da323b3ad 2016-01-13 kinaba: double _ = 0.0; 2da323b3ad 2016-01-13 kinaba: END 2da323b3ad 2016-01-13 kinaba: CASE(3) 2da323b3ad 2016-01-13 kinaba: int t_[] = {9,3,4,8,1,2,5,7,6}; 2da323b3ad 2016-01-13 kinaba: vector <int> t(t_, t_+sizeof(t_)/sizeof(*t_)); 2da323b3ad 2016-01-13 kinaba: int x_[] = {123,456,789,1011,1213,1415,1617,1819,2021}; 2da323b3ad 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 2da323b3ad 2016-01-13 kinaba: int C = 11; 2da323b3ad 2016-01-13 kinaba: double _ = 2019.1666666666665; 2da323b3ad 2016-01-13 kinaba: END 2da323b3ad 2016-01-13 kinaba: CASE(4) 2da323b3ad 2016-01-13 kinaba: int t_[] = {100}; 2da323b3ad 2016-01-13 kinaba: vector <int> t(t_, t_+sizeof(t_)/sizeof(*t_)); 2da323b3ad 2016-01-13 kinaba: int x_[] = {1000}; 2da323b3ad 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 2da323b3ad 2016-01-13 kinaba: int C = 12345; 2da323b3ad 2016-01-13 kinaba: double _ = 876.55; 2da323b3ad 2016-01-13 kinaba: END 2da323b3ad 2016-01-13 kinaba: /* 2da323b3ad 2016-01-13 kinaba: CASE(5) 2da323b3ad 2016-01-13 kinaba: int t_[] = ; 2da323b3ad 2016-01-13 kinaba: vector <int> t(t_, t_+sizeof(t_)/sizeof(*t_)); 2da323b3ad 2016-01-13 kinaba: int x_[] = ; 2da323b3ad 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 2da323b3ad 2016-01-13 kinaba: int C = ; 2da323b3ad 2016-01-13 kinaba: double _ = ; 2da323b3ad 2016-01-13 kinaba: END 2da323b3ad 2016-01-13 kinaba: CASE(6) 2da323b3ad 2016-01-13 kinaba: int t_[] = ; 2da323b3ad 2016-01-13 kinaba: vector <int> t(t_, t_+sizeof(t_)/sizeof(*t_)); 2da323b3ad 2016-01-13 kinaba: int x_[] = ; 2da323b3ad 2016-01-13 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 2da323b3ad 2016-01-13 kinaba: int C = ; 2da323b3ad 2016-01-13 kinaba: double _ = ; 2da323b3ad 2016-01-13 kinaba: END 2da323b3ad 2016-01-13 kinaba: */ 2da323b3ad 2016-01-13 kinaba: } 2da323b3ad 2016-01-13 kinaba: // END CUT HERE