标签:
原题:
Description
Input
Output
Sample Input
Sample Output
#include <iostream> using namespace std; int main() { int t, n, a[25], i, key[25], k, total, ans[25]; scanf("%d", &t); while (t--) { memset(ans, 0, sizeof(ans)); total = 0; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", a + i), total += a[i]; total /= 2; //总票数的一半【写成total = (total+1)/2 会错……】 //枚举子集【1】~【2的n次方-1】 int maxs = 1 << 3; cout <<"maxs="<< maxs << endl; int flag = 0; for (i = 1; i < maxs; i++) { cout << flag++ << endl; k = 0; int tp = i, j = 0, sum = 0; while (tp) { //cout <<"tp="<<tp<<" "<< (tp & 1) << endl; if (tp & 1)//tp的二进制从左往右数第j位是1,则认为a[j]入了集合 key[k++] = j, sum += a[j]; //记住入了集合的a的元素编号j,并累加票数 tp >>= 1; //tp的二进制往右移动,即消去二进制最后一位 j++; } if (sum > total) //如果团体的票数超过总票数的一半 for (j = 0; j < k; j++) if (sum - a[key[j]] <= total) ans[key[j]]++; //如果没了a[key[j]]就不行,则编号为key[j]的元素为“关键加入者”,该元素权利指数+1 } printf("%d", ans[0]); for (i = 1; i < n; i++) printf(" %d", ans[i]); printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/shawn-ji/p/4696658.html