其他分享
首页 > 其他分享> > 【Luogu P1984】烧水问题

【Luogu P1984】烧水问题

作者:互联网

SDOI2008]烧水问题

分析

这道题是一道较难以看出来的贪心。

首先先加热第一杯水,然后通过热传递,尽量加热温度最高的,在与次高的热传递中:

img

img

img

为了方便起见,每杯水升 \(1\)℃ 需要消耗 \(1\texttt{J}\),假设有 \(4\) 杯水。

第一杯水需要消耗 \(100\texttt{J}\) 的能量。

第二杯水需要消耗 \(50\texttt{J}\) 的能量。

第三杯水需要消耗 \(37.5\texttt{J}\) 的能量。

第四杯水需要消耗 \(31.25\texttt{J}\) 的能量。

……设第 \(i\) 杯水花费的能量为 \(\large{\mathrm{cost}_i}\)。

那么:

\[\mathrm{cost}_2=\frac{1}{2}\mathrm{cost}_1\\ \mathrm{cost}_3=\frac{3}{4}\mathrm{cost}_2\\ \mathrm{cost}_4=\frac{5}{6}\mathrm{cost}_3\\ \mathrm{cost}_{i+1}=\mathrm{cost}_i(1-\frac{1}{2i}),i\in\mathbb{N^+} \]

代码

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	double res,now;
	scanf("%d",&n);
	now=420000.00/n;
	for(int i=1; i<=n; i++) {
		res+=now,now*=(1-0.5/i);
	}
	printf("%.2lf",res);
	return 0;
}

标签:烧水,cost,int,Luogu,texttt,P1984,frac,杯水,mathrm
来源: https://www.cnblogs.com/Sam2007/p/14852667.html