codeforces 107 B. Basketball Team

http://codeforces.com/problemset/problem/107/B

题意:有m个部门,每个部分s[i]个人,HW在第h部门,现在要从这m个部门中挑选包括HW在内的n个人去参加比赛,问被挑选的人中有HW的队友(同部门的人)的概率是多少。如果m个部分的人数不够组成n人的球队,输出-1.

思路:考虑一般情况。至少有一个队友的情况较多,应该从反面考虑,即没有一个队友的情况。选完HW以后面临的状态是:事件总数为从total(m个部门的人员之和)-1个人中选n-1个的方案数,包含的事件数目为从a(a=total-s[h])中选n-1个人包含的方案数。 可以看出分母相同,可以约掉。

然后对于边界情况,首先判断total是否比n小。然后,如果a<n-1,表示除去HW所在的h部分之外的人不可能组成n-1个人,也就是一定要选择HW的队友,概率为1.

/* ***********************************************
Author :111qqz
Created Time :2016年02月03日 星期三 17时56分30秒
File Name :code/cf/problem/107B.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int M=1E3+7;
 7int n ,m,h;
 8int s[M];
 9int main()
10{
11	#ifndef  ONLINE_JUDGE 
12	freopen("code/in.txt","r",stdin);
13  #endif
	cin>>n>>m>>h;
	for ( int i = 1 ; i <= m ; i++) cin>>s[i];
 1	double total = 0;
 2	for ( int i =1 ; i <= m ; i++)
 3	{
 4	    total+=s[i];
 5	}
 6	if (total<n)
 7	{
 8	    puts("-1");
 9	    return 0;
10	}
11	total--;
12	n--;
13	s[h]--; //减去那个人。
14	double a = total - s[h];
15	double ans = 1.0;
16//	cout<<"total:"<<total<<" a:"<<a<<endl;
17	if (a<n) //s[h]队外的人无法满足剩余的要求。
18	{
19	    puts("1");
20	    return 0;
21	}
22	for ( int i = 1 ; i <= n ; i++)
23	{
24	    ans = ans *(a*1.0/total*1.0);
25//	    cout<<"ans:"<<ans<<endl;
26	    a--;
27	    total--;
28	}
29	printf("%.10f",1-ans);
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}