B. Andrey and Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Andrey needs one more problem to conduct a programming contest. He has n friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends — the probability that this friend will come up with a problem if Andrey asks him.

Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of Andrey's friends. The second line contains n real numbers pi (0.0 ≤ pi ≤ 1.0) — the probability that the i-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point.

Output

Print a single real number — the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10 - 9.

Examples
Input
4
0.1 0.2 0.3 0.8
Output
0.800000000000
Input
2
0.1 0.2
Output
0.260000000000
Note

In the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one.

In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1·0.8 + 0.9·0.2 = 0.26.

概率计算:P(某set) = 

令:  和  

现在考虑:

1.考虑某个集合,再加一个概率为Pi的朋友后能不能使总概率提高。

即: 由公式可知, 如果 S < 1,则delta > 0,则可以加入这个朋友。

2.如果要加一个朋友有两个候选的,其概率分别为Pi,Pj,(设Pi < Pj)那么加哪个会更优呢?

Δi - Δj = P·pi·(1 - S) - P·pj·(1 - S) = P·(1 - S)·(pi - pj) > 0.  如果 S < 1 那么 pi > pj 时可以使 Δi - Δj  > 0,即P较大的那个带来的价值更高,所以优先选P大的那个。虽然这个

只是局部更优,但是用反证法可以证明是全局最优的。

所以由上述分析,以概率从大到小的方式逐步加入元素,因为由1,很可能S<1,即可能加一个会更优,所以我们逐步加入,又由2,优先加最大的,所以从大到小加入能保证最优。

然后每加入一个都计算该种情况下的总概率,更新答案。

上面的题解来自http://www.cnblogs.com/whatbeg/p/3799957.html;

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<queue>
6 #include<string.h>
7 #include<map>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 double dp[106][106];
12 double ans[106];
13 bool cmp(double x,double y)
14 {
15 return x>y;
16 }
17 int main(void)
18 {
19 int n;
20 while(scanf("%d",&n)!=EOF)
21 {
22 int i,j;
23 for(i = 0; i < n; i++)
24 {
25 scanf("%lf",&ans[i]);
26 }
27 sort(ans,ans+n,cmp);
28 double maxx = ans[0];
29 double x = (1-ans[0]);
30 if(ans[0]!=1)
31 {
32 double y = ans[0]/(1-ans[0]);
33 for(i = 1; i < n; i++)
34 {
35 double d = -x*y+x*(1-ans[i])*(y+ans[i]/(1-ans[i]));
36 x*=(1-ans[i]);
37 y+=ans[i]/(1-ans[i]);
38 if(d <= 0)break;
39 else maxx = maxx+d;
40 }
41 }
42 printf("%.10f\n",maxx);
43 }
44 return 0;
45 }

Andrey and Problem的更多相关文章

  1. codeforces 442B B. Andrey and Problem(贪心)

    题目链接: B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. cf442B Andrey and Problem

    B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  3. codeforces#253 D - Andrey and Problem里的数学知识

    这道题是这种,给主人公一堆事件的成功概率,他仅仅想恰好成功一件. 于是,问题来了,他要选择哪些事件去做,才干使他的想法实现的概率最大. 我的第一个想法是枚举,枚举的话我想到用dfs,但是认为太麻烦. ...

  4. Codeforces Round #253 (Div. 1) B. Andrey and Problem

    B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. Codeforces 442B Andrey and Problem(贪婪)

    题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...

  6. Codeforces 442B. Andrey and Problem

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. 【codeforces 442B】 Andrey and Problem

    http://codeforces.com/problemset/problem/442/B (题目链接) 题意 n个人,每个人有p[i]的概率出一道题.问如何选择其中s个人使得这些人正好只出1道题的 ...

  8. [CF442B] Andrey and Problem (概率dp)

    题目链接:http://codeforces.com/problemset/problem/442/B 题目大意:有n个人,第i个人出一道题的概率是pi,现在选出一个子集,使得这些人恰好出一个题的概率 ...

  9. Codeforces Round #253 (Div. 2) D. Andrey and Problem

    关于证明可以参考题解http://codeforces.com/blog/entry/12739 就是将概率从大到小排序然后,然后从大到小计算概率 #include <iostream> ...

随机推荐

  1. javaSE中级篇2 — 工具类篇 — 更新完毕

    1.工具类(也叫常用类)-- 指的是别人已经写好了的,我们只需要拿来用就行了 官网网址:Overview (Java Platform SE 8 ) (oracle.com) ---- 但是这个是英文 ...

  2. adb命令对app进行测试

    1.何为adb adb android  debug  bridge ,sdk包中的工具,将Platform-tooks 和tools  两个路径配置到环境变量中 2.SDK下载链接:http://t ...

  3. 2.9 go mod 之本地仓库搭建

    wikihttps://github.com/golang/go/wiki/Modules#how-to-prepare-for-a-release参考https://blog.csdn.net/be ...

  4. 通过SSE(Server-Send Event)实现服务器主动向浏览器端推送消息

    一.SSE介绍 1.EventSource 对象 SSE 的客户端 API 部署在EventSource对象上.下面的代码可以检测浏览器是否支持 SSE. if ('EventSource' in w ...

  5. [MySQL实战-Mysql基础篇]-mysql的日志

    参考文章: https://www.cnblogs.com/f-ck-need-u/archive/2018/05/08/9010872.html https://dev.mysql.com/doc/ ...

  6. 【C/C++】贪心/算法笔记4.4/PAT B1020月饼/PAT B1023组内最小数

    简单贪心 所谓简单贪心,就是每步都取最优的一种方法. 月饼问题:有N种月饼,市场最大需求量D,给出每种月饼的库存量和总售价. 思路:从贵的往便宜的卖.如果当前的已经卖完了,就卖下一个.如果剩余D不足, ...

  7. C# 使用modbus 读取PLC 寄存器地址

    使用的组件Nmodbus 定义参数,全局变量: //创建modbus实体对象 private static ModbusFactory modbusFactory; private static IM ...

  8. linux重启后JDk环境变量配置失效最终解决方案

    最终解决方案:https://bbs.deepin.org/forum.php?mod=viewthread&tid=147762 其实这个修改可能也存在问题,如果有耐心的可以每次打开终端   ...

  9. Redis集群到集群迁移

    目录 一.物理导入 简介 实际操作 一.物理导入 简介 redis集群在存储数据时,是根据槽点进行存储.例如老集群A如下: 都在一台机器,实际可以在多台机器上. 主节点:7000(0-5460) 70 ...

  10. pipeline 共享库

    目录 一.简介 二.共享库扩展 共享库使用 共享库结构 pipeline模板 一些小问题 三.共享库例子 使用公共变量 使用共享库的src方法 使用共享库的vars方法 四.插件实现pipeline ...