-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_princess.cpp
119 lines (91 loc) · 1.82 KB
/
save_princess.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
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int getSum(int arr[], int n) {
int count = 0;
for (int i = 0; i < n; ++i) {
count += arr[i];
}
return count;
}
void print_arr ( float arr[][10100], int n, int m ) {
cout << endl;
cout << endl;
cout << endl;
for ( int i = 0; i < n; i++ ) {
for ( int j = 0; j < m; j++ ) {
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << endl;
cout << endl;
}
void print_arr1(float arr[], int n) {
for (int i = 0; i < n; ++i){
cout << arr[i] << " ";
}
cout << endl << endl;
}
float winProbabilty(int n, int flag[], float prob[]) {
int sum = getSum(flag, n);
if ( sum % 2 == 0 ) {
sum = (sum / 2 ) ;
} else {
sum = (sum / 2) + 1;
}
float arr[n][10100];
for (int i = 0; i < n; i++){
for (int j = 0; j < sum; j++){
arr[i][j] = 0;
}
}
arr[0][0] = 1 - prob[0];
for (int i = 1; i < n; ++i) {
arr[i][0] = arr[i-1][0] * ( 1 - prob[i]);
}
arr[0][flag[0]] = prob[0];
for (int i = 1; i < n; ++i){
for (int j = 0; j < sum; ++j){
arr[i][j] = arr[i - 1][j] * ( 1 - prob[i]);
}
for (int j = 0; j < sum; ++j) {
if (arr[i - 1][j] != 0 ) {
if ( j + flag[i] < sum) {
// arr[i][j + flag[i]] = arr[i - 1][j + flag[i]] * ( prob[i]);
arr[i][j + flag[i]] += arr[i - 1][j] * (prob[i]);
}
}
}
}
float total_prob = 0;
for ( int i = 0; i < sum; i++ ) {
total_prob += arr[n-1][i];
}
return 1 - total_prob;
}
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int n;
cin >> n;
int flags[n];
for (int j = 0; j < n; ++j) {
cin >> flags[j];
}
float prob[n];
for (int j = 0; j < n; ++j) {
cin >> prob[j];
prob[j] *= 0.01;
}
printf("%.7f\n", winProbabilty(n, flags, prob));
}
return 0;
}