当前位置:网站首页>CCF CSP 202109-4 collect cards
CCF CSP 202109-4 collect cards
2022-06-10 22:36:00 【Brienzz】
Define the storage structure
Structure : Card draw status , The layer number ( Extraction times ), Probability product
Define a queue , Simulate the hierarchical traversal process of the tree
Input
Total number of cards read n And coins k Number of cards changed
Read in the card to get the probability
Use queues to simulate n Hierarchy traversal of fork tree
Every outgoing node
- Judge whether the card drawing is completed
- If it's done , Calculate the probability product , And sum it up
- If not done , Keep drawing , Generate child nodes , Calculate the probability product , And put the child nodes in the queue
The queue is empty ( End of traversal ), Output results
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
double p[16];// Number of cards n Less than or equal to 16, The corresponding probability number is less than or equal to 16
int n,k;
struct node{
int state;// Card draw status [ It's essentially a vector , Someone is 1, Indicates the card drawn to this position ]
int level;// The layer number ( Extraction times )
double p;// Probability product
};
int main(){
scanf("%d%d",&n,&k);// Total number of cards read n And coins k Number of cards changed
for(int i=0;i<n;i++) scanf("%lf",p+i);// For the first i The probability of a card is p[i-1]
double ans = 0;// Record the expected number of cards drawn
node nodecur,nodechild;
queue<node> q;// Define a queue , Simulate the hierarchical traversal process of the tree
// Initial root node
nodecur.state = 0;
nodecur.level = 0;
nodecur.p = 1;
// Start n Hierarchy traversal of fork tree
q.push(nodecur);
while(!q.empty()){
nodecur = q.front();
q.pop();
int cnt = 0;// Record the number of different types of cards drawn
for(int i=0;i<n;i++)
if((nodecur.state>>i)&1) cnt++;
//nodecur.level-cnt Is the number of coins
if(cnt+(nodecur.level-cnt)/k>=n){
ans = ans += nodecur.p * nodecur.level; // Cumulative probability product * The layer number
continue;// here , There is no need to generate child nodes , Just skip , The reason is that the conditions have been met , There is no need to draw
}
for(int i=0;i<n;i++){// Generate child nodes
nodechild.p = nodecur.p*p[i];// Calculate the probability product of child nodes
nodechild.state = nodecur.state|(1<<(n-i-1));
nodechild.level = nodecur.level + 1;
q.push(nodechild);// Queue child nodes
}
}
printf("%.10lf\n",ans);// Title Example 2 It is reserved to the... After the decimal point 10 position
return 0;
}边栏推荐
- 【Xpath】使用following-sibling获取后面的同级节点
- C语言内功修炼【整型在内存中的存储】
- 在D天内送达包裹的能力[抽象类二分--抽象判定方式]
- GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
- leetcode 130. Surrounded Regions 被围绕的区域(中等)
- datagrip 报错 “The specified database user/password combination is rejected...”的解决方法
- Web3生态去中心化金融平台——Sealem Finance
- Whether there are duplicate elements in the array
- leetcode:333. 最大 BST 子树
- Different ways to create four indexes in MySQL
猜你喜欢

Visio 转为高质量PDF

【TcaplusDB知识库】TcaplusDB巡检统计介绍

GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.

小微企业如何低成本搭建微官网

TcaplusDB君 · 行业新闻汇编(三)

Matlab - Implementation of evolutionary game theory

1.Tornado简介&&本专栏搭建tornado项目简介

How to do well in the top-level design of informatization in the process of informatization upgrading of traditional enterprises

【TcaplusDB知识库】TcaplusDB查看线上运行情况介绍

【Debug】could not find ref wiht poc XXX解决
随机推荐
[XPath] use following sibling to obtain the following peer nodes
Several Apache related security vulnerability fixes
SQL Server查询区分大小写
Array plus one
TcaplusDB君 · 行业新闻汇编(一)
mathtype7.x的基本使用
Mmcv Config class introduction
[tcapulusdb knowledge base] Introduction to tcapulusdb push configuration
String inversion
Diablo immortality database station address Diablo immortality database website
【小程序】Vant滑动单元格添加点击其他位置自动关闭的功能
Add, delete, query and modify [MySQL] table data (DML)
[MySQL] summary of common data types
dc_labs--lab1的学习与总结
How to stimulate the vitality and driving force of cultural innovation
README
leetcode 130. Surrounded regions (medium)
【TcaplusDB知识库】TcaplusDB进程启动介绍
torch_ geometric
[tcapulusdb knowledge base] Introduction to the machine where the tcapulusdb viewing process is located