|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <set> |
| 6 | +#include <map> |
| 7 | +using namespace std; |
| 8 | +int a[55]; |
| 9 | +int b[55]; |
| 10 | + |
| 11 | +class ZigZag { |
| 12 | + public: |
| 13 | + int longestZigZag(vector <int> t) |
| 14 | + { |
| 15 | + int n = t.size(); |
| 16 | + a[n-1] = b[n-1] = 1; |
| 17 | + for(int i = n-2; i >= 0; i--){ |
| 18 | + int maxa = 0; |
| 19 | + for(int j = i+1; j < n; j++) |
| 20 | + if(t[j] > t[i]) maxa = max(maxa, b[j]); |
| 21 | + a[i] = maxa+1; |
| 22 | + |
| 23 | + int maxb = 0; |
| 24 | + for(int j = i+1; j < n; j++) |
| 25 | + if(t[j] < t[i]) maxb = max(maxb, a[j]); |
| 26 | + b[i] = maxb+1; |
| 27 | + } |
| 28 | + return max(a[0], b[0]); |
| 29 | + } |
| 30 | + |
| 31 | +// BEGIN CUT HERE |
| 32 | + public: |
| 33 | + 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(); if ((Case == -1) || (Case == 5)) test_case_5(); } |
| 34 | + private: |
| 35 | + 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(); } |
| 36 | + 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; } } |
| 37 | + void test_case_0() { int Arr0[] = { 1, 7, 4, 9, 2, 5 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; verify_case(0, Arg1, longestZigZag(Arg0)); } |
| 38 | + void test_case_1() { int Arr0[] = { 1, 17, 5, 10, 13, 15, 10, 5, 16, 8 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; verify_case(1, Arg1, longestZigZag(Arg0)); } |
| 39 | + void test_case_2() { int Arr0[] = { 44 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(2, Arg1, longestZigZag(Arg0)); } |
| 40 | + void test_case_3() { int Arr0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(3, Arg1, longestZigZag(Arg0)); } |
| 41 | + void test_case_4() { int Arr0[] = { 70, 55, 13, 2, 99, 2, 80, 80, 80, 80, 100, 19, 7, 5, 5, 5, 1000, 32, 32 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 8; verify_case(4, Arg1, longestZigZag(Arg0)); } |
| 42 | + void test_case_5() { int Arr0[] = { 374, 40, 854, 203, 203, 156, 362, 279, 812, 955, |
| 43 | +600, 947, 978, 46, 100, 953, 670, 862, 568, 188, |
| 44 | +67, 669, 810, 704, 52, 861, 49, 640, 370, 908, |
| 45 | +477, 245, 413, 109, 659, 401, 483, 308, 609, 120, |
| 46 | +249, 22, 176, 279, 23, 22, 617, 462, 459, 244 } |
| 47 | +; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 36; verify_case(5, Arg1, longestZigZag(Arg0)); } |
| 48 | + |
| 49 | +// END CUT HERE |
| 50 | + |
| 51 | +}; |
| 52 | + |
| 53 | +// BEGIN CUT HERE |
| 54 | + int main() |
| 55 | + { |
| 56 | + ZigZag ___test; |
| 57 | + ___test.run_test(-1); |
| 58 | + } |
| 59 | +// END CUT HERE |
0 commit comments