#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <map> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define SIZE(c) ((int)(c).size()) #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef vector<double> VD; double p[50000]; map<int, VD> cache; double res[2][50001]; struct C { double re, im; C(double re = 0, double im = 0) : re(re), im(im) {} C operator+(const C& z) const { return C(re + z.re, im + z.im); } C operator-(const C& z) const { return C(re - z.re, im - z.im); } C operator*(const C& z) const { return C(re * z.re - im * z.im, re * z.im + im * z.re); } }; C ac[1 << 17], at[1 << 17], bc[1 << 17], bt[1 << 17], cc[1 << 17], ct[1 << 17]; C rootm(int k, int n) { double x = -2 * M_PI * k / n; return C(cos(x), sin(x)); } int round2(int i) { int j = 1; while (j < i) j <<= 1; return j; } void fft(C x[], int n, int s, C r[]) { if (n == 1) { r[0] = x[0]; return; } int h = n >> 1; int s2 = s << 1; fft(x, h, s2, r); fft(x + s, h, s2, r + h); REP(k,h) { C p = r[k]; C q = rootm(k, n) * r[k + h]; r[k] = p + q; r[k + h] = p - q; } } void ifft(C x[], int n, C r[]) { REP(i,n) x[i].im = -x[i].im; fft(x, n, 1, r); REP(i,n) r[i].im = -r[i].im; REP(i,n) r[i] = r[i] * (1.0L / n); } VD conv(const VD& a, const VD& b) { if (SIZE(a) < 20 || SIZE(b) < 20 || SIZE(a) + SIZE(b) < 2000) { VD c(SIZE(a) + SIZE(b) - 1); REP(i,SIZE(a)) REP(j,SIZE(b)) c[i + j] += a[i] * b[j]; return c; } int cn = SIZE(a) + SIZE(b) - 1; int n = round2(cn); REP(i,SIZE(a)) ac[i] = a[i]; FOR(i,SIZE(a),n) ac[i] = 0; REP(i,SIZE(b)) bc[i] = b[i]; FOR(i,SIZE(b),n) bc[i] = 0; fft(ac, n, 1, at); fft(bc, n, 1, bt); REP(i,n) ct[i] = at[i] * bt[i]; ifft(ct, n, cc); VD c(cn); REP(i,cn) c[i] = cc[i].re; return c; } VD seq(int x, int y, bool save = 0) { if (x + 1 == y) return VD{1 - p[x], p[x]}; if (!x) { VAR(it,cache.lower_bound(y)); if (it != cache.end() && it->FT == y) return it->SD; if (it != cache.begin()) { --it; VD r = conv(it->SD, seq(it->FT, y)); if (save) cache[y] = r; return r; } } int m = (x + y) >> 1; VD r = conv(seq(x, m, save), seq(m, y)); if (save) cache[y] = r; return r; } double check(int n, int t) { VD v = seq(0, n, 1); double r = 0; FOR(i,(n+t+1)>>1,n+1) r += v[i]; return r; } double scan(int start, int end, int t) { VD ss = seq(0, start, 1); REP(j,start+1) res[start & 1][j] = ss[j]; double best = 0; FOR(j,(start+t+1)>>1,start+1) best += res[start & 1][j]; FOR(i,start+1,end+1) { REP(j,i+1) { res[i & 1][j] = 0; if (j) res[i & 1][j] = res[(i + 1) & 1][j - 1] * p[i - 1]; if (j < i) res[i & 1][j] += res[(i + 1) & 1][j] * (1 - p[i - 1]); } double res1 = 0; FOR(j,(i+t+1)>>1,i+1) res1 += res[i & 1][j]; best = max(best, res1); } return best; } int main() { INT(n); INT(t); REP(i,n) { double pp; scanf("%lf", &pp); p[i] = pp; } sort(p, p + n, greater<double>()); int d = (n + 1 - t) >> 1; int a = 0; int b = d - 1; while (a + 1000 < b) { int m1 = ((a << 1) + b) / 3; int m2 = (a + (b << 1)) / 3; if (check(t + (m1 << 1), t) < check(t + (m2 << 1), t)) a = m1; else b = m2; } double r = max(r, scan(t + (a << 1), t + (b << 1), t)); d = (n - t) >> 1; a = 0; b = d - 1; while (a + 1000 < b) { int m1 = ((a << 1) + b) / 3; int m2 = (a + (b << 1)) / 3; if (check(t + (m1 << 1) + 1, t) < check(t + (m2 << 1) + 1, t)) a = m1; else b = m2; } r = max(r, scan(t + (a << 1) + 1, t + (b << 1) + 1, t)); printf("%.9f\n", r); }
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <map> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define SIZE(c) ((int)(c).size()) #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef vector<double> VD; double p[50000]; map<int, VD> cache; double res[2][50001]; struct C { double re, im; C(double re = 0, double im = 0) : re(re), im(im) {} C operator+(const C& z) const { return C(re + z.re, im + z.im); } C operator-(const C& z) const { return C(re - z.re, im - z.im); } C operator*(const C& z) const { return C(re * z.re - im * z.im, re * z.im + im * z.re); } }; C ac[1 << 17], at[1 << 17], bc[1 << 17], bt[1 << 17], cc[1 << 17], ct[1 << 17]; C rootm(int k, int n) { double x = -2 * M_PI * k / n; return C(cos(x), sin(x)); } int round2(int i) { int j = 1; while (j < i) j <<= 1; return j; } void fft(C x[], int n, int s, C r[]) { if (n == 1) { r[0] = x[0]; return; } int h = n >> 1; int s2 = s << 1; fft(x, h, s2, r); fft(x + s, h, s2, r + h); REP(k,h) { C p = r[k]; C q = rootm(k, n) * r[k + h]; r[k] = p + q; r[k + h] = p - q; } } void ifft(C x[], int n, C r[]) { REP(i,n) x[i].im = -x[i].im; fft(x, n, 1, r); REP(i,n) r[i].im = -r[i].im; REP(i,n) r[i] = r[i] * (1.0L / n); } VD conv(const VD& a, const VD& b) { if (SIZE(a) < 20 || SIZE(b) < 20 || SIZE(a) + SIZE(b) < 2000) { VD c(SIZE(a) + SIZE(b) - 1); REP(i,SIZE(a)) REP(j,SIZE(b)) c[i + j] += a[i] * b[j]; return c; } int cn = SIZE(a) + SIZE(b) - 1; int n = round2(cn); REP(i,SIZE(a)) ac[i] = a[i]; FOR(i,SIZE(a),n) ac[i] = 0; REP(i,SIZE(b)) bc[i] = b[i]; FOR(i,SIZE(b),n) bc[i] = 0; fft(ac, n, 1, at); fft(bc, n, 1, bt); REP(i,n) ct[i] = at[i] * bt[i]; ifft(ct, n, cc); VD c(cn); REP(i,cn) c[i] = cc[i].re; return c; } VD seq(int x, int y, bool save = 0) { if (x + 1 == y) return VD{1 - p[x], p[x]}; if (!x) { VAR(it,cache.lower_bound(y)); if (it != cache.end() && it->FT == y) return it->SD; if (it != cache.begin()) { --it; VD r = conv(it->SD, seq(it->FT, y)); if (save) cache[y] = r; return r; } } int m = (x + y) >> 1; VD r = conv(seq(x, m, save), seq(m, y)); if (save) cache[y] = r; return r; } double check(int n, int t) { VD v = seq(0, n, 1); double r = 0; FOR(i,(n+t+1)>>1,n+1) r += v[i]; return r; } double scan(int start, int end, int t) { VD ss = seq(0, start, 1); REP(j,start+1) res[start & 1][j] = ss[j]; double best = 0; FOR(j,(start+t+1)>>1,start+1) best += res[start & 1][j]; FOR(i,start+1,end+1) { REP(j,i+1) { res[i & 1][j] = 0; if (j) res[i & 1][j] = res[(i + 1) & 1][j - 1] * p[i - 1]; if (j < i) res[i & 1][j] += res[(i + 1) & 1][j] * (1 - p[i - 1]); } double res1 = 0; FOR(j,(i+t+1)>>1,i+1) res1 += res[i & 1][j]; best = max(best, res1); } return best; } int main() { INT(n); INT(t); REP(i,n) { double pp; scanf("%lf", &pp); p[i] = pp; } sort(p, p + n, greater<double>()); int d = (n + 1 - t) >> 1; int a = 0; int b = d - 1; while (a + 1000 < b) { int m1 = ((a << 1) + b) / 3; int m2 = (a + (b << 1)) / 3; if (check(t + (m1 << 1), t) < check(t + (m2 << 1), t)) a = m1; else b = m2; } double r = max(r, scan(t + (a << 1), t + (b << 1), t)); d = (n - t) >> 1; a = 0; b = d - 1; while (a + 1000 < b) { int m1 = ((a << 1) + b) / 3; int m2 = (a + (b << 1)) / 3; if (check(t + (m1 << 1) + 1, t) < check(t + (m2 << 1) + 1, t)) a = m1; else b = m2; } r = max(r, scan(t + (a << 1) + 1, t + (b << 1) + 1, t)); printf("%.9f\n", r); } |