标签:style blog http color os io for art
题意:有n个装备,每个装备分别有5个属性值。要你从中选出k个装备,使得所得的实力加成最多。(每个属性值要选k个装备中最大的那个数值)
思路:5个属性值可以有2^5-1种方案,所以直接暴力枚举所以子集,找出和最大的k个。我们可以预处理每个子集在k个装备中出现的最大值。
PS:二进制表示子集还是很好用的,必须要好好掌握。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 1 << 5; const int N = 5; int sum[MAXN], arr[10005][N]; int n, k, ans; void build() { memset(sum, 0, sizeof(sum)); for (int i = 0; i < n; i++) for (int j = 0; j < MAXN; j++) { int temp = 0; for (int k = 0; k < N; k++) if (j & (1 << k)) temp += arr[i][k]; sum[j] = max(sum[j], temp); } } void dfs(int S, int cnt, int x) { if (cnt == k) { ans = max(ans, x); } for (int i = S; i; i = (i - 1) & S) dfs(S ^ i, cnt + 1, x + sum[i]); } int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) for (int j = 0; j < N; j++) scanf("%d", &arr[i][j]); ans = 0; if (k >= 5) { for (int i = 0; i < N; i++) { int temp = 0; for (int j = 0; j < n; j++) temp = max(temp, arr[j][i]); ans += temp; } } else { build(); dfs(MAXN - 1, 0, 0); } printf("%d\n", ans); } return 0; }
UVA1508-Equipment,布布扣,bubuko.com
标签:style blog http color os io for art
原文地址:http://blog.csdn.net/u011345461/article/details/38444679