UVA1508-Equipment(状态压缩+暴力枚举)
2014-08-08 20:38
417 查看
题目链接
题意:有n个装备,每个装备分别有5个属性值。要你从中选出k个装备,使得所得的实力加成最多。(每个属性值要选k个装备中最大的那个数值)
思路:5个属性值可以有2^5-1种方案,所以直接暴力枚举所以子集,找出和最大的k个。我们可以预处理每个子集在k个装备中出现的最大值。
PS:二进制表示子集还是很好用的,必须要好好掌握。
参考思路
题意:有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] ; 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; }
相关文章推荐
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
- UVa 1508 - Equipment (状态压缩 + 枚举子集)
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
- uva 1508 - Equipment(暴力+枚举子集)
- uva 1508 - Equipment(暴力+枚举子集)
- uva1508-Equipment 状态压缩 记忆化搜索
- UVa --- 10464 Even Parity 偶数矩阵 【状态压缩 + 暴力枚举 + 思维】
- uva1508(状态压缩 +枚举)
- uva 1508 Equipment(暴力+枚举子集)
- POJ 3279 (状态压缩暴力枚举)
- UVA 1508 Equipment(技巧枚举)
- HDU4770 —— Lights Against Dudely (状态压缩,暴力枚举)
- HDU 4770 Lights Against Dudely 【状态压缩+暴力枚举】
- CF 474C Captain Marmot[暴力枚举 || bfs状态压缩 ]
- uva 1047 - Zones-World finals 2005 状态压缩枚举
- UVALive 3953 Strange Billboard (状态压缩+枚举)
- UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集
- UVALive - 3693 Balancing the Scale 枚举 + 状态压缩
- 枚举+状态压缩——2013 杭州区域赛A ( UVA 6454 )
- UVA - 565 Pizza Anyone?(状态压缩+暴利枚举)