Insurance

Contest2892 - 2021个人训练赛第39场

**

Insurance

**
时间限制: 1 Sec 内存限制: 128 MB Special Judge

题目描述
Snuke has read his own fortune for tomorrow, and learned that there are N scenarios that can happen, one of which will happen tomorrow with equal probability. The i-th scenario will cost him Ai yen (Japanese currency).

Following this, Snuke has decided to get insurance today. If he pays x yen to his insurance company, he will get compensation of min(Ai,2x) yen when Ai yen is lost. Here, he can choose any non-negative real number as x.

Snuke wants to minimize the expected value of the amount of money he loses, which is x+Ai−min(Ai,2x). Find the minimized value.

Constraints
1≤N≤105
1≤Ai≤109
All values in input are integers.
输入
Input is given from Standard Input in the following format:
N
A1 A2 ⋯ AN
输出
Print the answer. Your answer will be judged correct when its absolute or relative error is at most 10−6.
样例输入

【样例13
3 1 4
【样例210
866111664 178537096 844917655 218662351 383133839 231371336 353498483 865935868 472381277 579910117

样例输出

【样例11.83333333333333333333
【样例2362925658.10000000000000000000

提示
样例1解释:
The optimum choice is x=1.5. After paying 1.5 yen, one of the following three scenarios will happen with equal probability:

Scenario 1: Lose 3 yen and get compensation of min(3,2x)=3 yen. After all, Snuke loses x+A1−min(A1,2x)=1.5+3−3=1.5 yen.
Scenario 2: Lose 1 yen and get compensation of min(1,2x)=1 yen. After all, Snuke loses x+A2−min(A2,2x)=1.5+1−1=1.5 yen.
Scenario 3: Lose 4 yen and get compensation of min(4,2x)=3 yen. After all, Snuke loses x+A3−min(A3,2x)=1.5+4−3=2.5 yen.
Thus, the expected amount of money lost is (1.5+1.5+2.5)/3=1.833333⋯ yen.
思路
怎么说呢,没想到能过当时。题意就是,一个人想买保险,有n种情况,这n种情况发生概率相同,x可以任意取,每种情况的花费为x+Ai−min(Ai,2x),求最小的花费。
因为每种情况的花费一样,所以求出每种情况的x+Ai−min(Ai,2x),求和,在➗n,就可以了,那么x如何取呢,我们看样例,这里的x=1.5,所以2x=3,3刚好是样例一的中位数。另外我们也可以看 min(Ai,2x),我们最终求的是每种情况的平均值,然后选中位数的话就比较平均,好吧我自己也说不明白我选的中位数原因,但是结果是对的。

代码:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
int n;
ll a[100010],x;
double ans,y;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	sort(a+1,a+1+n);//排序
	//选中位数: 
	if(n%2) x=a[(n+1)/2];
	else x=a[n/2];
	y=(double)x/2;
	for(int i=1;i<=n;i++){//题目种的x+Ai-min(Ai,2x)相加 
		ans=ans+y+a[i]-min(a[i],x);//注意a[i]和x是同种类型 
	}
	ans=ans/n;
	printf("%.20lf",ans);//和样例输出保持一致 
	return 0;
} 
//1.833333333333333333333
//362925658.10000000000000000000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值