|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <set> |
| 6 | +#include <map> |
| 7 | +#include <cstring> |
| 8 | +using namespace std; |
| 9 | +int p[1005]; |
| 10 | +int a[1005]; |
| 11 | +int b[1005]; |
| 12 | + |
| 13 | +class BadNeighbors { |
| 14 | + public: |
| 15 | + int maxDonations(vector <int> d) |
| 16 | + { |
| 17 | + memset(p, 0, sizeof(p)); |
| 18 | + memset(a, 0, sizeof(a)); |
| 19 | + memset(b, 0, sizeof(b)); |
| 20 | + int n = d.size(); |
| 21 | + if(n == 2) return max(d[0], d[1]); |
| 22 | + n--; |
| 23 | + a[n] = d[n]; a[n-1] = 0; |
| 24 | + b[n] = 0; b[n-1] = d[n-1]; |
| 25 | + p[n] = p[n-1] = -1; |
| 26 | + int maxa = 0, maxb = 0; |
| 27 | + for(int i = n-2; i >= 1; i--){ |
| 28 | + maxa = 0, maxb = 0; |
| 29 | + for(int j = i+2; j <= n; j++){ |
| 30 | + if(a[j] > maxa) maxa = a[j]; |
| 31 | + if(b[j] > maxb) maxb = b[j]; |
| 32 | + } |
| 33 | + a[i] = d[i] + maxa; |
| 34 | + b[i] = d[i] + maxb; |
| 35 | + } |
| 36 | + maxa = 0; maxb = 0; |
| 37 | + for(int i = 1; i <= n; i++) maxa = max(maxa, a[i]); |
| 38 | + for(int i = 2; i <= n; i++) maxb = max(maxb, b[i]); |
| 39 | + cout<<a[1]<<endl<<b[1]<<endl; |
| 40 | + a[0] = maxa; |
| 41 | + b[0] = max(b[1], maxb+d[0]); |
| 42 | + return max(a[0], b[0]); |
| 43 | + } |
| 44 | + |
| 45 | +// BEGIN CUT HERE |
| 46 | + public: |
| 47 | + 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(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } |
| 48 | + private: |
| 49 | + 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(); } |
| 50 | + 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; } } |
| 51 | + void test_case_0() { int Arr0[] = { 10, 3, 2, 5, 7, 8 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 19; verify_case(0, Arg1, maxDonations(Arg0)); } |
| 52 | + void test_case_1() { int Arr0[] = { 11, 15 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 15; verify_case(1, Arg1, maxDonations(Arg0)); } |
| 53 | + void test_case_2() { int Arr0[] = { 7, 7, 7, 7, 7, 7, 7 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 21; verify_case(2, Arg1, maxDonations(Arg0)); } |
| 54 | + void test_case_3() { int Arr0[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 16; verify_case(3, Arg1, maxDonations(Arg0)); } |
| 55 | + void test_case_4() { int Arr0[] = { 94, 40, 49, 65, 21, 21, 106, 80, 92, 81, 679, 4, 61, |
| 56 | + 6, 237, 12, 72, 74, 29, 95, 265, 35, 47, 1, 61, 397, |
| 57 | + 52, 72, 37, 51, 1, 81, 45, 435, 7, 36, 57, 86, 81, 72 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2926; verify_case(4, Arg1, maxDonations(Arg0)); } |
| 58 | + |
| 59 | +// END CUT HERE |
| 60 | + |
| 61 | +}; |
| 62 | + |
| 63 | +// BEGIN CUT HERE |
| 64 | + int main() |
| 65 | + { |
| 66 | + BadNeighbors ___test; |
| 67 | + ___test.run_test(-1); |
| 68 | + } |
| 69 | +// END CUT HERE |
0 commit comments