#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); #define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { (( cerr << x << ", " ), ...) << '\n'; } #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else #define deb(...) 0 #endif // https://github.com/kth-competitive-programming/kactl/blob/main/kactl.pdf typedef complex<double> C; typedef vector<double> vd; void fft(vector<C> &a) { int n = sz(a), L = 31 - __builtin_clz(n); static vector<complex<ld>> R(2,1); static vector<C> rt(2,1); for(static int k = 2; k < n; k *= 2) { R.resize(n); rt.resize(n); auto x = polar(1.0L, acos(-1.0L) / k); fwd(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2]; } vi rev(n); rep(i,n) rev[i] = (rev[i/2] | (i & 1) << L) / 2; rep(i, n) if (i < rev[i]) swap(a[i], a[rev[i]]); for(int k = 1; k < n; k*=2) { for(int i = 0; i < n; i += 2 * k) rep (j,k) { C z = rt[j+k] * a[i+j+k]; a[i+j+k] = a[i+j] - z; a[i+j] += z; } } } vd conv(const vd &a, const vd &b) { if(a.empty() || b.empty()) return {}; vd res(sz(a) + sz(b) - 1); int L = 32 - __builtin_clz(sz(res)), n = 1 << L; vector<C> in(n), out(n); copy(all(a), begin(in)); rep(i,sz(b)) in[i].imag(b[i]); fft(in); for(C& x: in) x *= x; rep(i,n) out[i] = in[-i & (n-1)] - conj(in[i]); fft(out); rep(i,sz(res)) res[i] = imag(out[i]) / (4*n); return res; } #define S 65536 vd tree[2*S+7]; vector<pii> cum; void gett(int l, int r, int ll, int rr, int w) { if(l > rr || r < ll) return; if(l >= ll && r <= rr) { cum.pb({r-l+1, w}); return; } int sr = (l+r)/2; gett(l,sr,ll,rr,w*2); gett(sr+1,r,ll,rr,w*2+1); } vd mult(int l, int r) { cum.clear(); gett(1,S,l,r,1); sort(all(cum)); vd tmp = tree[cum[0].second]; for(int i = 1; i < cum.size(); i++) { tmp = conv(tmp, tree[cum[i].second]); } return tmp; } int n,t; ld calc(vd &V, int k) { ld p = 0; for(int i = 0; i < V.size(); i++) { int score = i - (k-i); if(score >= t) p += V[i]; } return p; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); cin >> n >> t; // n = 50000; // t = 12500; vd V; // default_random_engine gen; // uniform_real_distribution<double> dist(0.0, 1.0); for(int i = 1; i <= n; i++) { double x; cin >> x; // x = dist(gen); V.pb(x); } sort(all(V)); reverse(all(V)); for(int i = 0; i < V.size(); i++) { tree[i+S] = {1-V[i], V[i]}; } for(int i = V.size(); i <= S; i++) { tree[i+S] = {1}; } for(int i = S-1; i >= 1; i--) { tree[i] = conv(tree[i*2], tree[i*2+1]); } int C = max(1,4*(int)(sqrt(n-t))); int K = 2; int b = t; int where = b; vd tmp = mult(1,b); ld maks = calc(tmp, b); for(int i = t+1; i <= min(n,t + K*C); i++) { vd tmp2; tmp2.resize(i+1); ld p = V[i-1]; tmp2[0] = tmp[0] * ((ld)1-p); tmp2.back() = tmp.back() * p; for(int j = 1; j < i; j++) { tmp2[j] = tmp[j-1] * (p) + tmp[j] * ((ld)1-p); } swap(tmp, tmp2); // ans = max(ans, calc(tmp, i)); ld pp = calc(tmp, i); if(pp > maks) { maks = pp; where = i; } } tmp = mult(1,b); while(1) { if(b == n) break; int lastt = b; b += C; b = min(b,n); vd tmp2 = mult(lastt+1, b); tmp = conv(tmp, tmp2); ld p = calc(tmp, b); if(p > maks){ maks = p; where = b; } } int s = max(t, where-K*C); tmp = mult(1,s); ld ans = calc(tmp, s); for(int i = s+1; i <= min(n,where+K*C); i++) { vd tmp2; tmp2.resize(i+1); ld p = V[i-1]; tmp2[0] = tmp[0] * ((ld)1-p); tmp2.back() = tmp.back() * p; for(int j = 1; j < i; j++) { tmp2[j] = tmp[j-1] * (p) + tmp[j] * ((ld)1-p); } swap(tmp, tmp2); ans = max(ans, calc(tmp, i)); } cout << ans << "\n"; } /* 5 2 0.77 0.85 0.75 0.98 0.6 */
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); #define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { (( cerr << x << ", " ), ...) << '\n'; } #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else #define deb(...) 0 #endif // https://github.com/kth-competitive-programming/kactl/blob/main/kactl.pdf typedef complex<double> C; typedef vector<double> vd; void fft(vector<C> &a) { int n = sz(a), L = 31 - __builtin_clz(n); static vector<complex<ld>> R(2,1); static vector<C> rt(2,1); for(static int k = 2; k < n; k *= 2) { R.resize(n); rt.resize(n); auto x = polar(1.0L, acos(-1.0L) / k); fwd(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2]; } vi rev(n); rep(i,n) rev[i] = (rev[i/2] | (i & 1) << L) / 2; rep(i, n) if (i < rev[i]) swap(a[i], a[rev[i]]); for(int k = 1; k < n; k*=2) { for(int i = 0; i < n; i += 2 * k) rep (j,k) { C z = rt[j+k] * a[i+j+k]; a[i+j+k] = a[i+j] - z; a[i+j] += z; } } } vd conv(const vd &a, const vd &b) { if(a.empty() || b.empty()) return {}; vd res(sz(a) + sz(b) - 1); int L = 32 - __builtin_clz(sz(res)), n = 1 << L; vector<C> in(n), out(n); copy(all(a), begin(in)); rep(i,sz(b)) in[i].imag(b[i]); fft(in); for(C& x: in) x *= x; rep(i,n) out[i] = in[-i & (n-1)] - conj(in[i]); fft(out); rep(i,sz(res)) res[i] = imag(out[i]) / (4*n); return res; } #define S 65536 vd tree[2*S+7]; vector<pii> cum; void gett(int l, int r, int ll, int rr, int w) { if(l > rr || r < ll) return; if(l >= ll && r <= rr) { cum.pb({r-l+1, w}); return; } int sr = (l+r)/2; gett(l,sr,ll,rr,w*2); gett(sr+1,r,ll,rr,w*2+1); } vd mult(int l, int r) { cum.clear(); gett(1,S,l,r,1); sort(all(cum)); vd tmp = tree[cum[0].second]; for(int i = 1; i < cum.size(); i++) { tmp = conv(tmp, tree[cum[i].second]); } return tmp; } int n,t; ld calc(vd &V, int k) { ld p = 0; for(int i = 0; i < V.size(); i++) { int score = i - (k-i); if(score >= t) p += V[i]; } return p; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); cin >> n >> t; // n = 50000; // t = 12500; vd V; // default_random_engine gen; // uniform_real_distribution<double> dist(0.0, 1.0); for(int i = 1; i <= n; i++) { double x; cin >> x; // x = dist(gen); V.pb(x); } sort(all(V)); reverse(all(V)); for(int i = 0; i < V.size(); i++) { tree[i+S] = {1-V[i], V[i]}; } for(int i = V.size(); i <= S; i++) { tree[i+S] = {1}; } for(int i = S-1; i >= 1; i--) { tree[i] = conv(tree[i*2], tree[i*2+1]); } int C = max(1,4*(int)(sqrt(n-t))); int K = 2; int b = t; int where = b; vd tmp = mult(1,b); ld maks = calc(tmp, b); for(int i = t+1; i <= min(n,t + K*C); i++) { vd tmp2; tmp2.resize(i+1); ld p = V[i-1]; tmp2[0] = tmp[0] * ((ld)1-p); tmp2.back() = tmp.back() * p; for(int j = 1; j < i; j++) { tmp2[j] = tmp[j-1] * (p) + tmp[j] * ((ld)1-p); } swap(tmp, tmp2); // ans = max(ans, calc(tmp, i)); ld pp = calc(tmp, i); if(pp > maks) { maks = pp; where = i; } } tmp = mult(1,b); while(1) { if(b == n) break; int lastt = b; b += C; b = min(b,n); vd tmp2 = mult(lastt+1, b); tmp = conv(tmp, tmp2); ld p = calc(tmp, b); if(p > maks){ maks = p; where = b; } } int s = max(t, where-K*C); tmp = mult(1,s); ld ans = calc(tmp, s); for(int i = s+1; i <= min(n,where+K*C); i++) { vd tmp2; tmp2.resize(i+1); ld p = V[i-1]; tmp2[0] = tmp[0] * ((ld)1-p); tmp2.back() = tmp.back() * p; for(int j = 1; j < i; j++) { tmp2[j] = tmp[j-1] * (p) + tmp[j] * ((ld)1-p); } swap(tmp, tmp2); ans = max(ans, calc(tmp, i)); } cout << ans << "\n"; } /* 5 2 0.77 0.85 0.75 0.98 0.6 */ |