207f32d4fb 2018-04-14 kinaba: #include <iostream> 207f32d4fb 2018-04-14 kinaba: #include <sstream> 207f32d4fb 2018-04-14 kinaba: #include <iomanip> 207f32d4fb 2018-04-14 kinaba: #include <vector> 207f32d4fb 2018-04-14 kinaba: #include <string> 207f32d4fb 2018-04-14 kinaba: #include <map> 207f32d4fb 2018-04-14 kinaba: #include <set> 207f32d4fb 2018-04-14 kinaba: #include <algorithm> 207f32d4fb 2018-04-14 kinaba: #include <numeric> 207f32d4fb 2018-04-14 kinaba: #include <iterator> 207f32d4fb 2018-04-14 kinaba: #include <functional> 207f32d4fb 2018-04-14 kinaba: #include <complex> 207f32d4fb 2018-04-14 kinaba: #include <queue> 207f32d4fb 2018-04-14 kinaba: #include <stack> 207f32d4fb 2018-04-14 kinaba: #include <cmath> 207f32d4fb 2018-04-14 kinaba: #include <cassert> 207f32d4fb 2018-04-14 kinaba: #include <tuple> 207f32d4fb 2018-04-14 kinaba: using namespace std; 207f32d4fb 2018-04-14 kinaba: typedef long long LL; 207f32d4fb 2018-04-14 kinaba: typedef complex<double> CMP; 207f32d4fb 2018-04-14 kinaba: 207f32d4fb 2018-04-14 kinaba: struct Rat { 207f32d4fb 2018-04-14 kinaba: LL s, b; // s/b 207f32d4fb 2018-04-14 kinaba: 207f32d4fb 2018-04-14 kinaba: Rat(LL s, LL b) : s(s), b(b) {} 207f32d4fb 2018-04-14 kinaba: 207f32d4fb 2018-04-14 kinaba: // take abs() too 207f32d4fb 2018-04-14 kinaba: Rat operator-(const Rat& rhs) { 207f32d4fb 2018-04-14 kinaba: LL a = s, c = rhs.s, d = rhs.b; 207f32d4fb 2018-04-14 kinaba: return Rat(abs(a*d - b*c), b*d); 207f32d4fb 2018-04-14 kinaba: } 207f32d4fb 2018-04-14 kinaba: 207f32d4fb 2018-04-14 kinaba: bool operator<(const Rat& rhs) { 207f32d4fb 2018-04-14 kinaba: unsigned long long a = s, c = rhs.s, d = rhs.b; 207f32d4fb 2018-04-14 kinaba: return a*d < b*c; 207f32d4fb 2018-04-14 kinaba: } 207f32d4fb 2018-04-14 kinaba: }; 207f32d4fb 2018-04-14 kinaba: 207f32d4fb 2018-04-14 kinaba: class MinimizeAbsoluteDifferenceDiv1 { public: 207f32d4fb 2018-04-14 kinaba: vector <int> findTuple(vector <int> x) 207f32d4fb 2018-04-14 kinaba: { 207f32d4fb 2018-04-14 kinaba: vector<int> idx = { 0,1,2,3,4 }; 207f32d4fb 2018-04-14 kinaba: vector<int> best; 207f32d4fb 2018-04-14 kinaba: Rat bestval(0, 0); 207f32d4fb 2018-04-14 kinaba: do { 207f32d4fb 2018-04-14 kinaba: Rat val = Rat(x[idx[0]], x[idx[1]]) - Rat(x[idx[2]], x[idx[3]]); 207f32d4fb 2018-04-14 kinaba: if (best.empty() || val < bestval) 207f32d4fb 2018-04-14 kinaba: best = idx, bestval = val; 207f32d4fb 2018-04-14 kinaba: } while (next_permutation(idx.begin(), idx.end())); 207f32d4fb 2018-04-14 kinaba: 207f32d4fb 2018-04-14 kinaba: best.resize(4); 207f32d4fb 2018-04-14 kinaba: return best; 207f32d4fb 2018-04-14 kinaba: } 207f32d4fb 2018-04-14 kinaba: }; 207f32d4fb 2018-04-14 kinaba: 207f32d4fb 2018-04-14 kinaba: // BEGIN CUT HERE 207f32d4fb 2018-04-14 kinaba: #include <ctime> 207f32d4fb 2018-04-14 kinaba: double start_time; string timer() 207f32d4fb 2018-04-14 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 207f32d4fb 2018-04-14 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 207f32d4fb 2018-04-14 kinaba: { os << "{ "; 207f32d4fb 2018-04-14 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 207f32d4fb 2018-04-14 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 207f32d4fb 2018-04-14 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { 207f32d4fb 2018-04-14 kinaba: bool ok = (Expected == Received); 207f32d4fb 2018-04-14 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 207f32d4fb 2018-04-14 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } 207f32d4fb 2018-04-14 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 207f32d4fb 2018-04-14 kinaba: #define END verify_case(_, MinimizeAbsoluteDifferenceDiv1().findTuple(x));} 207f32d4fb 2018-04-14 kinaba: int main(){ 207f32d4fb 2018-04-14 kinaba: 207f32d4fb 2018-04-14 kinaba: CASE(0) 207f32d4fb 2018-04-14 kinaba: int x_[] = {1,1,1,1,1}; 207f32d4fb 2018-04-14 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 207f32d4fb 2018-04-14 kinaba: int __[] = {0, 1, 2, 3 }; 207f32d4fb 2018-04-14 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 207f32d4fb 2018-04-14 kinaba: END 207f32d4fb 2018-04-14 kinaba: CASE(1) 207f32d4fb 2018-04-14 kinaba: int x_[] = {2,3,5,7,11}; 207f32d4fb 2018-04-14 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 207f32d4fb 2018-04-14 kinaba: int __[] = {0, 3, 1, 4 }; 207f32d4fb 2018-04-14 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 207f32d4fb 2018-04-14 kinaba: END 207f32d4fb 2018-04-14 kinaba: CASE(2) 207f32d4fb 2018-04-14 kinaba: int x_[] = {8, 2, 4, 2, 6}; 207f32d4fb 2018-04-14 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 207f32d4fb 2018-04-14 kinaba: int __[] = {1, 0, 3, 4 }; 207f32d4fb 2018-04-14 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 207f32d4fb 2018-04-14 kinaba: END 207f32d4fb 2018-04-14 kinaba: CASE(3) 207f32d4fb 2018-04-14 kinaba: int x_[] = {124, 182, 9, 33, 70}; 207f32d4fb 2018-04-14 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 207f32d4fb 2018-04-14 kinaba: int __[] = {2, 4, 3, 1 }; 207f32d4fb 2018-04-14 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 207f32d4fb 2018-04-14 kinaba: END 207f32d4fb 2018-04-14 kinaba: CASE(4) 207f32d4fb 2018-04-14 kinaba: int x_[] = {9947, 3, 7, 11, 13}; 207f32d4fb 2018-04-14 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 207f32d4fb 2018-04-14 kinaba: int __[] = {1, 4, 3, 0 }; 207f32d4fb 2018-04-14 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 207f32d4fb 2018-04-14 kinaba: END 207f32d4fb 2018-04-14 kinaba: CASE(5) 207f32d4fb 2018-04-14 kinaba: int x_[] = {10000, 4, 10, 4, 10}; 207f32d4fb 2018-04-14 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 207f32d4fb 2018-04-14 kinaba: int __[] = {1, 2, 3, 4 }; 207f32d4fb 2018-04-14 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 207f32d4fb 2018-04-14 kinaba: END 207f32d4fb 2018-04-14 kinaba: /* 207f32d4fb 2018-04-14 kinaba: CASE(6) 207f32d4fb 2018-04-14 kinaba: int x_[] = ; 207f32d4fb 2018-04-14 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 207f32d4fb 2018-04-14 kinaba: int __[] = ; 207f32d4fb 2018-04-14 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 207f32d4fb 2018-04-14 kinaba: END 207f32d4fb 2018-04-14 kinaba: CASE(7) 207f32d4fb 2018-04-14 kinaba: int x_[] = ; 207f32d4fb 2018-04-14 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 207f32d4fb 2018-04-14 kinaba: int __[] = ; 207f32d4fb 2018-04-14 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 207f32d4fb 2018-04-14 kinaba: END 207f32d4fb 2018-04-14 kinaba: */ 207f32d4fb 2018-04-14 kinaba: } 207f32d4fb 2018-04-14 kinaba: // END CUT HERE