#include <cstdio> #include <utility> #include <cmath> #include <algorithm> #include <functional> using ll = long long; using namespace std; const int N = 50005; double dp[N][2]; double p[N]; int main() { int n, t; scanf("%d%d", &n, &t); for (int i = 1; i <= n; i++) { scanf("%lf", &p[i]); } sort(p + 1, p + n + 1, greater<double>()); dp[0][0] = 1; int must_have_n = t + (n - t + 1) / 2; for (int i = 1; i < t; i++) { int least_calc = max(1, must_have_n - n + i); int i_mod = i % 2; int i_dec_mod = 1 - i_mod; dp[0][i_mod] = dp[0][i_dec_mod] * (1 - p[i]); for (int j = least_calc; j <= i; j++) { dp[j][i_mod] = p[i] * dp[j - 1][i_dec_mod] + (1 - p[i]) * dp[j][i_dec_mod]; } } double best_res = 0; double cnd_res = 0; for (int i = t; i <= n; i++) { int must_have = t + (i - t + 1) / 2; int least_calc = max(1, must_have_n - n + i); int i_mod = i % 2; int i_dec_mod = 1 - i_mod; dp[0][i_mod] = dp[0][i_dec_mod] * (1 - p[i]); for (int j = least_calc; j < must_have_n; j++) { dp[j][i_mod] = p[i] * dp[j - 1][i_dec_mod] + (1 - p[i]) * dp[j][i_dec_mod]; } if ((i - t) % 2 == 0) { cnd_res += p[i] * dp[must_have - 1][i_dec_mod]; } else { cnd_res -= (1 - p[i]) * dp[must_have - 1][i_dec_mod]; } best_res = max(best_res, cnd_res); } printf("%.7lf\n", best_res); return 0; }
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 | #include <cstdio> #include <utility> #include <cmath> #include <algorithm> #include <functional> using ll = long long; using namespace std; const int N = 50005; double dp[N][2]; double p[N]; int main() { int n, t; scanf("%d%d", &n, &t); for (int i = 1; i <= n; i++) { scanf("%lf", &p[i]); } sort(p + 1, p + n + 1, greater<double>()); dp[0][0] = 1; int must_have_n = t + (n - t + 1) / 2; for (int i = 1; i < t; i++) { int least_calc = max(1, must_have_n - n + i); int i_mod = i % 2; int i_dec_mod = 1 - i_mod; dp[0][i_mod] = dp[0][i_dec_mod] * (1 - p[i]); for (int j = least_calc; j <= i; j++) { dp[j][i_mod] = p[i] * dp[j - 1][i_dec_mod] + (1 - p[i]) * dp[j][i_dec_mod]; } } double best_res = 0; double cnd_res = 0; for (int i = t; i <= n; i++) { int must_have = t + (i - t + 1) / 2; int least_calc = max(1, must_have_n - n + i); int i_mod = i % 2; int i_dec_mod = 1 - i_mod; dp[0][i_mod] = dp[0][i_dec_mod] * (1 - p[i]); for (int j = least_calc; j < must_have_n; j++) { dp[j][i_mod] = p[i] * dp[j - 1][i_dec_mod] + (1 - p[i]) * dp[j][i_dec_mod]; } if ((i - t) % 2 == 0) { cnd_res += p[i] * dp[must_have - 1][i_dec_mod]; } else { cnd_res -= (1 - p[i]) * dp[must_have - 1][i_dec_mod]; } best_res = max(best_res, cnd_res); } printf("%.7lf\n", best_res); return 0; } |