-
Notifications
You must be signed in to change notification settings - Fork 2
/
knockout.cpp
138 lines (122 loc) · 3.13 KB
/
knockout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#define _USE_MATH_DEFINES
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <stack>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
map<int, double> getOpponentProb(const vector<map<int, double>> & chances, int min, int max)
{
int size = max - min;
int midPoint = min + (size)/2;
if(size > 1)
{
auto leftSide = getOpponentProb(chances, min, midPoint);
auto rightSide = getOpponentProb(chances, midPoint, max);
// Interlace
map<int, double> newMap;
for(auto leftMember : leftSide)
{
int leftValue = leftMember.first;
double leftProb = leftMember.second;
for(auto rightMember : rightSide)
{
int rightValue = rightMember.first;
double rightProb = rightMember.second;
double total = rightValue + leftValue;
newMap[rightValue] += rightValue / total * rightProb * leftProb;
newMap[leftValue] += leftValue / total * rightProb * leftProb;
}
}
return newMap;
}
else
{
return chances[min];
}
}
double getSurviveChance(const vector<map<int, double>> & chances, int min, int max, int myValue)
{
int size = max - min;
int midPoint = min + (size)/2;
if(size > 1)
{
auto opponentChances = getOpponentProb(chances, min, midPoint);
auto surviveChance = getSurviveChance(chances, midPoint, max, myValue);
float newSurviveChance = 0.0;
for(auto opponent : opponentChances)
{
int strength = opponent.first;
double prob = opponent.second;
float total = strength + myValue;
newSurviveChance += myValue / total * prob;
}
return newSurviveChance * surviveChance;
}
return 1.0;
}
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
int members;
cin >> members;
members--;
int myValue;
cin >> myValue;
vector<int> opponents(members);
for(int i = 0; i < members; i++)
{
cin >> opponents[i];
}
sort(opponents.rbegin(), opponents.rend());
opponents.push_back(myValue);
members++;
// Initial round
int difference = (members) - (1 << int(log2(members)));
// Resolve our bracket down till we have a clean power of two
vector<map<int, double>> chances;
for(int i = 0; i < difference; i++)
{
map<int, double> thisChance;
float total = opponents[2 * i] + opponents[2 * i + 1];
int A = opponents[2 * i];
int B = opponents[2 * i + 1];
thisChance[A] += A / float(total);
thisChance[B] += B / float(total);
chances.push_back(thisChance);
}
for(int i = difference * 2; i < members; i++)
{
map<int, double> tempMap;
tempMap[opponents[i]] = 1.0;
chances.push_back(tempMap);
}
/*
for(auto mapThing : chances)
{
cout << "\n";
for(auto entry : mapThing)
{
cout << entry.first << " " << entry.second;
}
}
cout << "\n\n";
*/
// Now we have even maps
cout << fixed << setprecision(10);
cout << getSurviveChance(chances, 0, chances.size(), myValue) << "\n";
return 0;
}