[ICPC2020南京F] Fireworks - 期望,三分
Description
你每做一个烟花要n分钟,释放已做好的所有烟花需要m分钟,每只烟花成功释放的概率为p。问你在采取最优策略的前提下,直到成功释放第一个烟花时最小的期望时间花费。
Solution
每轮的时间开销,乘上至少成功释放一个烟花的时间期望(几何级数),得到得期望时间花费,由于是单峰的三分即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m;
int p;
double qpow(double p, int q)
{
return (q & 1 ? p : 1) * (q ? qpow(p * p, q / 2) : 1);
}
double f(int k)
{
return (1.0 * k * n + m) / (1.0 - qpow(1 - p * 0.0001, k));
}
void solve()
{
cin >> n >> m >> p;
double l = 1, r = 1e9;
while (r - l > 3)
{
int mid_l = (2 * l + r) / 3;
int mid_r = (l + 2 * r) / 3;
mid_l = max(mid_l, 1ll);
mid_r = max(mid_r, 1ll);
if (f(mid_l) < f(mid_r))
r = mid_r;
else
l = mid_l;
}
double min_ans = 1e18;
for (int i = 1; i <= 12; i++)
{
int mid = i + l - 6;
mid = max(mid, 1ll);
min_ans = min(min_ans, f(mid));
}
cout << fixed << setprecision(10) << min_ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
solve();
}
}