Skip to content

Commit 56f4f3a

Browse files
committed
srm 639 div 2 500
1 parent a7ca20f commit 56f4f3a

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

AliceGameEasy.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// BEGIN CUT HERE
2+
3+
// END CUT HERE
4+
#include <iostream>
5+
#include <sstream>
6+
#include <set>
7+
#include <vector>
8+
#include <map>
9+
#include <algorithm>
10+
#include <cmath>
11+
using namespace std;
12+
13+
class AliceGameEasy{
14+
public:
15+
long long findMinimumValue(long long x, long long y) {
16+
long s = (x+y);
17+
if (s == 0) return 0;
18+
long k = sqrt(1+8*s);
19+
if(k*k == (1+8*s)){
20+
21+
long n = (k-1)/2;
22+
long sum = 0, c = 0, i = n;
23+
24+
while(i >= 0){
25+
if(sum >= x) return c;
26+
c++;
27+
sum += i;
28+
i--;
29+
}
30+
}
31+
return -1;
32+
}
33+
34+
// BEGIN CUT HERE
35+
public:
36+
void run_test(long 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(); }
37+
private:
38+
template <typename T> string prlong_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(); }
39+
void verify_case(long Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
40+
void test_case_0() { long long Arg0 = 7LL; long long Arg1 = 14LL; long long Arg2 = 2LL; verify_case(0, Arg2, findMinimumValue(Arg0, Arg1)); }
41+
void test_case_1() { long long Arg0 = 10LL; long long Arg1 = 0LL; long long Arg2 = 4LL; verify_case(1, Arg2, findMinimumValue(Arg0, Arg1)); }
42+
void test_case_2() { long long Arg0 = 932599670050LL; long long Arg1 = 67400241741LL; long long Arg2 = 1047062LL; verify_case(2, Arg2, findMinimumValue(Arg0, Arg1)); }
43+
void test_case_3() { long long Arg0 = 7LL; long long Arg1 = 13LL; long long Arg2 = -1LL; verify_case(3, Arg2, findMinimumValue(Arg0, Arg1)); }
44+
void test_case_4() { long long Arg0 = 0LL; long long Arg1 = 0LL; long long Arg2 = 0LL; verify_case(4, Arg2, findMinimumValue(Arg0, Arg1)); }
45+
void test_case_5() { long long Arg0 = 100000LL; long long Arg1 = 400500LL; long long Arg2 = 106LL; verify_case(5, Arg2, findMinimumValue(Arg0, Arg1)); }
46+
47+
// END CUT HERE
48+
49+
};
50+
51+
// BEGIN CUT HERE
52+
int main(){
53+
54+
AliceGameEasy ___test;
55+
___test.run_test(-1);
56+
}
57+
// END CUT HERE

AliceGameEasy.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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><p>
2+
Alice and Kirito just played a game.
3+
The game consisted of a finite (possibly empty) sequence of turns.
4+
You do not know the exact number of turns.
5+
The turns were numbered starting from 1.
6+
In each turn, exactly one of our two players won.
7+
The winner of turn i scored i points.
8+
</p>
9+
10+
<p>
11+
You are given two long longs <b>x</b> and <b>y</b>.
12+
Find out whether it is possible that at the end of the game Alice had exactly <b>x</b> points and Kirito had exactly <b>y</b> points.
13+
If it is possible, return the smallest number of turns Alice could have won.
14+
If the given final result is not possible, return -1 instead.
15+
</p></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>AliceGameEasy</td></tr><tr><td>Method:</td><td>findMinimumValue</td></tr><tr><td>Parameters:</td><td>long long, long long</td></tr><tr><td>Returns:</td><td>long long</td></tr><tr><td>Method signature:</td><td>long long findMinimumValue(long long x, long long y)</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>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>x</b> and <b>y</b> will be between 0 and 1,000,000,000,000(10^12), 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>7</pre></td></tr><tr><td><pre>14</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">This final result is possible.
16+
One possibility is that Alice won turns 1, 2, and 4 (for 1+2+4 = 7 points) and Kirito won turns 3, 5, and 6 (for 3+5+6 = 14 points).
17+
However, there are also some other possibilities in which Alice only won two of the six turns, so the correct answer is 2.</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>10</pre></td></tr><tr><td><pre>0</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</pre></td></tr><tr><td><table><tr><td colspan="2">There must have been four turns and Alice must have won all four of them.</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>932599670050</pre></td></tr><tr><td><pre>67400241741</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1047062</pre></td></tr><tr><td><table><tr><td colspan="2">Watch out for integer overflow.</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>7</pre></td></tr><tr><td><pre>13</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2"></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>0</pre></td></tr><tr><td><pre>0</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">5)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>100000</pre></td></tr><tr><td><pre>400500</pre></td></tr></table></td></tr><tr><td><pre>Returns: 106</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></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)