@@ -18,34 +18,38 @@ typedef vector<vs> vvs;
1818class PlayGame {
1919 public:
2020 int saveCreatures (vector <int > you, vector <int > computer) {
21- int n = you.size ();
22- if (n == 0 ) return 0 ;
23- sort (you.begin (), you.end ());
24- sort (computer.begin (), computer.end ());
25- if (you[n-1 ] <= computer[n-1 ]){
26- swap (you[0 ], you[n-1 ]);
27- you.pop_back ();
28- computer.pop_back ();
29- return saveCreatures (you, computer);
30- }
31-
32- int i = n-1 ;
33- while (i >= 0 && you[i] > computer[n-1 ]) i--;
34- swap (you[++i], you[n-1 ]);
35- int m = you[n-1 ];
36- you.pop_back ();
37- computer.pop_back ();
38- return saveCreatures (you, computer) + m;
21+ int n = you.size ();
22+ if (n == 0 ) return 0 ;
23+
24+ int done[55 ];
25+ fill (done, done+50 , 0 );
26+ int sol = 0 ;
27+ for (int i = 0 ; i < you.size (); i++){
28+ sort (you.begin (), you.begin ()+n);
29+ sort (computer.begin (), computer.begin ()+n);
30+ if (you[n-1 ] <= computer[n-1 ]){
31+ swap (you[0 ], you[n-1 ]);
32+ }
33+ else {
34+ int i = n-1 ;
35+ while (i >= 0 && you[i] > computer[n-1 ]) i--;
36+ swap (you[++i], you[n-1 ]);
37+ int m = you[n-1 ];
38+ sol += m;
39+ }
40+ n--;
41+ }
42+ return sol;
3943 }
4044
4145// BEGIN CUT HERE
42- public:
43- 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 (); }
44- private:
45- 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 (); }
46- 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 << " \t Expected: \" " << Expected << ' \" ' << endl; cerr << " \t Received: \" " << Received << ' \" ' << endl; } }
47- void test_case_0 () { int Arr0[] = {5 , 15 , 100 , 1 , 5 }; vector <int > Arg0 (Arr0, Arr0 + (sizeof (Arr0) / sizeof (Arr0[0 ]))); int Arr1[] = {5 , 15 , 100 , 1 , 5 }; vector <int > Arg1 (Arr1, Arr1 + (sizeof (Arr1) / sizeof (Arr1[0 ]))); int Arg2 = 120 ; verify_case (0 , Arg2, saveCreatures (Arg0, Arg1)); }
48- void test_case_1 () { int Arr0[] = {1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 ,
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 << " \t Expected: \" " << Expected << ' \" ' << endl; cerr << " \t Received: \" " << Received << ' \" ' << endl; } }
51+ void test_case_0 () { int Arr0[] = {5 , 15 , 100 , 1 , 5 }; vector <int > Arg0 (Arr0, Arr0 + (sizeof (Arr0) / sizeof (Arr0[0 ]))); int Arr1[] = {5 , 15 , 100 , 1 , 5 }; vector <int > Arg1 (Arr1, Arr1 + (sizeof (Arr1) / sizeof (Arr1[0 ]))); int Arg2 = 120 ; verify_case (0 , Arg2, saveCreatures (Arg0, Arg1)); }
52+ void test_case_1 () { int Arr0[] = {1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 ,
4953 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 ,
5054 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 ,
5155 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 ,
@@ -54,9 +58,9 @@ class PlayGame{
5458 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 ,
5559 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 ,
5660 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 }; vector <int > Arg1 (Arr1, Arr1 + (sizeof (Arr1) / sizeof (Arr1[0 ]))); int Arg2 = 0 ; verify_case (1 , Arg2, saveCreatures (Arg0, Arg1)); }
57- void test_case_2 () { int Arr0[] = {1 , 3 , 5 , 7 , 9 , 11 , 13 , 15 , 17 , 19 }; vector <int > Arg0 (Arr0, Arr0 + (sizeof (Arr0) / sizeof (Arr0[0 ]))); int Arr1[] = {2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 , 20 }; vector <int > Arg1 (Arr1, Arr1 + (sizeof (Arr1) / sizeof (Arr1[0 ]))); int Arg2 = 99 ; verify_case (2 , Arg2, saveCreatures (Arg0, Arg1)); }
58- void test_case_3 () { int Arr0[] = {2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 }; vector <int > Arg0 (Arr0, Arr0 + (sizeof (Arr0) / sizeof (Arr0[0 ]))); int Arr1[] = {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 }; vector <int > Arg1 (Arr1, Arr1 + (sizeof (Arr1) / sizeof (Arr1[0 ]))); int Arg2 = 65 ; verify_case (3 , Arg2, saveCreatures (Arg0, Arg1)); }
59- void test_case_4 () { int Arr0[] = {651 , 321 , 106 , 503 , 227 , 290 , 915 , 549 , 660 , 115 ,
61+ void test_case_2 () { int Arr0[] = {1 , 3 , 5 , 7 , 9 , 11 , 13 , 15 , 17 , 19 }; vector <int > Arg0 (Arr0, Arr0 + (sizeof (Arr0) / sizeof (Arr0[0 ]))); int Arr1[] = {2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 , 20 }; vector <int > Arg1 (Arr1, Arr1 + (sizeof (Arr1) / sizeof (Arr1[0 ]))); int Arg2 = 99 ; verify_case (2 , Arg2, saveCreatures (Arg0, Arg1)); }
62+ void test_case_3 () { int Arr0[] = {2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 }; vector <int > Arg0 (Arr0, Arr0 + (sizeof (Arr0) / sizeof (Arr0[0 ]))); int Arr1[] = {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 }; vector <int > Arg1 (Arr1, Arr1 + (sizeof (Arr1) / sizeof (Arr1[0 ]))); int Arg2 = 65 ; verify_case (3 , Arg2, saveCreatures (Arg0, Arg1)); }
63+ void test_case_4 () { int Arr0[] = {651 , 321 , 106 , 503 , 227 , 290 , 915 , 549 , 660 , 115 ,
6064 491 , 378 , 495 , 789 , 507 , 381 , 685 , 530 , 603 , 394 ,
6165 7 , 704 , 101 , 620 , 859 , 490 , 744 , 495 , 379 , 781 ,
6266 550 , 356 , 950 , 628 , 177 , 373 , 132 , 740 , 946 , 609 ,
0 commit comments