Fat brother and Maze are playing a kind of special (hentai) game with some cards. There are N*2 cards in this game for the players to use, this is the card deck. The game goes for N rounds and in each round, Fat Brother would draw one card from the remaining card deck randomly and then Maze would draw one card from the remaining card deck randomly. Then they compare for the integers which is written on these two cards. The player with the higher number wins this round and score 1 victory point. And then these two cards are removed from the game such that they would not appear in the later rounds. As we all know, Fat Brother is very clever, so he would like to know the expect number of the victory points he could get if he knows all the integers written in these cards.
Note that the integers written on these N*2 cards are pair wise distinct. At the beginning of this game, each player has 0 victory point.
The first line of the data is an integer T (1 <= T <= 100), which is the number of the text cases.
Then T cases follow, each case contains an integer N (1 <= N<=10000) which described before. Then follow one line with N*2 integers, which indicate the integers in these cards.
All the integers are positive and no morethan 10000000.
For each case, output the case number first, and then output the expect number of the victory points Fat Brother could get in this game. The answer should be rounded to 2 digits after the decimal point.
2 1 1 2 2 1 2 3 4Sample Output
Case 1: 0.50 Case 2: 1.00
题意:输入t,下面有t组数据,每组数据第一行一个数n,表示有n轮比赛,下面有 2*n 的数,从第一轮比赛开始,每一轮两人一次从剩下的牌中各抽取一张,比较点数,谁大谁赢,获得一分,求A最后获得分数的期数;
下面给你期望的定义:
在概率论和统计学中,数学期望(mean)(或均值,亦简称期望)是试验中每次可能结果的概率乘以其结果的总和,是最基本的数学特征之一。它反映随机变量平均取值的大小。
1. 在17世纪,有一个赌徒向法国著名数学家帕斯卡挑战,给他出了一道题目:甲乙两个人赌博,他们两人获胜的机率相等,比赛规则是先胜三局者为赢家,赢家可以获得100法郎的奖励。当比赛进行到第四局的时候,甲胜了两局,乙胜了一局,这时由于某些原因中止了比赛,那么如何分配这100法郎才比较公平?
求 n轮之后 获得分数的期望 分数的期望等于 赢的概率 * 本轮获得的分数
给出结论:两个从n的数中依次选出一个数,比较大小,两人赢得概率相等都是 0.5;
推:
当n = 2时, 1,2 A的赢的为0.5;
当n = 3时 1,2,3 A选每个数的概率都是相等的 都是1/3, 选1 赢的概率为0,选2赢的概率为 1/2 选3赢得概率为 1,综上述三种情况 1/3*0+1/3*1/2+1/3*1 = 0.5;
当 n = 4时,A赢的概率也是0.5;
综上知:每轮赢得概率都是 0,5;
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int i,j;
int t,n;
scanf("%d",&t);
j = 1;
while(t--)
{
scanf("%d",&n);
int x;
for(i = 0;i<2*n;i++)
scanf("%d",&x);
printf("Case %d: %.2f\n",j++,(double)n*0.5);
}
return 0;
}
Card Game (Second Edition) FZU - 2265
Fat brother and Maze are playing a kind of special (hentai) game with some cards. In this game, every player gets N cards at first and these are their own card deck. The game goes for N rounds and in each round, Fat Brother and Maze would draw one card from their own remaining card deck randomly and compare for the integer which is written on the cards. The player with the higher number wins this round and score 1 victory point. And then these two cards are removed from the game such that they would not appear in the later rounds. As we all know, Fat Brother is very clever, so he would like to know the expect number of the victory points he could get if he knows all the integers written in these cards.
Note that the integers written on these N*2 cards are pair wise distinct. At the beginning of this game, each player has 0 victory point.
The first line of the data is an integer T (1 <= T <= 100), which is the number of the text cases.
Then T cases follow, each case contains an integer N (1 <=N<=10000) which described before. Then follow two lines with N integers each. The first N integers indicate Fat Brother’s cards and the second N integers indicate Maze’s cards.
All the integers are positive and no more than 10000000.
For each case, output the case number first, and then output the expect number of victory points Fat Brother would gets in this game. The answer should be rounded to 2 digits after the decimal point.
2 1 1 2 2 1 3 2 4Sample Output
Case 1: 0.00 Case 2: 0.50
题意:t组数据,n表示有 A有n张牌,B有n张牌,每一轮中,A从A中的剩余牌中 取出一张,B从B中剩余牌中取出一张,两张牌比较大小,大的获胜的1分,求 n轮后,A获胜的数学期望;
思路:第一轮 A赢的概率,容易求,但是以后下面那n-1轮 A赢得概率怎么求呢? 大家可以想想, A抽A中的数时,不管怎么在第几轮抽到,被抽到的概率都是1/n (比如在第二轮抽到,那么是在第一轮没有抽的前提下抽到的,在第 i 轮被抽到,是在前i-1轮没有抽到的前提下抽到的,你算一算就知道为啥概率都是1/n了),B也一样,所以 后n-1 轮赢的概率和 第一轮赢的概率是相同的;
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 10010
int a[Max],b[Max];
int main()
{
int i,j,t,tt=1;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(i = 0;i<n;i++)
scanf("%d",&a[i]);
for(j = 0;j<n;j++)
scanf("%d",&b[j]);
sort(a,a+n);
sort(b,b+n);
double ans = 0;
for(i = 0;i<n;i++)
{
ans += lower_bound(b,b+n,a[i]) - b;
}
printf("Case %d: %.2f\n",tt++,ans/n);
}
return 0;
}