|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <set> |
| 6 | +#include <map> |
| 7 | +#include <cstring> |
| 8 | +#include <climits> |
| 9 | +using namespace std; |
| 10 | +typedef pair<int,int> pi; |
| 11 | +typedef vector<int> vi; |
| 12 | +typedef vector<vi> vvi; |
| 13 | +typedef vector<string> vs; |
| 14 | +typedef vector<vs> vvs; |
| 15 | +int c, a1, a2; |
| 16 | + |
| 17 | +class TeamPhoto { |
| 18 | + public: |
| 19 | + |
| 20 | + int findMin(vi b, int a, int z){ |
| 21 | + |
| 22 | + int x1[4] = { b[0], b[a-1], b[a-1], b[0] }; |
| 23 | + int y1[4] = { b[a-1], b[0], b[0], b[a-1] }; |
| 24 | + int x2[4] = { b[z-1], b[z-1], b[a], b[a] }; |
| 25 | + int y2[4] = { b[a], b[a], b[z-1], b[z-1] }; |
| 26 | + int d1 = 0, d2 = 0; |
| 27 | + d1 = b[a-1]-b[0]; d2 = b[z-1]-b[a]; |
| 28 | + int minv = 1000000; |
| 29 | + |
| 30 | + for(int i = 0; i < 4; i++){ |
| 31 | + minv = min(minv, min( abs(a1-x1[i]) + abs(a2-x2[i]), abs(a1-x2[i]) + abs(a2-x1[i]) ) + d1 + d2 + abs(c-y1[i]) + abs(c-y2[i])); |
| 32 | + } |
| 33 | + return minv; |
| 34 | + } |
| 35 | + |
| 36 | + int minDiff(vector <int> h) |
| 37 | + { |
| 38 | + c = h[0], a1 = h[1], a2 = h[2]; |
| 39 | + h.erase(h.begin(), h.begin()+3); |
| 40 | + sort(h.begin(), h.end()); |
| 41 | + int k = h.size(); |
| 42 | + if(k%2 == 0) return findMin(h, k/2, k); |
| 43 | + return min(findMin(h, k/2, k), findMin(h, k/2+1, k)); |
| 44 | + } |
| 45 | + |
| 46 | +// BEGIN CUT HERE |
| 47 | + public: |
| 48 | + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); } |
| 49 | + private: |
| 50 | + template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } |
| 51 | + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } |
| 52 | + void test_case_0() { int Arr0[] = {80,82,81,50,90,65}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 79; verify_case(0, Arg1, minDiff(Arg0)); } |
| 53 | + void test_case_1() { int Arr0[] = {70,82,91,50,50,50,50,50,50}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 113; verify_case(1, Arg1, minDiff(Arg0)); } |
| 54 | + void test_case_2() { int Arr0[] = {13, 17, 11, 12, 10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; verify_case(2, Arg1, minDiff(Arg0)); } |
| 55 | + |
| 56 | +// END CUT HERE |
| 57 | + |
| 58 | +}; |
| 59 | + |
| 60 | +// BEGIN CUT HERE |
| 61 | + int main() |
| 62 | + { |
| 63 | + TeamPhoto ___test; |
| 64 | + ___test.run_test(-1); |
| 65 | + } |
| 66 | +// END CUT HERE |
0 commit comments