Valentine's Day

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0
Special Judge

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

题意:

有n种商品,每种商品有pi的概率让女朋友非常开心,现在问买那些商品才能让女朋友恰好非常开心一次的概率最大

思路:

记p为恰好让女朋友非常开心一次的概率,p=Σ(i=1->m)pi*π(j=1->m且i!=j)(1-pj),算了一些样例后发现如果pi>0.5,那么无论再买那些商品都会让总概率变小,
如果pi==0.5,则无论再买那些商品都会让总概率=0.5,所以如果存在pi>=0.5,则我们就取一个最大的大于等于0.5概率的商品,否则商品概率都小于0.5时,
队友通过打表发现将商品排序后,从最大的那个开始连续取,总概率会先递增再递减,且只有一个极大值,现在的问题是最坏的情况是要遍历所有商品,复杂度为O(n)
每次都要重新计算总概率,计算总概率的复杂度为O(n*n),所以这样的总复杂度为O(n*n*n),而题目中n最大为1e4,显然n的立方的复杂度是不能接受的,
遍历的O(n)没什么办法优化,但我们可以把计算总概率的O(n*n)的复杂度降为O(n),这样O(n*n)的复杂度是能支持1e4的数据的,所以现在的问题就是怎么优化计算
原始公式为p=Σ(i=1->m)pi*π(j=1->m且i!=j)(1-pj),可以将其转换为,p=Σ(i=1->m)pi*π(j=1->m)(1-pj)/(1-pi),因为π(j=1->m)(1-pj)与i无关,所以可以把他从累加中提出来,
转换为p=[Σ(i=1->m)pi/(1-pi)]*[π(j=1->m)(1-pj)],于是可以O(n)地计算Σ(i=1->m)pi/(1-pi)和π(j=1->m)(1-pj),最后将两者相乘即为总概率

 #include<bits/stdc++.h>
using namespace std;
const int amn=1e4+;
struct node{
double p,q;
}nm[amn];
double p[amn],q[amn],ans,cnt,maxn,aa,s,t;
bool cmp(node a,node b){
if(a.p==b.p)return a.q<b.q;
return a.p>b.p;
}
int main(){
int n,T,tp;
ios::sync_with_stdio();
scanf("%d",&T);
while(T--){
scanf("%d",&n);
tp=maxn=;
aa=;
for(int i=;i<=n;i++){
scanf("%lf",&nm[i].p);
maxn=max(maxn,nm[i].p);
nm[i].q=-nm[i].p;
}
if(maxn>=0.5){
ans=maxn;
}
else{
tp=maxn=;
sort(nm+,nm++n,cmp);
ans=nm[].p;
for(int i=;i<=n;i++){
s=,t=;
for(int j=;j<=i;j++)s+=nm[j].p/nm[j].q;
for(int j=;j<=i;j++)t*=nm[j].q;
if(s*t>ans)
ans=s*t;
else break;
}
}
printf("%.12lf\n",ans);
}
}
/**
有n种商品,每种商品有pi的概率让女朋友非常开心,现在问买那些商品才能让女朋友恰好非常开心一次的概率最大
记p为恰好让女朋友非常开心一次的概率,p=Σ(i=1->m)pi*π(j=1->m且i!=j)(1-pj),算了一些样例后发现如果pi>0.5,那么无论再买那些商品都会让总概率变小,
如果pi==0.5,则无论再买那些商品都会让总概率=0.5,所以如果存在pi>=0.5,则我们就取一个最大的大于等于0.5概率的商品,否则商品概率都小于0.5时,
队友通过打表发现将商品排序后,从最大的那个开始连续取,总概率会先递增再递减,且只有一个极大值,现在的问题是最坏的情况是要遍历所有商品,复杂度为O(n)
每次都要重新计算总概率,计算总概率的复杂度为O(n*n),所以这样的总复杂度为O(n*n*n),而题目中n最大为1e4,显然n的立方的复杂度是不能接受的,
遍历的O(n)没什么办法优化,但我们可以把计算总概率的O(n*n)的复杂度降为O(n),这样O(n*n)的复杂度是能支持1e4的数据的,所以现在的问题就是怎么优化计算
原始公式为p=Σ(i=1->m)pi*π(j=1->m且i!=j)(1-pj),可以将其转换为,p=Σ(i=1->m)pi*π(j=1->m)(1-pj)/(1-pi),因为π(j=1->m)(1-pj)与i无关,所以可以把他从累加中提出来,
转换为p=[Σ(i=1->m)pi/(1-pi)]*[π(j=1->m)(1-pj)],于是可以O(n)地计算Σ(i=1->m)pi/(1-pi)和π(j=1->m)(1-pj),最后将两者相乘即为总概率
**/

[概率] HDU 2019 Multi-University Training Contest 10 - Valentine's Day的更多相关文章

  1. [二分,multiset] 2019 Multi-University Training Contest 10 Welcome Party

    Welcome Party Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)T ...

  2. hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)

    CRB and Tree                                                             Time Limit: 8000/4000 MS (J ...

  3. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  4. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  5. 2016 Multi-University Training Contest 10

    solved 7/11 2016 Multi-University Training Contest 10 题解链接 分类讨论 1001 Median(BH) 题意: 有长度为n排好序的序列,给两段子 ...

  6. 2015 Multi-University Training Contest 10(9/11)

    2015 Multi-University Training Contest 10 5406 CRB and Apple 1.排序之后费用流 spfa用stack才能过 //#pragma GCC o ...

  7. hdu 4946 2014 Multi-University Training Contest 8

    Area of Mushroom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. hdu 4864 Task---2014 Multi-University Training Contest 1

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4864 Task Time Limit: 4000/2000 MS (Java/Others)    M ...

  9. hdu 4937 2014 Multi-University Training Contest 7 1003

    Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) T ...

随机推荐

  1. iPhone 8价格狂跌:是国产手机的胜利,还是苹果的黄昏

    ​ 8价格狂跌:是国产手机的胜利,还是苹果的黄昏" title="iPhone 8价格狂跌:是国产手机的胜利,还是苹果的黄昏"> ​   其实呢,这年头发布新款智能 ...

  2. X因素 开启它就能让你成为超级明星

    开启它就能让你成为超级明星" title="X因素 开启它就能让你成为超级明星"> "只要努力就能成为明星!"记得电影学院的不少老师都这样告诫学 ...

  3. linux入门系列16--文件共享之Samba和NFS

    前一篇文章"linux入门系列15--文件传输之vsftp服务"讲解了文件传输,本篇继续讲解文件共享相关知识. 文件共享在生活和工作中非常常见,比如同一团队中不同成员需要共同维护同 ...

  4. python 读取 execl 文件 之 xlrd 模块

    1. 安装 xlrd模块 pip install xlrd 2. 读取文件内容 #!/usr/bin/env python3 import xlrd name = r"E:\excel\yo ...

  5. 6,HDFS HA

    目录 HDFS HA 一.HA(High Availability)的使用原因 二.HA的同步 三.HA的自动容灾 HDFS HA 一.HA(High Availability)的使用原因 1.1 在 ...

  6. 从头认识js-基本概念(关键字,保留字,数据类型)

    语法 ECMAScript的语法大量借鉴了C及其他类C语言(如Java和Perl)的语法.因此,熟悉这些语言的开发人员在接受ECMSAScript更加宽松的语法时,一定会有一种轻松自在的感觉. 区分大 ...

  7. idea导入 spring framework项目

    准备的环境:gradle,idea 注意:gradle版本不一致会报各种错误,那么怎么查找依赖的版本呢? 首先在git上把spring framework项目拉取下来, 步骤一:复制URL路径 步骤二 ...

  8. idea 报Сannot Run Git runnerw.exe: AttachConsole failed with error 6

    报错:Сannot Run Git runnerw.exe: AttachConsole failed with error 6 解决方案:指向Git 的git.exe文件所在的安装目录,配置上就可以 ...

  9. c/s用户与服务器之间的传输

    ####第一页 <%@ page language="java" contentType="text/html; charset=UTF-8" pageE ...

  10. 解决vue2.0下IE浏览器白屏问题

    公司新开发的项目需要兼容到IE9+ 就在index.html页面加入 <meta http-equiv="X-UA-Compatible" content="IE= ...