ecb4f1560e 2013-02-07 kinaba: #include <iostream> ecb4f1560e 2013-02-07 kinaba: #include <sstream> ecb4f1560e 2013-02-07 kinaba: #include <iomanip> ecb4f1560e 2013-02-07 kinaba: #include <vector> ecb4f1560e 2013-02-07 kinaba: #include <string> ecb4f1560e 2013-02-07 kinaba: #include <map> ecb4f1560e 2013-02-07 kinaba: #include <set> ecb4f1560e 2013-02-07 kinaba: #include <algorithm> ecb4f1560e 2013-02-07 kinaba: #include <numeric> ecb4f1560e 2013-02-07 kinaba: #include <iterator> ecb4f1560e 2013-02-07 kinaba: #include <functional> ecb4f1560e 2013-02-07 kinaba: #include <complex> ecb4f1560e 2013-02-07 kinaba: #include <queue> ecb4f1560e 2013-02-07 kinaba: #include <stack> ecb4f1560e 2013-02-07 kinaba: #include <cmath> ecb4f1560e 2013-02-07 kinaba: #include <cassert> ecb4f1560e 2013-02-07 kinaba: using namespace std; ecb4f1560e 2013-02-07 kinaba: typedef long long LL; ecb4f1560e 2013-02-07 kinaba: typedef long double LD; ecb4f1560e 2013-02-07 kinaba: typedef complex<LD> CMP; ecb4f1560e 2013-02-07 kinaba: ecb4f1560e 2013-02-07 kinaba: class BallsSeparating { public: ecb4f1560e 2013-02-07 kinaba: int minOperations(vector <int> red, vector <int> green, vector <int> blue) ecb4f1560e 2013-02-07 kinaba: { ecb4f1560e 2013-02-07 kinaba: int best = 1<<30; ecb4f1560e 2013-02-07 kinaba: int N = red.size(); ecb4f1560e 2013-02-07 kinaba: ecb4f1560e 2013-02-07 kinaba: if(N<=2) ecb4f1560e 2013-02-07 kinaba: return -1; ecb4f1560e 2013-02-07 kinaba: for(int R=0; R<N; ++R) ecb4f1560e 2013-02-07 kinaba: for(int G=0; G<N; ++G) if(G!=R) ecb4f1560e 2013-02-07 kinaba: for(int B=0; B<N; ++B) if(B!=G && B!=R) ecb4f1560e 2013-02-07 kinaba: { ecb4f1560e 2013-02-07 kinaba: int c = 0; ecb4f1560e 2013-02-07 kinaba: for(int i=0; i<N; ++i) ecb4f1560e 2013-02-07 kinaba: { ecb4f1560e 2013-02-07 kinaba: int gb = green[i] + blue[i]; ecb4f1560e 2013-02-07 kinaba: int br = blue[i] + red[i]; ecb4f1560e 2013-02-07 kinaba: int rg = red[i] + green[i]; ecb4f1560e 2013-02-07 kinaba: if(i==R) c += gb; ecb4f1560e 2013-02-07 kinaba: else if(i==B) c += rg; ecb4f1560e 2013-02-07 kinaba: else if(i==G) c += br; ecb4f1560e 2013-02-07 kinaba: else c += min(gb, min(rg,br)); ecb4f1560e 2013-02-07 kinaba: } ecb4f1560e 2013-02-07 kinaba: best = min(best, c); ecb4f1560e 2013-02-07 kinaba: } ecb4f1560e 2013-02-07 kinaba: return best; ecb4f1560e 2013-02-07 kinaba: } ecb4f1560e 2013-02-07 kinaba: }; ecb4f1560e 2013-02-07 kinaba: ecb4f1560e 2013-02-07 kinaba: // BEGIN CUT HERE ecb4f1560e 2013-02-07 kinaba: #include <ctime> ecb4f1560e 2013-02-07 kinaba: double start_time; string timer() ecb4f1560e 2013-02-07 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } ecb4f1560e 2013-02-07 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) ecb4f1560e 2013-02-07 kinaba: { os << "{ "; ecb4f1560e 2013-02-07 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) ecb4f1560e 2013-02-07 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } ecb4f1560e 2013-02-07 kinaba: void verify_case(const int& Expected, const int& Received) { ecb4f1560e 2013-02-07 kinaba: bool ok = (Expected == Received); ecb4f1560e 2013-02-07 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; ecb4f1560e 2013-02-07 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } ecb4f1560e 2013-02-07 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); ecb4f1560e 2013-02-07 kinaba: #define END verify_case(_, BallsSeparating().minOperations(red, green, blue));} ecb4f1560e 2013-02-07 kinaba: int main(){ ecb4f1560e 2013-02-07 kinaba: ecb4f1560e 2013-02-07 kinaba: CASE(0) ecb4f1560e 2013-02-07 kinaba: int red_[] = {1, 1, 1}; ecb4f1560e 2013-02-07 kinaba: vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); ecb4f1560e 2013-02-07 kinaba: int green_[] = {1, 1, 1}; ecb4f1560e 2013-02-07 kinaba: vector <int> green(green_, green_+sizeof(green_)/sizeof(*green_)); ecb4f1560e 2013-02-07 kinaba: int blue_[] = {1, 1, 1}; ecb4f1560e 2013-02-07 kinaba: vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); ecb4f1560e 2013-02-07 kinaba: int _ = 6; ecb4f1560e 2013-02-07 kinaba: END ecb4f1560e 2013-02-07 kinaba: CASE(1) ecb4f1560e 2013-02-07 kinaba: int red_[] = {5}; ecb4f1560e 2013-02-07 kinaba: vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); ecb4f1560e 2013-02-07 kinaba: int green_[] = {6}; ecb4f1560e 2013-02-07 kinaba: vector <int> green(green_, green_+sizeof(green_)/sizeof(*green_)); ecb4f1560e 2013-02-07 kinaba: int blue_[] = {8}; ecb4f1560e 2013-02-07 kinaba: vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); ecb4f1560e 2013-02-07 kinaba: int _ = -1; ecb4f1560e 2013-02-07 kinaba: END ecb4f1560e 2013-02-07 kinaba: CASE(2) ecb4f1560e 2013-02-07 kinaba: int red_[] = {4, 6, 5, 7}; ecb4f1560e 2013-02-07 kinaba: vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); ecb4f1560e 2013-02-07 kinaba: int green_[] = {7, 4, 6, 3}; ecb4f1560e 2013-02-07 kinaba: vector <int> green(green_, green_+sizeof(green_)/sizeof(*green_)); ecb4f1560e 2013-02-07 kinaba: int blue_[] = {6, 5, 3, 8}; ecb4f1560e 2013-02-07 kinaba: vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); ecb4f1560e 2013-02-07 kinaba: int _ = 37; ecb4f1560e 2013-02-07 kinaba: END ecb4f1560e 2013-02-07 kinaba: CASE(3) ecb4f1560e 2013-02-07 kinaba: int red_[] = {7, 12, 9, 9, 7}; ecb4f1560e 2013-02-07 kinaba: vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); ecb4f1560e 2013-02-07 kinaba: int green_[] = {7, 10, 8, 8, 9}; ecb4f1560e 2013-02-07 kinaba: vector <int> green(green_, green_+sizeof(green_)/sizeof(*green_)); ecb4f1560e 2013-02-07 kinaba: int blue_[] = {8, 9, 5, 6, 13}; ecb4f1560e 2013-02-07 kinaba: vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); ecb4f1560e 2013-02-07 kinaba: int _ = 77; ecb4f1560e 2013-02-07 kinaba: END ecb4f1560e 2013-02-07 kinaba: CASE(4) ecb4f1560e 2013-02-07 kinaba: int red_[] = {842398, 491273, 958925, 849859, 771363, 67803, 184892, 391907, 256150, 75799}; ecb4f1560e 2013-02-07 kinaba: vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); ecb4f1560e 2013-02-07 kinaba: int green_[] = {268944, 342402, 894352, 228640, 903885, 908656, 414271, 292588, 852057, 889141}; ecb4f1560e 2013-02-07 kinaba: vector <int> green(green_, green_+sizeof(green_)/sizeof(*green_)); ecb4f1560e 2013-02-07 kinaba: int blue_[] = {662939, 340220, 600081, 390298, 376707, 372199, 435097, 40266, 145590, 505103}; ecb4f1560e 2013-02-07 kinaba: vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); ecb4f1560e 2013-02-07 kinaba: int _ = 7230607; ecb4f1560e 2013-02-07 kinaba: END ecb4f1560e 2013-02-07 kinaba: /* ecb4f1560e 2013-02-07 kinaba: CASE(5) ecb4f1560e 2013-02-07 kinaba: int red_[] = ; ecb4f1560e 2013-02-07 kinaba: vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); ecb4f1560e 2013-02-07 kinaba: int green_[] = ; ecb4f1560e 2013-02-07 kinaba: vector <int> green(green_, green_+sizeof(green_)/sizeof(*green_)); ecb4f1560e 2013-02-07 kinaba: int blue_[] = ; ecb4f1560e 2013-02-07 kinaba: vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); ecb4f1560e 2013-02-07 kinaba: int _ = ; ecb4f1560e 2013-02-07 kinaba: END ecb4f1560e 2013-02-07 kinaba: CASE(6) ecb4f1560e 2013-02-07 kinaba: int red_[] = ; ecb4f1560e 2013-02-07 kinaba: vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); ecb4f1560e 2013-02-07 kinaba: int green_[] = ; ecb4f1560e 2013-02-07 kinaba: vector <int> green(green_, green_+sizeof(green_)/sizeof(*green_)); ecb4f1560e 2013-02-07 kinaba: int blue_[] = ; ecb4f1560e 2013-02-07 kinaba: vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); ecb4f1560e 2013-02-07 kinaba: int _ = ; ecb4f1560e 2013-02-07 kinaba: END ecb4f1560e 2013-02-07 kinaba: */ ecb4f1560e 2013-02-07 kinaba: } ecb4f1560e 2013-02-07 kinaba: // END CUT HERE