Skip to content

Commit 9725491

Browse files
committed
srm 626 div 2 500
1 parent a46b145 commit 9725491

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

Diff for: FixedDiceGameDiv2.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
10+
class FixedDiceGameDiv2 {
11+
public:
12+
double getExpectation(int a, int b)
13+
{
14+
int s = 0, c = 0;
15+
for(int aa = 2; aa <= a; aa++){
16+
int bb = min(b, aa-1);
17+
s += aa*bb;
18+
c += bb;
19+
}
20+
return (double)s/(double)c;
21+
22+
}
23+
24+
// BEGIN CUT HERE
25+
public:
26+
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(); }
27+
private:
28+
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(); }
29+
void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
30+
void test_case_0() { int Arg0 = 2; int Arg1 = 2; double Arg2 = 2.0; verify_case(0, Arg2, getExpectation(Arg0, Arg1)); }
31+
void test_case_1() { int Arg0 = 4; int Arg1 = 2; double Arg2 = 3.2; verify_case(1, Arg2, getExpectation(Arg0, Arg1)); }
32+
void test_case_2() { int Arg0 = 3; int Arg1 = 3; double Arg2 = 2.6666666666666665; verify_case(2, Arg2, getExpectation(Arg0, Arg1)); }
33+
void test_case_3() { int Arg0 = 11; int Arg1 = 13; double Arg2 = 7.999999999999999; verify_case(3, Arg2, getExpectation(Arg0, Arg1)); }
34+
35+
// END CUT HERE
36+
37+
};
38+
39+
// BEGIN CUT HERE
40+
int main()
41+
{
42+
FixedDiceGameDiv2 ___test;
43+
___test.run_test(-1);
44+
}
45+
// END CUT HERE

Diff for: FixedDiceGameDiv2.txt

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
PROBLEM STATEMENT
2+
3+
Alice and Bob are playing a game.
4+
Alice has an a-sided die.
5+
Bob has a b-sided die.
6+
(The faces of an n-sided die have numbers 1 through n written on them.)
7+
8+
9+
10+
The game is simple:
11+
Each player rolls their die, and the player who rolls a strictly larger number wins.
12+
(It is possible that neither player wins.)
13+
14+
15+
16+
You are given the ints a and b.
17+
The players already rolled their dice.
18+
You do not know what numbers they rolled, but you know that Alice won the game.
19+
Compute and return the expected value of the number Alice rolled.
20+
21+
22+
23+
DEFINITION
24+
Class:FixedDiceGameDiv2
25+
Method:getExpectation
26+
Parameters:int, int
27+
Returns:double
28+
Method signature:double getExpectation(int a, int b)
29+
30+
31+
NOTES
32+
-Your return value must have absolute or relative error smaller than 1e-9.
33+
-The expected value can be seen as the average of many experiments. In our case, suppose that you folow a lot of games, and each time Alice wins, you write down her score. As you watch more and more games, the average of all numbers you have written down will converge to the expected value you should return.
34+
35+
36+
CONSTRAINTS
37+
-a and b will each be between 2 and 1000, inclusive.
38+
39+
40+
EXAMPLES
41+
42+
0)
43+
2
44+
2
45+
46+
Returns: 2.0
47+
48+
Alice can only win if she rolls a 2 and Bob rolls a 1. Thus, if we know Alice wins, we know she rolled a 2.
49+
50+
1)
51+
4
52+
2
53+
54+
Returns: 3.2
55+
56+
57+
58+
2)
59+
3
60+
3
61+
62+
Returns: 2.6666666666666665
63+
64+
65+
66+
3)
67+
11
68+
13
69+
70+
Returns: 7.999999999999999
71+
72+

0 commit comments

Comments
 (0)