Valentine's Day(概率dp)

Problem Description
Oipotato loves his girlfriend very much. Since Valentine’s Day is coming, he decides to buy some presents for her.

There are n presents in the shop, and Oipotato can choose to buy some of them. We know that his girlfriend will possibly feel extremely happy if she receives a present. Therefore, if Oipotato gives k presents to his girlfriend, she has k chances to feel extremely happy. However, Oipotato doesn’t want his girlfriend to feel extremely happy too many times for the gifts.

Formally, for each present i, it has a possibility of Pi to make Oipotato’s girlfriend feel extremely happy. Please help Oipotato decide what to buy and maximize the possibility that his girlfriend feels extremely happy for exactly one time.

Input
There are multiple test cases. The first line of the input contains an integer T (1≤T≤100), indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤10 000), indicating the number of possible presents.

The second line contains n decimals Pi (0≤Pi≤1) with exactly six digits after the decimal point, indicating the possibility that Oipotato’s girlfriend feels extremely happy when receiving present i.

It is guaranteed that the sum of n in all test cases does not exceed 450000.

Output
For each test case output one line, indicating the answer. Your answer will be considered correct if and only if the absolute error of your answer is less than 10−6.

Sample Input
2
3
0.100000 0.200000 0.900000
3
0.100000 0.300000 0.800000

Sample Output
0.900000000000
0.800000000000
一开始读错题了,有几件物品,每一件物品都有xi的概率让他女朋友开心。问让他女朋友开心一次的概率最大是多少。
首先我们排下序,由大到小,概率大的物品选它总是没错的。
之后我们应该怎么办?对于一件物品我们选了就要考虑它没有让女朋友开心的概率。我们就记录一下之前女朋友已经开心了的概率qi和女朋友没有开心的概率pi。那么到当前物品i了,这件物品对于开心概率的贡献是tmp=qi*(1.0-xi)+pi*xi。(如果之前开心了,那么这件物品就不开心。如果之前不开心,那么这件物品就开心)。如果tmp大于当前的值_max,那么就代表着这件物品可以选,那么_max=tmp,并且pi *=(1.0-xi)。一直记录最大值就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
double a[maxx];
double dp[maxx][2];
int n;

bool cmp(double x,double y)
{
	return x>y;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
		sort(a+1,a+1+n,cmp);
		double _max=a[1];
		double bx=1.0-a[1];
		for(int i=2;i<=n;i++)
		{
			double sum=_max*(1.0-a[i])+bx*a[i];
			if(sum>=_max)
			{
				_max=sum;
				bx*=(1.0-a[i]);
			}
		}
		printf("%.12lf\n",_max);
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值