题目链接:

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

题意:

给出n个人想出问题的概率,现在问选哪些人才能使正好想出一个问题的概率最大;

思路:

贪心,ans表示当前选一个得到的最大的概率,pre表示什么都不选的概率.然后判断当前这个如果加进来来会不会使ans变大,否则就不选;

AC代码:
#include <bits/stdc++.h>
using namespace std; int n;
double a[110];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
sort(a+1,a+n+1);
double ans=a[n],pre=1-a[n];
for(int i=n-1;i>0;i--)
{
if(ans*(1-a[i])+a[i]*pre>=ans)
{
ans=ans*(1-a[i])+a[i]*pre;
pre=pre*(1-a[i]);
}
}
printf("%.12f",ans);
return 0;
}

  

codeforces 442B B. Andrey and Problem(贪心)的更多相关文章

  1. 【codeforces 442B】 Andrey and Problem

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

  2. 【Codeforces 442B】Andrey and Problem

    [链接] 我是链接,点我呀:) [题意] n个朋友 第i个朋友帮你的概率是pi 现在问你恰好有一个朋友帮你的概率最大是多少 前提是你可以选择只问其中的某些朋友不用全问. [题解] 主要思路是逆向思维, ...

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

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

  4. Codeforces 798C - Mike and gcd problem(贪心+数论)

    题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...

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

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

  6. 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 ...

  7. cf442B Andrey and Problem

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

  8. Codeforces 442B

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

  9. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

随机推荐

  1. No.003:Longest Substring Without Repeating Characters

    问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...

  2. mysql下一个版本应该且实现并不复杂增加的常用功能

    1.innodb的auto_increment应该在参考oracle的实现方式,定期持久化: 我们目前遇到个问题,出于性能考虑,我们每天会把当天处理完成的数据归到另外一张历史表,并清空,同时有可能会重 ...

  3. mysql bin-log和innodb_log的关系

    首先,二进制日志会记录所有与MySQL数据库有关的日志记录,包括InnoDB.MyISAM.Heap(memory除外)等其他存储引擎的日志.而InnoDB存储引擎的重做日志记录有关该引擎本身的事务日 ...

  4. OSX cordova+Ionic的安装配置

    0.安装前确定你的系统安装了node和xcode 1.cordova (1)安装cordova npm config set registry http://registry.cnpmjs.org s ...

  5. 解决SQL Server 2008 64位系统无法导入Access/Excel的问题 2012/08/01

    操作系统Windows Server 2008 X64,数据库SQL Server 2008 X64,Office 2007(好像只有32位),在存储过程执行OpenDatasource导入Acces ...

  6. js阻塞

    阻塞指的是暂停一个线程的执行以等待某个条件发生(如某资源就绪),JS单线程避免阻塞方法: 1.sleep()方法:sleep()允许指定以毫秒为单位的一段时间作为参数,使得线程在指定的时间内进入阻塞状 ...

  7. 我在用的mac软件(3)-效率篇

    距离上篇博客竟然一晃就2个月过去了……最近确实太忙了,但一忙其实就容易乱,反而积累就少. 今天继续介绍下我在用的mac软件.标题为效率篇,其实没有严格的限定,就是杂乱的介绍我在用的感觉能提升效率的工具 ...

  8. IOS中(Xcode) DEBUG模式(RELEASE模式)控制NSLog输出,NSLog两种不同情况的输出方式

    [新年新气象,2016/01/04] 俺们在开发IOS程序过程中,经常需要用到NSLog输出一些信息,甚至有的开发过程,必须在控制台查看输出,有经验的程序员通过控制台输出就能知道整个数据交互的一个流程 ...

  9. ReactiveCocoa入门教程:第二部分

    翻译自:http://www.raywenderlich.com/62796/reactivecocoa-tutorial-pt2 原文链接: ReactiveCocoa 是一个框架,它允许你在你的i ...

  10. JAVA基础学习day13--String、StringBuilder与StringBuffer与包装类

    一.String 1.1.String String 类是final修饰的,是顶级类,不可被继承 String 类代表字符串.Java 程序中的所有字符串字面值(如 "abc" ) ...