【HOJ】Elections(带概率的01背包)

 题面

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
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
Sample Output
0.4000
0.5000
0.5000
0.8000
0.7510

题目大意

题目抽象出来的模型就是:有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;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值