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

这道题是这样的,给主人公一堆事件的成功概率,他只想恰好成功一件。

于是,问题来了,他要选择哪些事件去做,才能使他的想法实现的概率最大。

我的第一个想法是枚举,枚举的话我想到用dfs,可是觉得太麻烦。

于是想是不是有什么规律,于是推导了一下,推了一个出来,写成代码提交之后发现是错的。

最后就没办法了,剩下的时间不够写dfs,于是就放弃了。

今天看thnkndblv的代码,代码很短,于是就想肯定是有什么数学规律,于是看了一下,

果然如此。

是这样的,还是枚举,当然是有技巧的,看我娓娓道来。

枚举的话,假设总共有n件事件,

从中选择1件事情去做,实现的概率最大是多少,

选择2件事情去做,实现的概率最大是多少,以此类推到选择n件事情去做。

于是就会得到n个结果,里面最大的那个就是实现主人公想法的最大概率。

这样的话要从n件事件中任意选出1件事情然后比概率,任意选出2件事情然后比概率……任意选出n件事情然后比概率

还是要搜索。

但是这里就有数学知识了,可以不用搜索,做法是这样的:

将n个事件成功的概率从大到小排序,

然后,

只选择前1件事去做成功的概率就是“从中选择1件事情去做,最大的实现概率”

只选择前2件事去做成功的概率就是“从中选择2件事情去做,最大的实现概率”

以此类推。

有了这个,这道题就很简单了,至于为什么这样做可以,无法证明,如果有人证明的话,欢迎留言讨论,附上原题以及地址和我后来AC的代码如下:

http://codeforces.com/contest/443/problem/D

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

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(double a,double b)
{
	return a>b;
}
int main()
{
	int n,i,j,k;
	double a[110],x,sum,MAX;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%lf",&a[i]);
	sort(a,a+n,cmp);
	MAX=0;
	for(i=1;i<=n;i++)
	{
		sum=0;
		for(j=0;j<i;j++)
		{
			x=a[j];
			for(k=0;k<i;k++)
			{
				if(j!=k)
					x*=1-a[k];
			}
			sum+=x;
		}
		MAX=max(sum,MAX);
	}
	printf("%.12lf\n",MAX);
}

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

时间: 2024-12-26 09:14:34

codeforces#253 D - Andrey and Problem里的数学知识的相关文章

【Codeforces 442B】Andrey and Problem

[链接] 我是链接,点我呀:) [题意] n个朋友 第i个朋友帮你的概率是pi 现在问你恰好有一个朋友帮你的概率最大是多少 前提是你可以选择只问其中的某些朋友不用全问. [题解] 主要思路是逆向思维,转换成一个一个地加上去 然后看看概率的改变值在何时为正数,显然只有为正数的时候才能加 然后概率大的和概率小的,哪一个加上去会比较优一点,也比较一下. 但是因为加上去之后S也会变化,所以只知道概率大的加上去比较优还不够. 可以再看看下面那个反证法. 它指出为何一定是选择末尾最大的若干个(也即优先选择p

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c). Over time the stars twinkle. At moment 0 the i-th

Codeforces 776D:The Door Problem(DFS染色)

http://codeforces.com/problemset/problem/776/D 题意:有n个门,m个开关,每个门有一个当前的状态(0表示关闭,1表示打开),每个开关控制k个门,但是每个门确切的受两个开关控制,如果一个开关打开,那么原来关闭的门会打开,打开的门关闭,问是否存在一个情况使得所有的门打开. 思路:类似于01染色,把开关当成点,门当前的状态当成边权建图.初始先假设一个门的状态(初始假设为0和假设为1都是一样的,举几个例子就发现了),然后因为每个门受两个开关控制,所以可以推出

[CF442B] Andrey and Problem (概率dp)

题目链接:http://codeforces.com/problemset/problem/442/B 题目大意:有n个人,第i个人出一道题的概率是pi,现在选出一个子集,使得这些人恰好出一个题的概率最大.问最大概率. 可以仿照背包问题来做,即每个人可问可不问. f[i][j]代表从前i个人里问j个人所获得1个题的最大概率. f[i][j] = max{ f[i-1][j], f[i-1][j-1]*(1-p[i])+z[i-1][j-1]*p[i] }; z[i][j]为从前i个人里问j个人所

Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to h

Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing

http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开始到倒数第二项,当前的a[i]=(a[i-1],a[i],a[i+1])三项排序后的中间项,比如连续3项为 1 0 1,那么中间的就变为1,然后题目让你输出达到稳定状态时所需的最小步数,不能的话输出-1. 无论给你啥数列,都能达到稳态.所以不可能输出-1: 还有一开始就稳定不变,或经过几次变换而稳定

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)

Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeForces. Original ID: 385C 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None Recently, the bear started studyi