#pragma GCC optimize("Ofast,fast-math")
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
namespace FFT {
#define rep(a,b,c) for(int a=b;a<c;a++)
typedef complex<double> C;
typedef vector<ld> vd;
typedef vector<int> vi;
void fft(vector<C>& a) {
int n = sz(a), L = 31 - __builtin_clz(n);
static vector<complex<long double>> R(2, 1);
static vector<C> rt(2, 1); // (^ 10% faster if double)
for (static int k = 2; k < n; k *= 2) {
R.resize(n); rt.resize(n);
auto x = polar(1.0L, acos(-1.0L) / k);
rep(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2];
}
vi rev(n);
rep(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2;
rep(i,0,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,0,k) {
// C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled) /// -line
auto x = (double *)&rt[j+k], y = (double *)&a[i+j+k]; /// exclude-line
C z(x[0]*y[0] - x[1]*y[1], x[0]*y[1] + x[1]*y[0]); /// exclude-line
a[i + j + k] = a[i + j] - z;
a[i + j] += z;
}
}
vd multiply(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,0,sz(b)) in[i].imag(b[i]);
fft(in);
for (C& x : in) x *= x;
rep(i,0,n) out[i] = in[-i & (n - 1)] - conj(in[i]);
fft(out);
rep(i,0,sz(res)) res[i] = imag(out[i]) / (4 * n);
return res;
}
}
vector<ld> dividefft(vector<ld> v) {
if(sz(v) == 0) return vector<ld>{1};
else if(sz(v) == 1) return vector<ld>{1 - v[0], v[0]};
vector<ld> L, R;
for(int i = 0; i < sz(v); i++)
(i % 2? L : R).emplace_back(v[i]);
auto A = dividefft(L);
auto B = dividefft(R);
return FFT::multiply(A, B);
}
vector<ld> advance(vector<ld> x, ld p) {
vector<ld> nv(sz(x) + 1);
for(int i = 0; i < sz(x); i++) {
nv[i + 1] += x[i] * p;
nv[i] += x[i] * (1 - p);
}
return nv;
}
ld getrez(int req, int idx, vector<ld> R) {
ld sum = 0;
for(int i = (idx + 1 + req) / 2; i < sz(R); i++)
sum += R[i];
return sum;
}
ld getrez(int req, vector<ld> tofind) {
if(sz(tofind) < req) return -1;
auto R = dividefft(tofind);
return getrez(req, sz(tofind), R);
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n, req;
cin >> n >> req;
vector<ld> v(n);
for(auto &x : v) cin >> x;
sort(all(v), greater<ld>());
if(n <= 17000) {
vector<ld> t(1, 1);
ld mx = 0;
for(auto x : v) {
t = advance(t, x);
if(sz(t) >= req + 1) mx = max(mx, getrez(req, sz(t) - 1, t));
//if(sz(t) % 1000 == 0) {
//cerr << mx << '\n';
//}
}
cout << setprecision(7) << fixed << mx << '\n';
return 0;
}
vector<ld> incluse;
int P;
for(P = 0; P < req; P++) incluse.emplace_back(v[P]);
for(; P + 1 < sz(v) && v[P + 1] > 0.51; P += 2) {
incluse.emplace_back(v[P]);
incluse.emplace_back(v[P + 1]);
}
vector<ld> extras;
for(int i = P; i < n; i++) extras.emplace_back(v[i]);
while(sz(extras) > 8 && rbegin(extras)[4] < 0.49) extras.pop_back();
//cerr << sz(incluse) << ' ' << sz(extras) << ' ' << sz(incluse) + sz(extras) << ' ' << n << '\n';
if(sz(extras) % 2 != 0) extras.pop_back();
int threshold = max(1000, sz(extras) / 15);
int CNT = 0;
while(CNT < 2 && threshold > min(100, sz(extras) / 101)) { CNT++;
int l = sz(extras), r = 0;
vector<pair<ld, int>> bests;
auto simpleeval = [&](int x) {
auto A = incluse; copy(begin(extras), begin(extras) + x, back_inserter(A));
auto a = getrez(req, A);
a = (floor(a * 1e10)) * 1e-10;
return a;
};
//for(int i = 0; i < sz(extras); i += threshold) {
//bests.emplace_back(simpleeval(i), i);
//}
//sort(all(bests), greater<pair<ld, int>>());
//int CL = 0, CR = 0;
//for(auto [v, i] : bests) {
//if(v < 1e-9) break;
//if(i > r && CR < 4)
//r = max(r, i + threshold - 1), CR++;
//if(i < l && CL < 4)
//l = min(l, i), CL++;
//}
l = min(l, 0);
r = max(r, sz(extras));
if(l > r) { threshold /= 2; continue; }
//cerr << l << ' ' << r << '\n';
int L = l;
vector<ld> prereq = incluse; copy(begin(extras), begin(extras) + L, back_inserter(prereq));
auto V = dividefft(prereq);
auto eval = [&](int x) {
auto A = vector<ld>{}; copy(begin(extras) + L, begin(extras) + x, back_inserter(A));
A = FFT::multiply(dividefft(A), V);
auto B = incluse; copy(begin(extras), begin(extras) + x, back_inserter(B));
B = dividefft(B);
auto a = getrez(req, x + sz(incluse), A);
a = (floor(a * 1e10)) * 1e-10;
return a;
};
while(r - l > 2000) {
int m1 = l + (r - l + 1) / 3, m2 = r - (r - l + 1) / 3;
if(m1 % 2 != 0) m1--;
if(m2 % 2 != 0) m2++;
//cerr << l << ' ' << eval(m1) << ' ' << eval(m2) << ' ' << r << '\n';
if(eval(m1) > eval(m2)) r = m2;
else l = m1;
}
ld mx = max({simpleeval(l), simpleeval(r), simpleeval(0), simpleeval(sz(extras))});
auto partial = incluse; copy(begin(extras), begin(extras) + l, back_inserter(partial));
auto slideV = dividefft(partial);
for(int i = l; i + 1 <= r; i += 2) {
slideV = advance(slideV, extras[i]);
slideV = advance(slideV, extras[i + 1]);
//assert(abs(getrez(req, sz(slideV) - 1, slideV) - eval(i + 2)) < 1e-0);
mx = max(mx, getrez(req, sz(slideV) - 1, slideV));
//cerr << i + 2 << ' ' << simpleeval(i + 2) << '\n';
}
cout<< setprecision(7) << fixed << mx << '\n';
return 0;
}
cout << "0\n";
return 0;
}
/**
Binecuvanteaza Doamne Ukraina.
* teste naspa: 1026
--
*/
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 | #pragma GCC optimize("Ofast,fast-math") #include <bits/stdc++.h> #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; using ld = long double; //#define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; namespace FFT { #define rep(a,b,c) for(int a=b;a<c;a++) typedef complex<double> C; typedef vector<ld> vd; typedef vector<int> vi; void fft(vector<C>& a) { int n = sz(a), L = 31 - __builtin_clz(n); static vector<complex<long double>> R(2, 1); static vector<C> rt(2, 1); // (^ 10% faster if double) for (static int k = 2; k < n; k *= 2) { R.resize(n); rt.resize(n); auto x = polar(1.0L, acos(-1.0L) / k); rep(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2]; } vi rev(n); rep(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2; rep(i,0,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,0,k) { // C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled) /// -line auto x = (double *)&rt[j+k], y = (double *)&a[i+j+k]; /// exclude-line C z(x[0]*y[0] - x[1]*y[1], x[0]*y[1] + x[1]*y[0]); /// exclude-line a[i + j + k] = a[i + j] - z; a[i + j] += z; } } vd multiply(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,0,sz(b)) in[i].imag(b[i]); fft(in); for (C& x : in) x *= x; rep(i,0,n) out[i] = in[-i & (n - 1)] - conj(in[i]); fft(out); rep(i,0,sz(res)) res[i] = imag(out[i]) / (4 * n); return res; } } vector<ld> dividefft(vector<ld> v) { if(sz(v) == 0) return vector<ld>{1}; else if(sz(v) == 1) return vector<ld>{1 - v[0], v[0]}; vector<ld> L, R; for(int i = 0; i < sz(v); i++) (i % 2? L : R).emplace_back(v[i]); auto A = dividefft(L); auto B = dividefft(R); return FFT::multiply(A, B); } vector<ld> advance(vector<ld> x, ld p) { vector<ld> nv(sz(x) + 1); for(int i = 0; i < sz(x); i++) { nv[i + 1] += x[i] * p; nv[i] += x[i] * (1 - p); } return nv; } ld getrez(int req, int idx, vector<ld> R) { ld sum = 0; for(int i = (idx + 1 + req) / 2; i < sz(R); i++) sum += R[i]; return sum; } ld getrez(int req, vector<ld> tofind) { if(sz(tofind) < req) return -1; auto R = dividefft(tofind); return getrez(req, sz(tofind), R); } signed main() { cin.tie(0) -> sync_with_stdio(0); int n, req; cin >> n >> req; vector<ld> v(n); for(auto &x : v) cin >> x; sort(all(v), greater<ld>()); if(n <= 17000) { vector<ld> t(1, 1); ld mx = 0; for(auto x : v) { t = advance(t, x); if(sz(t) >= req + 1) mx = max(mx, getrez(req, sz(t) - 1, t)); //if(sz(t) % 1000 == 0) { //cerr << mx << '\n'; //} } cout << setprecision(7) << fixed << mx << '\n'; return 0; } vector<ld> incluse; int P; for(P = 0; P < req; P++) incluse.emplace_back(v[P]); for(; P + 1 < sz(v) && v[P + 1] > 0.51; P += 2) { incluse.emplace_back(v[P]); incluse.emplace_back(v[P + 1]); } vector<ld> extras; for(int i = P; i < n; i++) extras.emplace_back(v[i]); while(sz(extras) > 8 && rbegin(extras)[4] < 0.49) extras.pop_back(); //cerr << sz(incluse) << ' ' << sz(extras) << ' ' << sz(incluse) + sz(extras) << ' ' << n << '\n'; if(sz(extras) % 2 != 0) extras.pop_back(); int threshold = max(1000, sz(extras) / 15); int CNT = 0; while(CNT < 2 && threshold > min(100, sz(extras) / 101)) { CNT++; int l = sz(extras), r = 0; vector<pair<ld, int>> bests; auto simpleeval = [&](int x) { auto A = incluse; copy(begin(extras), begin(extras) + x, back_inserter(A)); auto a = getrez(req, A); a = (floor(a * 1e10)) * 1e-10; return a; }; //for(int i = 0; i < sz(extras); i += threshold) { //bests.emplace_back(simpleeval(i), i); //} //sort(all(bests), greater<pair<ld, int>>()); //int CL = 0, CR = 0; //for(auto [v, i] : bests) { //if(v < 1e-9) break; //if(i > r && CR < 4) //r = max(r, i + threshold - 1), CR++; //if(i < l && CL < 4) //l = min(l, i), CL++; //} l = min(l, 0); r = max(r, sz(extras)); if(l > r) { threshold /= 2; continue; } //cerr << l << ' ' << r << '\n'; int L = l; vector<ld> prereq = incluse; copy(begin(extras), begin(extras) + L, back_inserter(prereq)); auto V = dividefft(prereq); auto eval = [&](int x) { auto A = vector<ld>{}; copy(begin(extras) + L, begin(extras) + x, back_inserter(A)); A = FFT::multiply(dividefft(A), V); auto B = incluse; copy(begin(extras), begin(extras) + x, back_inserter(B)); B = dividefft(B); auto a = getrez(req, x + sz(incluse), A); a = (floor(a * 1e10)) * 1e-10; return a; }; while(r - l > 2000) { int m1 = l + (r - l + 1) / 3, m2 = r - (r - l + 1) / 3; if(m1 % 2 != 0) m1--; if(m2 % 2 != 0) m2++; //cerr << l << ' ' << eval(m1) << ' ' << eval(m2) << ' ' << r << '\n'; if(eval(m1) > eval(m2)) r = m2; else l = m1; } ld mx = max({simpleeval(l), simpleeval(r), simpleeval(0), simpleeval(sz(extras))}); auto partial = incluse; copy(begin(extras), begin(extras) + l, back_inserter(partial)); auto slideV = dividefft(partial); for(int i = l; i + 1 <= r; i += 2) { slideV = advance(slideV, extras[i]); slideV = advance(slideV, extras[i + 1]); //assert(abs(getrez(req, sz(slideV) - 1, slideV) - eval(i + 2)) < 1e-0); mx = max(mx, getrez(req, sz(slideV) - 1, slideV)); //cerr << i + 2 << ' ' << simpleeval(i + 2) << '\n'; } cout<< setprecision(7) << fixed << mx << '\n'; return 0; } cout << "0\n"; return 0; } /** Binecuvanteaza Doamne Ukraina. * teste naspa: 1026 -- */ |
English