题面
Elections |
Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:524288KB |
Total submit users: 9, Accepted users: 4 |
Problem 14121 : No special judgement |
Problem description |
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. |
Input |
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. |
Output |
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). |
Sample Input |
|
Sample Output |
|
题目大意
题目抽象出来的模型就是:有n个物品,第i个物品有e[i]的重量,有p[i]的概率拿到第i个物品,问拿到物品重量是所有物品总重量一半以上的概率是多少~
总重量是不超过2000的奇数,最多1000个物品,一个物品要么取,要么不取,不允许取一部分~
思路
因为那么多的东西,要么取,要去不取,那么显然是0-1背包,概率是价值,重量是代价。
dp[i][j]保存考虑前i个物品,背包的价值为j的概率。设Pi为取第i件物品的概率,Wi为其重量。
dp[1][0] = 1 - P1
dp[1][W1] = P1
for i from 2 to n
for j from 0 to ΣWi
if Wi > j
dp[i][j] = dp[i-1][j] * (1 - Pi)
else
dp[i][j] = dp[i-1][j] * (1 - Pi) + dp[i-1][j-Wi] * Pi
AC代码
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,sum;
int w[1005];
double res;
double p[1005];
double dp[2005];
while(cin>>n && n)
{
sum = res = 0;
memset(dp,0,sizeof(dp));
for(int i=0; i<n; ++i)
{
cin>>p[i]>>w[i];
sum += w[i];
}
dp[0] = 1-p[0];
dp[w[0]] = p[0];
for(int i=1; i<n; ++i)
{
for(int j=sum; j>=0; --j)
{
if(w[i]>j) dp[j] = dp[j]*(1-p[i]);
else dp[j] = dp[j]*(1-p[i])+dp[j-w[i]]*p[i];
}
}
for(int i=(sum+1)/2; i<=sum; ++i)
res += dp[i];
cout<<fixed<<setprecision(4)<<res<<'\n';
}
return 0;
}