UPC——7713: Elections(概率DP)

7713: Elections

时间限制: 1 Sec  内存限制: 128 MB
提交: 82  解决: 38
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Jenabkhan who has become billionaire from his Laboo bussiness, is now running for president. His country uses a strange mechanism, so-called electoral college, to select the president. There are several states in the country, and each state counts the votes independently. Depending on the population, each state has some members in the electoral college, and all of those members will vote the candidate with the majority of votes in their state. In the case of ties, each state has some tie-break rule to announce the clear winner. The president will be the candidate who receives more than half of votes in the electoral college.
Given the chance of Jenabkhan to win in each state, compute his winning probability in the electoral college.

 

输入

The input consists of several test cases. Each test case starts with a line containing a single integer n denoting the number of states (1 ⩽ n ⩽ 1000). Each of the next n lines contains a real value pi with at most 4 digits after the decimal point (0 ⩽ pi ⩽ 1) and a positive integer ei , specifying the winning probability of Jenabkhan in the i-th state and the number of electoral votes associated with that state, respectively. The total number of members in the electoral college is an odd number and is no more than 2000. The input terminates with a line containing 0 which should not be processed.

 

输出

For each test case, output in a single line containing the winning probability of Jenabkhan, rounded to exactly four digits
after the decimal point (e.g., 0.3000 is correct while 0.3 is not).

 

样例输入

1
0.4 1
3
0.5 1
0.5 2
0.5 10
3
0.5 1
0.5 2
0.5 2
2
0.2 1
0.8 10
2
0.25 1
0.751 10
0

 

样例输出

0.4000
0.5000
0.5000
0.8000
0.7510

 

来源/分类

Tehran2016 

 

 

 

#include<bits/stdc++.h>
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define rep(i,j,k) for(int i=j;i<k;i++)
#define per(i,j,k) for(int i=j;i<=k;i++) 
using namespace std;
const int maxn = 1001;
int e[maxn];
double p[maxn],dp[maxn][maxn*2];  
int main(void)
{
    IO 
    int n;
    while(cin>>n&&n)
    {
        memset(dp,0,sizeof(dp));
        int tot=0;
        per(i,1,n) {
            cin>>p[i]>>e[i];
            tot+=e[i];
        }
 
        dp[0][0] =1.0;
        per(i,1,n)
            per(j,0,tot)     
        //   for(int j=tot;j>=0;j--)
        //      dp[j] = j>=e[i]? dp[j]*(1-p[i]) + dp[j-e[i]]*p[i]: dp[j]*(1-p[i]);
                  
                 
                    if(dp[i-1][j]) 
                    {
                      dp[i][j] += dp[i-1][j]*(1-p[i]);    //竞选,失败不增加人数,概率 + 上次这些人数的概率*失败概率   
                      dp[i][j+e[i]] += dp[i-1][j]*p[i];   //竞选,成功增加对应人数,概率 + 上次这些人数的概率*成功概率   
                    }
                 
        double ans=0.0;
        rep(i,(tot+1)>>1,tot+1) ans += dp[n][i];  
        cout<<setiosflags(ios::fixed)<<setprecision(4)<<ans<<endl;
         
 
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值