bc2785b91d 2012-08-17 kinaba: #include <iostream> bc2785b91d 2012-08-17 kinaba: #include <sstream> bc2785b91d 2012-08-17 kinaba: #include <iomanip> bc2785b91d 2012-08-17 kinaba: #include <vector> bc2785b91d 2012-08-17 kinaba: #include <string> bc2785b91d 2012-08-17 kinaba: #include <map> bc2785b91d 2012-08-17 kinaba: #include <set> bc2785b91d 2012-08-17 kinaba: #include <algorithm> bc2785b91d 2012-08-17 kinaba: #include <numeric> bc2785b91d 2012-08-17 kinaba: #include <iterator> bc2785b91d 2012-08-17 kinaba: #include <functional> bc2785b91d 2012-08-17 kinaba: #include <complex> bc2785b91d 2012-08-17 kinaba: #include <queue> bc2785b91d 2012-08-17 kinaba: #include <stack> bc2785b91d 2012-08-17 kinaba: #include <cmath> bc2785b91d 2012-08-17 kinaba: #include <cassert> bc2785b91d 2012-08-17 kinaba: using namespace std; bc2785b91d 2012-08-17 kinaba: typedef long long LL; bc2785b91d 2012-08-17 kinaba: typedef long double LD; bc2785b91d 2012-08-17 kinaba: typedef complex<LD> CMP; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: class FoxPaintingBalls { public: bc2785b91d 2012-08-17 kinaba: long long theMax(long long R, long long G, long long B, int N_) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: LL N = N_; bc2785b91d 2012-08-17 kinaba: LL tot = N*(N+1)/2; bc2785b91d 2012-08-17 kinaba: LL t = tot/3; bc2785b91d 2012-08-17 kinaba: LL tr = tot%3; bc2785b91d 2012-08-17 kinaba: // (t,t,t+tr) bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: LL l=0, r=(R+G+B)/tot+1; // [l,r) bc2785b91d 2012-08-17 kinaba: while( l+1 < r ) { bc2785b91d 2012-08-17 kinaba: LL c = (l+r)/2; bc2785b91d 2012-08-17 kinaba: LL rr = R-t*c; bc2785b91d 2012-08-17 kinaba: LL gg = G-t*c; bc2785b91d 2012-08-17 kinaba: LL bb = B-t*c; bc2785b91d 2012-08-17 kinaba: bool can = true; bc2785b91d 2012-08-17 kinaba: if(rr<0 || gg<0 || bb<0) bc2785b91d 2012-08-17 kinaba: can = false; bc2785b91d 2012-08-17 kinaba: else bc2785b91d 2012-08-17 kinaba: can = (rr+gg+bb >= tr*c); bc2785b91d 2012-08-17 kinaba: (can ? l : r) = c; bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: return l; bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: }; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: // BEGIN CUT HERE bc2785b91d 2012-08-17 kinaba: #include <ctime> bc2785b91d 2012-08-17 kinaba: double start_time; string timer() bc2785b91d 2012-08-17 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } bc2785b91d 2012-08-17 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) bc2785b91d 2012-08-17 kinaba: { os << "{ "; bc2785b91d 2012-08-17 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) bc2785b91d 2012-08-17 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } bc2785b91d 2012-08-17 kinaba: void verify_case(const long long& Expected, const long long& Received) { bc2785b91d 2012-08-17 kinaba: bool ok = (Expected == Received); bc2785b91d 2012-08-17 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; bc2785b91d 2012-08-17 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } bc2785b91d 2012-08-17 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); bc2785b91d 2012-08-17 kinaba: #define END verify_case(_, FoxPaintingBalls().theMax(R, G, B, N));} bc2785b91d 2012-08-17 kinaba: int main(){ bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: CASE(0) bc2785b91d 2012-08-17 kinaba: long long R = 2LL; bc2785b91d 2012-08-17 kinaba: long long G = 2LL; bc2785b91d 2012-08-17 kinaba: long long B = 2LL; bc2785b91d 2012-08-17 kinaba: int N = 3; bc2785b91d 2012-08-17 kinaba: long long _ = 1LL; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(1) bc2785b91d 2012-08-17 kinaba: long long R = 1LL; bc2785b91d 2012-08-17 kinaba: long long G = 2LL; bc2785b91d 2012-08-17 kinaba: long long B = 3LL; bc2785b91d 2012-08-17 kinaba: int N = 3; bc2785b91d 2012-08-17 kinaba: long long _ = 0LL; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(2) bc2785b91d 2012-08-17 kinaba: long long R = 8LL; bc2785b91d 2012-08-17 kinaba: long long G = 6LL; bc2785b91d 2012-08-17 kinaba: long long B = 6LL; bc2785b91d 2012-08-17 kinaba: int N = 4; bc2785b91d 2012-08-17 kinaba: long long _ = 2LL; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(3) bc2785b91d 2012-08-17 kinaba: long long R = 7LL; bc2785b91d 2012-08-17 kinaba: long long G = 6LL; bc2785b91d 2012-08-17 kinaba: long long B = 7LL; bc2785b91d 2012-08-17 kinaba: int N = 4; bc2785b91d 2012-08-17 kinaba: long long _ = 2LL; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(4) bc2785b91d 2012-08-17 kinaba: long long R = 100LL; bc2785b91d 2012-08-17 kinaba: long long G = 100LL; bc2785b91d 2012-08-17 kinaba: long long B = 100LL; bc2785b91d 2012-08-17 kinaba: int N = 4; bc2785b91d 2012-08-17 kinaba: long long _ = 30LL; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(5) bc2785b91d 2012-08-17 kinaba: long long R = 19330428391852493LL; bc2785b91d 2012-08-17 kinaba: long long G = 48815737582834113LL; bc2785b91d 2012-08-17 kinaba: long long B = 11451481019198930LL; bc2785b91d 2012-08-17 kinaba: int N = 3456; bc2785b91d 2012-08-17 kinaba: long long _ = 5750952686LL; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(6) bc2785b91d 2012-08-17 kinaba: long long R = 1LL; bc2785b91d 2012-08-17 kinaba: long long G = 1LL; bc2785b91d 2012-08-17 kinaba: long long B = 1LL; bc2785b91d 2012-08-17 kinaba: int N = 1; bc2785b91d 2012-08-17 kinaba: long long _ = 3LL; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(7) bc2785b91d 2012-08-17 kinaba: long long R = 1LL; bc2785b91d 2012-08-17 kinaba: long long G = 1LL; bc2785b91d 2012-08-17 kinaba: long long B = 1LL; bc2785b91d 2012-08-17 kinaba: int N = 7; bc2785b91d 2012-08-17 kinaba: long long _ = -1LL; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(8) bc2785b91d 2012-08-17 kinaba: long long R = 1000000000000000000LL; bc2785b91d 2012-08-17 kinaba: long long G = 1000000000000000000LL; bc2785b91d 2012-08-17 kinaba: long long B = 1000000000000000000LL; bc2785b91d 2012-08-17 kinaba: int N = 1; bc2785b91d 2012-08-17 kinaba: long long _ = 3000000000000000000LL; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: // END CUT HERE