UVA - 435
题目介绍:
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:
party votes
A 7
B 4
C 2
D 6
E 6
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.
大意:
加上某个值,哪些集合的总票数可以超过一半。和全排列一样,构造所有集合,用2进制构造(0-(1<<n)-1),对每个值判断,开始时集合值<一半,加上当前值后超过一半,且原集合没有当前值,则对应的答案+1.
AC代码:
#include<iostream>
#include<cstring>
using namespace std;
const int Maxn=(1<<20)+5;
int num[Maxn],n;
int que[30],len;
void create(){
memset(num,0,sizeof(num));
len=(1<<n);
for(int i=0;i<len;i++){
int k=i,sum=0;
for(int j=0;j<n;j++){
if(k%2)sum+=que[j];
k=k>>1;
}
num[i]=sum;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int sum=0;
for(int i=0;i<n;i++){
scanf("%d",&que[i]);
sum+=que[i];
}
sum=sum/2+1;
create();
for(int i=0;i<n;i++){
printf("party %d has power index ",i+1);
int ans=0;
for(int j=0;j<len;j++){
if(num[j]>=sum)continue;
if(num[j]+que[i]>=sum&&(j>>i)%2==0)
ans++;
}
cout<<ans<<endl;
}
cout<<'\n';
}
return 0;
}