# uva 435 Block Voting

Different types of electoral systems exist. In a block voting system the members of a party do not
vote individually as they like, but instead they must collectively accept or reject a proposal. Although a party with many votes clearly has more power than a party with few votes, the votes of a small party can nevertheless be crucial when they are needed to obtain a majority. Consider for example the following five-party system:

Coalition {A,B} has 7 + 4 = 11 votes, which is not a majority. When party C joins coalition {A,B},
however, {A,B,C} becomes a winning coalition with 7+4+2 = 13 votes. So even though C is a small
party, it can play an important role.As a measure of a party’s power in a block voting system, John F. Banzhaf III proposed to use the power index. The key idea is that a party’s power is determined by the number of minority coalitions that it can join and turn into a (winning) majority coalition. Note that the empty coalition is also a minority coalition and that a coalition only forms a majority when it has more than half of the total number of votes. In the example just given, a majority coalition must have at least 13 votes.
In an ideal system, a party’s power index is proportional to the number of members of that party.
Your task is to write a program that, given an input as shown above, computes for each party its
power index.
Input
The first line contains a single integer which equals the number of test cases that follow. Each of the following lines contains one test case.The first number on a line contains an integer P in [1 … 20] which equals the number of parties for that test case. This integer is followed by P positive integers, separated by spaces. Each of these integers represents the number of members of a party in the electoral system. The i-th number represents party number i. No electoral system has more than 1000 votes.
Output
For each test case, you must generate P lines of output, followed by one empty line. P is the number of parties for the test case in question. The i-th line (i in [1…P]) contains the sentence:
party i has power index I where I is the power index of party i.
Sample Input

3
5 7 4 2 6 6
6 12 9 7 3 1 1
3 2 1 1
Sample Output

party 1 has power index 10
party 2 has power index 2
party 3 has power index 2
party 4 has power index 6
party 5 has power index 6

party 1 has power index 18
party 2 has power index 14
party 3 has power index 14
party 4 has power index 2
party 5 has power index 2
party 6 has power index 2

party 1 has power index 3
party 2 has power index 1
party 3 has power index 1

7的指数是10，除了7以外，剩下的数进行组合有2^4=16种组合，其中有10种组合满足和小于13且加上7大于13。现在让你找出每个数的指数。

#include <bits/stdc++.h>
using namespace std;
int dp[1<<20+1];
int vote[21],ans[21];
int main()
{
ios::sync_with_stdio(false);
int n,t,tot,half;
cin>>t;
while(t--)
{
cin>>n;
tot=0;
memset(ans,0,sizeof(ans));
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
cin>>vote[i];
tot+=vote[i];
}
half=(tot)/2;
for(int s=1;s<(1<<n);s++)
{
for(int i=1;i<=n;i++)
{
if(s>>(i-1)&1)
dp[s]=dp[s&~(1<<(i-1))]+vote[i];
}
}
for(int s=0;s<(1<<n);s++)
{
for(int i=1;i<=n;i++)
{
if(!(s>>(i-1)&1))
{
if(dp[s]+vote[i]>half&&dp[s]<=half)
ans[i]++;
}
}
}
for(int i=1;i<=n;i++)
cout<<"party "<<i<<" has power index "<<ans[i]<<endl;
cout<<endl;
}
return 0;
}


#### uva 101 木块问题 The Blocks Problem

2016-10-17 21:27:56

#### 11.2 RAC环境OCR和Votedisk损坏丢失恢复

2015-02-15 13:59:17

#### 骁龙435和骁龙625处理器哪个好？

2018-01-22 14:51:25

#### Block Voting --解题报告

2009-07-01 13:05:00

#### UVA101 The Blocks Problem

2014-07-21 14:42:10

#### CRS-1714 Unable to discover any voting files

2016-12-11 21:29:21

#### drupal voting api

2010年05月26日 36KB 下载

#### Uva 101 the block problem 木块问题（算法竞赛经典入门）STL vector

2015-05-06 17:04:49

#### 高通骁龙435（MSM8940）处理器达到什么水平

2018-01-22 15:02:01

#### Majority Element解决:Moore's Voting Algorithm

2016-06-01 10:47:50