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;
         
 
    }
}

猜你喜欢

转载自blog.csdn.net/Achanss/article/details/82469113
UPC