UVA 435 Block Voting

Source: Internet
Author: User

Original question:
Different types of electoral systems exist. In a block voting system the "Party of a" does not
Vote individually as they like, but instead they must collectively accept or reject a proposal. Although a party with many votes clearly have more power than a party with few votes, the votes of a small party can Nevert Heless is crucial when they is needed to obtain a majority. Consider for example the following Five-party system:

Coalition {A, B} has 7 + 4 = One votes, which is not A majority. When the party C joins coalition {A, b},
However, {a,b,c} becomes A winning coalition with 7+4+2 = 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, a party's power is determined by the number of minority coalitions, it can join and turn into a ( Winning) majority coalition. Note that the empty coalition are also a minority coalition and that a coalition only forms a majority when it had more tha n half of the total number of votes. In the example just given, a majority coalition must has at least votes.
In an ideal system, a party's power index is proportional to the number of members of the.
Your task is to write a program that, given an input as shown above, computes
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 ... Which equals the number of parties for that test case. This integer is followed by P positive integers, separated by spaces. Each of the these integers represents the number of members of the the the electoral system. The i-th number represents party number I. No electoral system has more than votes.
Output
For the 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 have power index i where I am 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 have Power Index 10
Party 2 have Power Index 2
Party 3 have Power Index 2
Party 4 have Power Index 6
Party 5 have Power Index 6

Party 1 have Power Index 18
Party 2 have Power Index 14
Party 3 have Power Index 14
Party 4 have Power Index 2
Party 5 have Power Index 2
Party 6 have Power Index 2

Party 1 have Power Index 3
Party 2 have Power Index 1
Party 3 have Power Index 1
English:

Give you the number of N, now to calculate the "exponent"of each number. Assuming that the number is a, all the sums are sum, then all the numbers in addition to a accident are combined, the group gets and adds the number a if it does not exceed SUM/2, then the exponent of a is + 1. In the case of a sample,
The 7 index is 10, except for 7, the remaining number is combined with a combination of 2^4=16, of which 10 combinations are satisfied and less than 13, plus 7 is greater than 13. Now let you find out the index of each number.

#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&GT;&G
            t; (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<< "have Power Index" <<ans[i]<<endl;
    cout<<endl;
} return 0;
 }

Answer:
is a very simple state compression dynamic programming topic, which is suitable for practiced hand. There are 20 numbers, so the state can be a power of 2^20. Set Dp[s] for the sum of the set S hours if the set S does not exceed half of the sum, and there is no number I in the set S. Then Dp[s∪i]=dp[s]+vote[i]
where vote[i] is the number of I.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.