HDU-1557 权利指数(dfs)

权利指数

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1324    Accepted Submission(s): 921


Problem Description
在选举问题中,总共有n个小团体,每个小团体拥有一定数量的选票数。如果其中m个小团体的票数和超过总票数的一半,则此组合为“获胜联盟”。n个团体可形成若干个获胜联盟。一个小团体要成为一个“关键加入者”的条件是:在其所在的获胜联盟中,如果缺少了这个小团体的加入,则此联盟不能成为获胜联盟。一个小团体的权利指数是指:一个小团体在所有获胜联盟中成为“关键加入者”的次数。请你计算每个小团体的权利指数。
 

Input
输入数据的第一行为一个正整数T,表示有T组测试数据。每一组测试数据的第一行为一个正整数n(0<n<=20)。第二行有n个正整数,分别表示1到n号小团体的票数。
 

Output
对每组测试数据,在同一个行按顺序输出1到n号小团体的权利指数。
 

Sample Input
  
  
2 1 10 7 5 7 4 8 6 7 5
 

Sample Output
  
  
1 16 22 16 24 20 22 16
 


思路:n最大只由20,所以我们可以用深搜把所有情况都搜一遍。我们令所有票数的总和的一半为key,然后求每一个数的权利指数时我们都用一次dfs,当求第i个数的权利指数时,我们dfs的时候就跳过第i个数,直接搜下一个数,全部搜完后再判断当前的sum是否满足sum<key&&sum+a[i]>=key,如果满足ans++,最后ans就是第i个数的权利指数。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#include <functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9 + 5;
const int MAXN = 100000007;
const int MOD = 1e9+7;
const double eps = 1e-8;
const double PI = acos(-1.0);

int key;
int n;
int ans;
int a[25];

void dfs(int num, int sum,int t)
{
	if (num+1 == t)//跳过第t个数
	{
		dfs(num + 1, sum, t);
		return;			
	}
	if (num >= n)
	{
		if (sum < key&&sum + a[t] >= key)
			ans++;
		return;
	}
	dfs(num + 1, sum, t);
	dfs(num + 1, sum + a[num + 1], t);
}


int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		key = 0;
		ans = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			key += a[i];
		}
		key = (key + 2) / 2;//要超过,是大于不是大于等于
		dfs(0, 0, 1);
		printf("%d", ans);
		for (int i = 2; i <= n; i++)
		{
			ans = 0;
			dfs(0, 0, i);
			printf(" %d", ans);
		}
		printf("\n");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值