#include <iostream> #include <vector> #include <algorithm> #include <complex> #include <numbers> using namespace std; const int FFT_THRESHOLD = 20; void fft(vector<complex<double>>& a, bool invert) { int n = a.size(); if (n == 1) return; vector<complex<double>> a0(n / 2), a1(n / 2); for (int i = 0; 2 * i < n; i++) { a0[i] = a[2*i]; a1[i] = a[2*i+1]; } fft(a0, invert); fft(a1, invert); double ang = 2 * numbers::pi_v<double> / n * (invert ? -1 : 1); complex<double> w(1), wn(cos(ang), sin(ang)); for (int i = 0; 2 * i < n; i++) { a[i] = a0[i] + w * a1[i]; a[i + n/2] = a0[i] - w * a1[i]; if (invert) { a[i] /= 2; a[i + n/2] /= 2; } w *= wn; } } vector<complex<double>> mul(vector<complex<double>> x, vector<complex<double>> y) { int target_size = x.size() + y.size() - 1; if (x.size() * y.size() < FFT_THRESHOLD) { while (x.size() < target_size) x.emplace_back(0); for (int s = x.size() - 1; s >= 0; --s) { complex<double> dot(0); for (int i = 0; i <= s; ++i) { int j = s - i; if (j < y.size()) dot += x[i] * y[j]; } x[s] = dot; } return x; } while ((target_size&-target_size) != target_size) { ++target_size; } while (x.size() < target_size) x.emplace_back(0); while (y.size() < target_size) y.emplace_back(0); fft(x, false); fft(y, false); for (int i = 0; i < target_size; ++i) x[i] *= y[i]; fft(x, true); for (int i = 0; i < target_size; ++i) x[i].imag(0); return x; } vector<complex<double>> prod(const vector<double> &ps, int start, int end) { if (start == end - 1) { return {ps[start], 1 - ps[start]}; } int middle = (start + end) >> 1; return mul(prod(ps, start, middle), prod(ps, middle, end)); } vector<double> success_prob(const vector<double>& ps, int answers, int t) { int allowed_fails = (answers - t) >> 1; vector<complex<double>> k_fails = prod(ps, 0, answers); vector<double> ret(k_fails.size()); for (int i = 0; i < ret.size(); ++i) ret[i] = k_fails[i].real(); return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, t; cin >> n >> t; vector<double> probs(n); for (int i = 0; i < n; ++i) cin >> probs[i]; sort(probs.begin(), probs.end(), greater<double>()); int gthalf = 0; for (gthalf = t % 2; gthalf < probs.size() - 1 && probs[gthalf] > 0.5; gthalf += 2); int start = max(t, gthalf - 2); vector<double> k_fails = success_prob(probs, start, t); int allowed_fails = (start - t) >> 1; double best = 0.0; if (allowed_fails >= 0) { double exam_pass = 0.0; for (int k = 0; k <= allowed_fails; ++k) exam_pass += k_fails[k]; if (exam_pass > best) best = exam_pass; } while (k_fails.size() <= n) k_fails.emplace_back(0); for (int i = start; i < min(n, start + 2000); ++i) { double success = probs[i], fail = 1.0 - probs[i]; for (int k = i+1; k > 0; --k) { k_fails[k] = k_fails[k] * success + k_fails[k - 1] * fail; } k_fails[0] *= success; int allowed_fails = ((i + 1) - t) >> 1; if (allowed_fails >= 0) { double exam_pass = 0.0; for (int k = 0; k <= allowed_fails; ++k) exam_pass += k_fails[k]; if (exam_pass > best) best = exam_pass; } } cout.precision(8); cout << fixed << best << endl; }
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 139 140 141 142 143 144 145 146 147 148 | #include <iostream> #include <vector> #include <algorithm> #include <complex> #include <numbers> using namespace std; const int FFT_THRESHOLD = 20; void fft(vector<complex<double>>& a, bool invert) { int n = a.size(); if (n == 1) return; vector<complex<double>> a0(n / 2), a1(n / 2); for (int i = 0; 2 * i < n; i++) { a0[i] = a[2*i]; a1[i] = a[2*i+1]; } fft(a0, invert); fft(a1, invert); double ang = 2 * numbers::pi_v<double> / n * (invert ? -1 : 1); complex<double> w(1), wn(cos(ang), sin(ang)); for (int i = 0; 2 * i < n; i++) { a[i] = a0[i] + w * a1[i]; a[i + n/2] = a0[i] - w * a1[i]; if (invert) { a[i] /= 2; a[i + n/2] /= 2; } w *= wn; } } vector<complex<double>> mul(vector<complex<double>> x, vector<complex<double>> y) { int target_size = x.size() + y.size() - 1; if (x.size() * y.size() < FFT_THRESHOLD) { while (x.size() < target_size) x.emplace_back(0); for (int s = x.size() - 1; s >= 0; --s) { complex<double> dot(0); for (int i = 0; i <= s; ++i) { int j = s - i; if (j < y.size()) dot += x[i] * y[j]; } x[s] = dot; } return x; } while ((target_size&-target_size) != target_size) { ++target_size; } while (x.size() < target_size) x.emplace_back(0); while (y.size() < target_size) y.emplace_back(0); fft(x, false); fft(y, false); for (int i = 0; i < target_size; ++i) x[i] *= y[i]; fft(x, true); for (int i = 0; i < target_size; ++i) x[i].imag(0); return x; } vector<complex<double>> prod(const vector<double> &ps, int start, int end) { if (start == end - 1) { return {ps[start], 1 - ps[start]}; } int middle = (start + end) >> 1; return mul(prod(ps, start, middle), prod(ps, middle, end)); } vector<double> success_prob(const vector<double>& ps, int answers, int t) { int allowed_fails = (answers - t) >> 1; vector<complex<double>> k_fails = prod(ps, 0, answers); vector<double> ret(k_fails.size()); for (int i = 0; i < ret.size(); ++i) ret[i] = k_fails[i].real(); return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, t; cin >> n >> t; vector<double> probs(n); for (int i = 0; i < n; ++i) cin >> probs[i]; sort(probs.begin(), probs.end(), greater<double>()); int gthalf = 0; for (gthalf = t % 2; gthalf < probs.size() - 1 && probs[gthalf] > 0.5; gthalf += 2); int start = max(t, gthalf - 2); vector<double> k_fails = success_prob(probs, start, t); int allowed_fails = (start - t) >> 1; double best = 0.0; if (allowed_fails >= 0) { double exam_pass = 0.0; for (int k = 0; k <= allowed_fails; ++k) exam_pass += k_fails[k]; if (exam_pass > best) best = exam_pass; } while (k_fails.size() <= n) k_fails.emplace_back(0); for (int i = start; i < min(n, start + 2000); ++i) { double success = probs[i], fail = 1.0 - probs[i]; for (int k = i+1; k > 0; --k) { k_fails[k] = k_fails[k] * success + k_fails[k - 1] * fail; } k_fails[0] *= success; int allowed_fails = ((i + 1) - t) >> 1; if (allowed_fails >= 0) { double exam_pass = 0.0; for (int k = 0; k <= allowed_fails; ++k) exam_pass += k_fails[k]; if (exam_pass > best) best = exam_pass; } } cout.precision(8); cout << fixed << best << endl; } |