Skip to content

Commit 0513db4

Browse files
committed
srm 217 div 1 250
1 parent 800170f commit 0513db4

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

PlayGame.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef pair<int,int> pi;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
18+
class PlayGame{
19+
public:
20+
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;
39+
}
40+
41+
// 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 << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << 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,
49+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
50+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
51+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
52+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
53+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
54+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
55+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
56+
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,
60+
491, 378, 495, 789, 507, 381, 685, 530, 603, 394,
61+
7, 704, 101, 620, 859, 490, 744, 495, 379, 781,
62+
550, 356, 950, 628, 177, 373, 132, 740, 946, 609,
63+
29, 329, 57, 636, 132, 843, 860, 594, 718, 849}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {16, 127, 704, 614, 218, 67, 169, 621, 340, 319,
64+
366, 658, 798, 803, 524, 608, 794, 896, 145, 627,
65+
401, 253, 137, 851, 67, 426, 571, 302, 546, 225,
66+
311, 111, 804, 135, 284, 784, 890, 786, 740, 612,
67+
360, 852, 228, 859, 229, 249, 540, 979, 55, 82}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 25084; verify_case(4, Arg2, saveCreatures(Arg0, Arg1)); }
68+
69+
// END CUT HERE
70+
71+
};
72+
73+
// BEGIN CUT HERE
74+
int main(){
75+
76+
PlayGame ___test;
77+
___test.run_test(-1);
78+
}
79+
// END CUT HERE

PlayGame.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>You are playing a computer game and a big fight is planned between two armies.
2+
You and your computer opponent will line up your respective units in two rows, with each of your units facing exactly one of your opponent's units and vice versa. Then, each pair of units, who face each other will fight and the stronger one will be victorious, while the weaker one will be captured. If two opposing units are equally strong, your unit will lose and be captured. You know how the computer will arrange its units, and must decide how to line up yours. You want to maximize the sum of the strengths of your units that are not captured during the battle.
3+
<br></br>
4+
You will be given a vector &lt;int&gt; <b>you</b> and a vector &lt;int&gt; <b>computer</b> that specify the strengths of the units that you and the computer have, respectively. The return value should be an int, the maximum total strength of your units that are not captured.</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>PlayGame</td></tr><tr><td>Method:</td><td>saveCreatures</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;, vector &lt;int&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int saveCreatures(vector &lt;int&gt; you, vector &lt;int&gt; computer)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>you</b> and <b>computer</b> will have the same number of elements.</td></tr><tr><td align="center" valign="top">-</td><td><b>you</b> and <b>computer</b> will contain between 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>you</b> and <b>computer</b> will be between 1 and 1000, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{5, 15, 100, 1, 5}</pre></td></tr><tr><td><pre>{5, 15, 100, 1, 5}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 120</pre></td></tr><tr><td><table><tr><td colspan="2">Your units with strengths of 100 and 15, along with one of your strength 5 units, can avoid capture.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
5+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
6+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
7+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
8+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000}</pre></td></tr><tr><td><pre>{1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
9+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
10+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
11+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
12+
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">All units are equally strong, so all your units are captured.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}</pre></td></tr><tr><td><pre>{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 99</pre></td></tr><tr><td><table><tr><td colspan="2">You assign your weakest unit to the computer's strongest unit. Then you assign all your other units in such a way that each of your units has a strength one higher than the opposing unit. So all your units except the weakest one avoid capture.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}</pre></td></tr><tr><td><pre>{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 65</pre></td></tr><tr><td><table><tr><td colspan="2">All your units can win.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{651, 321, 106, 503, 227, 290, 915, 549, 660, 115,
13+
491, 378, 495, 789, 507, 381, 685, 530, 603, 394,
14+
7, 704, 101, 620, 859, 490, 744, 495, 379, 781,
15+
550, 356, 950, 628, 177, 373, 132, 740, 946, 609,
16+
29, 329, 57, 636, 132, 843, 860, 594, 718, 849}</pre></td></tr><tr><td><pre>{16, 127, 704, 614, 218, 67, 169, 621, 340, 319,
17+
366, 658, 798, 803, 524, 608, 794, 896, 145, 627,
18+
401, 253, 137, 851, 67, 426, 571, 302, 546, 225,
19+
311, 111, 804, 135, 284, 784, 890, 786, 740, 612,
20+
360, 852, 228, 859, 229, 249, 540, 979, 55, 82}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 25084</pre></td></tr><tr><td></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)