题目大意:给出n个5元组,要求从中选取k个,要求5个位置上的数的最大值的和尽量大。
#include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> using namespace std; int num[10005][6], temp[40], ans, n, k; void DFS(int pos, int sum, int d) { if (d == k) { ans = max(sum, ans); return; } for (int i = pos; i; i = (i - 1) & pos) { DFS(pos^i , sum + temp[i], d + 1); } return; } int main() { int T; scanf("%d\n", &T); while (T--) { memset(num, 0, sizeof(num)); memset(temp, 0, sizeof(temp)); scanf("%d %d\n", &n, &k); for (int i = 0; i < n; i++) { for (int j = 0; j < 5; j++) { scanf("%d", &num[i][j]); temp[j] = max(temp[j], num[i][j]); } } if (k >= 5) { printf("%d\n", temp[0] + temp[1] + temp[2] + temp[3] + temp[4]); continue; } memset(temp, 0, sizeof(temp)); for (int i = 0; i < n; i++) { for (int pos = 0; pos < 32; pos++) { //5位32种情况 int sum = 0; for (int j = 0; j < 5; j++) { if (pos & (1<<j)) { sum += num[i][j]; } } temp[pos] = max(temp[pos], sum); //32种情况,每种情况的最大值 } } ans = 0; DFS(31, 0, 0); printf("%d\n", ans); } return 0; }
原文地址:http://blog.csdn.net/llx523113241/article/details/43486319