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. Windows下python3登陆和操作linux服务器

    一.环境准备 python3远程连接需要用到pycrytodome和paramiko库,其中后者依赖前者,所以按照顺序来安装 1. 安装pycrytodome 1 pip install pycryt ...

  2. 乱世兄弟(豹老头 X 天捣臼)

    论CP之冷冷冷 只为白凡扫剧- 片源: 乱世兄弟 BGM:兄弟 人物角色: 豹老头 - 白凡 天捣臼 大专栏  乱世兄弟(豹老头 X 天捣臼)- 姚鲁 B站:豹老头 X 天捣臼-MV<乱世兄弟& ...

  3. Git pull 卡在Unpacking objects

    今天在拉取远程仓库的时候在Unpacking objects阶段 进度条卡住,不知道什么原因. 翻取相关资料搜索后得知:在拉取大型二进制对象(如Adobe Illustrator文件等)可能会使整个拉 ...

  4. Codeforces Round #612 (Div. 2)C. Garland

    第四次写题解,请多指教! http://codeforces.com/contest/1287/problem/C题目链接 题目大意是有一个数字串挂有1-n n个数字,现在上面缺失了一些数字,让你找出 ...

  5. linux lsof常用方法

    lsof简介 lsof(list open files)是一个列出当前系统打开文件的工具,在linux环境下,任何事物都是以文件形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.系统 ...

  6. 利用短信通知的方式在Tasker中实现收到Android手机短信自动转发到邮箱

    利用短信的通知实现短信内容转发到微信 code[class*="language-"] { padding: .1em; border-radius: .3em; white-sp ...

  7. 不一样的ZTree,权限树.js插件

    每一个有趣的创新,都源于苦逼的生活. 在最近的工作中,遇到一个做权限管理筛选的需求.简单总结需求:1展示一个组织中的组织结构2通过点击组织结构中的任意一个节点可以向上向下查询对应的组织结构 如果你不想 ...

  8. 基础JavaScript练习(三)总结

    任务目的 实践JavaScript数组.字符串相关操作 任务描述 基于任务四进行升级 将新元素输入框从input改为textarea 允许一次批量输入多个内容,格式可以为数字.中文.英文等,可以通过用 ...

  9. python3 flask shell

    python shell来操作flask flask shell 报错: from flask_bootstrap import BootstrapImportError: No module nam ...

  10. Rust入坑指南:智能指针

    在了解了Rust中的所有权.所有权借用.生命周期这些概念后,相信各位坑友对Rust已经有了比较深刻的认识了,今天又是一个连环坑,我们一起来把智能指针刨出来,一探究竟. 智能指针是Rust中一种特殊的数 ...