题目描述
题目要求计算每个政党在集体投票系统中的 Banzhaf\texttt{Banzhaf}Banzhaf 权力指数。给定每个政党的票数,多数联盟需要获得超过总票数一半的票数(即 ⌊总票数/2⌋+1\lfloor \text{总票数} / 2 \rfloor + 1⌊总票数/2⌋+1)。一个政党的权力指数定义为:该政党可以加入并使其变为多数联盟的少数联盟的数量。其中,空集被视为少数联盟。
输入格式
第一行一个整数 TTT,表示测试用例的数量。接下来 TTT 行,每行描述一个测试用例:第一个整数 PPP(1≤P≤201 \le P \le 201≤P≤20)表示政党数量,后面 PPP 个整数表示每个政党的票数。总票数不超过 100010001000。
输出格式
对于每个测试用例,输出 PPP 行,每行格式为:
party i has power index I
其中 iii 为政党编号(从 111 开始),III 为权力指数。每个测试用例的输出后跟一个空行。
样例
输入
3
5 7 4 2 6 6
6 12 9 7 3 1 1
3 2 1 1
输出
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
题目分析
本题的核心是计算每个政党的 Banzhaf\texttt{Banzhaf}Banzhaf 权力指数,即统计所有不含该政党的子集中,票数和小于多数阈值但加上该政党后大于等于多数阈值的子集个数。
问题转化
设总票数为 SSS,多数阈值 M=⌊S/2⌋+1M = \lfloor S / 2 \rfloor + 1M=⌊S/2⌋+1。对于政党 kkk,定义:
power(k)=∣{T⊆{1,…,P}∖{k} | ∑i∈Tv[i]<M≤∑i∈Tv[i]+v[k]}∣
power(k) = \left| \left\{ T \subseteq \{1,\ldots,P\} \setminus \{k\} \;\middle|\; \sum_{i \in T} v[i] < M \le \sum_{i \in T} v[i] + v[k] \right\} \right|
power(k)={T⊆{1,…,P}∖{k}i∈T∑v[i]<M≤i∈T∑v[i]+v[k]}
动态规划方法
对于每个政党 kkk,需要统计所有不含 kkk 的子集的票数和分布。使用 0/1\texttt{0/1}0/1 背包:
- dp[s]\textit{dp}[s]dp[s] 表示不考虑 kkk 时,子集和为 sss 的方案数。
- 初始化 dp[0]=1\textit{dp}[0] = 1dp[0]=1。
- 遍历所有 i≠ki \ne ki=k,对于每个 iii,从大到小更新:dp[j]+=dp[j−v[i]]\textit{dp}[j] += \textit{dp}[j - v[i]]dp[j]+=dp[j−v[i]]。
- 完成后,power(k)=∑j=max(0,M−v[k])M−1dp[j]\textit{power}(k) = \sum_{j = \max(0, M - v[k])}^{M-1} \textit{dp}[j]power(k)=∑j=max(0,M−v[k])M−1dp[j]。
多数阈值计算
多数阈值 M=⌊S/2⌋+1M = \lfloor S / 2 \rfloor + 1M=⌊S/2⌋+1,即超过一半的最小整数。例如总票数 252525,半数 12.512.512.5,多数需要 131313 票。
复杂度分析
对于每个政党 kkk,运行一次 0/1\texttt{0/1}0/1 背包,时间复杂度 O(P×S)O(P \times S)O(P×S),其中 S≤1000S \le 1000S≤1000,P≤20P \le 20P≤20,每个测试用例总复杂度 O(P2×S)≤20×20×1000=4×105O(P^2 \times S) \le 20 \times 20 \times 1000 = 4 \times 10^5O(P2×S)≤20×20×1000=4×105,完全可接受。
代码实现
// Block Voting
// UVa ID: 435
// Verdict: Accepted
// Submission Date: 2018-03-19
// UVa Run Time: 0.000s
//
// 版权所有(C)2018,邱秋。metaphysis # yeah dot net
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
int dp[MAXN];
int votes[24], majority = 0, cnt = 0;
int main(int argc, char *argv[])
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int cases;
cin >> cases;
for (int c = 1; c <= cases; c++)
{
cin >> cnt;
majority = 0;
for (int i = 0; i < cnt; i++)
{
cin >> votes[i];
majority += votes[i];
}
majority = majority / 2 + (majority % 2);
// DP, knapsack, count of different subset sum.
for (int k = 0; k < cnt; k++)
{
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = 0; i < cnt; i++)
{
if (i == k) continue;
for (int j = majority; j >= votes[i]; j--)
if (dp[j - votes[i]])
dp[j] += dp[j - votes[i]];
}
// Print.
int powerIndex = 0;
for (int i = max(0, majority - votes[k]); i < majority; i++)
powerIndex += dp[i];
cout << "party " << (k + 1) << " has power index " << powerIndex << '\n';
}
cout << '\n';
}
return 0;
}
427



被折叠的 条评论
为什么被折叠?



