UVA435 Block Voting序列处理

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA435 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:

partyvotes
A7
B4
C2
D6
E6

    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 numberof 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

问题链接UVA435 Block Voting
问题简述:(略)
问题分析:序列处理问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA435 Block Voting */

#include <bits/stdc++.h>

using namespace std;

const int P = 20;
int a[P], dp[1000];

int main()
{
    int t, p;
    scanf("%d", &t);
    while (t--) {
        int sum = 0, half;
        scanf("%d", &p);
        for (int i = 0; i < p; i++) {
            scanf("%d", &a[i]);
            sum += a[i];
        }

        half = (sum + 1) / 2;
        dp[0] = 1;
        for (int i = 0; i < p; i++) {
            memset(dp, 0, sizeof dp);
            dp[0] = 1;
            for (int j = 0; j < p; j++) {
                if (i != j)
                    for (int k = sum - a[j]; k >= 0; k--)
                        dp[k + a[j]] += dp[k];
            }

            int ans = 0;
            for (int j = half - a[i]; j < half && j >= 0; j++)
                ans += dp[j];

            printf("party %d has power index %d\\n", i + 1, ans);
        }
        printf("\\n");
    }

    return 0;
}

以上是关于UVA435 Block Voting序列处理的主要内容,如果未能解决你的问题,请参考以下文章

POJ2692 Australian Voting序列处理

UVA - 10410 Tree Reconstruction(栈处理递归)

Uva1608

UVa 1626 括号序列

创建序列化程序以显示正确的数据

Codeforces Round #388 (Div. 2) C. Voting