[第 45 届ICPC 南京] F.Fireworks (期望+三分)

题目链接:F.Fireworks
题解

放出完美烟花的最短时间——就是在第k次放烟花时就能得到完美烟花,那么设时间期望为f(k)
得出 f ( k ) = k ∗ n + m 1 − ( 1 − p ) k , ( k 为 正 整 数 ) {f(k)=\frac{k*n+m}{1-(1-p)^k}},(k为正整数) f(k)=1(1p)kkn+m(k)
现在可以将问题转化为求函数最小值。
通过求一次导发现函数不单调,再次求导得出函数为单峰函数,那么对于单峰函数求极值可以通过三分得到。

代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<iomanip>
#include<unordered_set>
#include<unordered_map>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x

const double PI=acos(-1.0);
const double eps=1e-10;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

double n,m,p,ans;

double f(double x) { 
	double tmp=(x*n+m)/(1.0-pow(1-p, x));
	ans=min(tmp,ans);
	return tmp;
}

int main()
{
	cout<<setprecision(8)<<fixed;
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> m >> p;
		p/=10000.0;
		ll l=1.0,r=1e12;
		ans=1e18;
		while (r>l) {
			ll midl=(l+r)/2.0;
			ll midr=(midl+r)/2.0;
			if(f(midl)<=f(midr)) r=midr;
			else l=midl;
		}
		cout << ans << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值