The Korea Defense and Science Institute, shortly KDSI, has been putting constant effort into new
equipment for individual soldiers for recent years, and at last released N new types of equipment.
KDSI has already done evaluation of each of the N types of equipment, finally resulting in scores in five
categories: attack improvement, defense improvement, vision improvement, portability, and easiness of
usage. The score in each category is quantified as an integer in the range 0 to 10,000, and the rating
of each type of equipment is thus represented as a sequence of five integers.
In consideration of costs and capability of average individual soldiers, KDSI also reported that each
soldier will be able to install at most K types of equipment on his body to extend his ability. If a single
type is installed on a soldier, then his ability in each category is extended by the specified score of that
type. Moreover, if a soldier installs more than one type of equipment, then his ability in each category
is extended by the maximum score of the chosen types in that category. For example, if the vision
improvement scores of type a and type b are 10 and 15, respectively, then installing a combination
of two types a and b will result in a vision improvement by their maximum score 15. We call the
maximum score 15 the extension score of a category; so, the extension score of vision improvement for
combination {a, b} is 15 in this example.
KDSI now started devising a way of finding an optimal combination of K types of equipment for best
performance of individual soldiers. While a force can sometimes be of a special purpose so that a certain
category would be more important than the others, every single category is, however, regarded equally
important in general. For this general purpose, KDSI defined the objective score of a combination of
equipment to be the sum of the extension scores of the five categories for the combination. KDSI thus
wants to find a best combination of K types of equipment such that its objective score is maximized
among all possible combinations of K types. You are asked by KDSI to devise and write a computer
program that finds the objective score of a best combination of K types of equipment, that is, the
maximum possible objective score for all possible combinations of K types among the given N types of
equipment.
Put differently, you are given N types of equipment {1, . . . , N} and their ratings Ri represented by
five integers Ri = (ri,1, ri,2, ri,3, ri,4, ri,5) with 0 ≤ ri,j ≤ 10, 000 for each i = 1, . . . , N and j = 1, . . . , 5.
Given another natural number K (1 ≤ K ≤ N), your program has to compute the objective score of a
best combination of K types of equipment.
For example, consider an input instance in which N = 4, K = 2, and each Ri
is given as below:
R1 = (30, 30, 30, 30, 0)
R2 = (50, 0, 0, 0, 0)
R3 = (0, 50, 0, 50, 10)
R4 = (0, 0, 50, 0, 20).
Then, choosing R1 and R3 forms a best combination of two types {1, 3} and yields the objective
score 30 + 50 + 30 + 50 + 10 = 170, which will be the answer of a correct program.
Input
Your program is to read from standard input. The input consists of T test cases. The number T of
test cases is given in the first line of the input. From the second line, each test case is given in order,
consisting of the following: a test case contains two integers N (1 ≤ N ≤ 10, 000) and K (1 ≤ K ≤ N)
in its first line, and is followed by N lines each of which consists of five integers inclusively between
0 and 10,000, representing the five scores ri,1, ri,2, ri,3, ri,4, and ri,5 of each type i of equipment for
i = 1, . . . , N in order. Two consecutive integers in one line are separated by a single space and there is
no empty line between two consecutive test cases.
Output
Your program is to write to standard output. Print exactly one line for each test case. The line should
contain a single integer that is the objective score of a best combination of K types of equipment; the
maximum possible objective score for all possible combinations of K types among the given N types
of equipment for the corresponding test case.
The following shows sample input and output for two test cases.
Sample Input
2
4 2
30 30 30 30 0
50 0 0 0 0
0 50 0 50 10
0 0 50 0 20
5 1
10 20 60 0 0
0 0 20 50 30
30 50 20 20 0
10 10 10 20 30
30 0 20 10 20
Sample Output
170
120

给出 n 组数据,每组数据是一个五元组,问当你从这 n 组数据中取出 m 组时,可以获得的最大价值是多少。

根据题意,当 m>=5 时,可以取上每一列的最大值,这样获得的价值是最大的。

当m < 5 时,如果对 n 去dfs,虽然m只有4,但是复杂度还是太高了....没莽过去...

那么考虑对五元组,对于一个五元组一共只有 31 种取法,打表出这 31 种取法可以取得的最大值。(mx[i]是 i 状态下只取一组数据的最大值)

接下来对于全集31,开始 dfs。(1表示还可以取,0表示已经取了)

对于每次开始 dfs 的状态 u,枚举他的子集 i,以及对补集 i^u 进行下一步的dfs。

这时候还有一种情况,两步 dfs 取的子集,事实上都是由同一组数据来的,那么这种应该要排除。

但是其实这种情况一定不可能是最优解,因为这两个不同子集都是由同一个数据来的,那么这两个子集的并集的最优解,一定也是从这组数据来的,所以只需要通过一步来完成,那么通过上面那种方法,必然会浪费一步,从而使最后的答案变小,所以一定会被排除。

/*
.
';;;;;.
'!;;;;;;!;`
'!;|&#@|;;;;!:
`;;!&####@|;;;;!:
.;;;!&@$$%|!;;;;;;!'.`:::::'.
'!;;;;;;;;!$@###&|;;|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''''::;!%&##$'
`$##@$$@@&|!!;;;:'''''::::;;;;;|&#%.
;&@##&$%!;;;;;;::''''''''::;!|%$@#@&@@:
.%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%.
:@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;`
`%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&'
|##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!'
:;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$: !@#$'
|##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%: '%%!%&;
|&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||` '%$|!%&;
|@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&: .|%;:!&%`
!@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&:
'$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&:
|#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$'
|&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:!$@!
`%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
!&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
'%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
.!%%&%'`|$; :|$#%|@#&;%#%.
*/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e4 + ;
const int maxm = 1e5 + ;
const ll mod = 1e9 + ;
const ll INF = 1e18 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
using namespace std; int n, m;
int cas, tol, T; struct Node{
int a[];
} node[maxn];
int mx[];
int ans; void dfs(int u, int cnt, int sum) {
if(cnt == m) {
ans = max(ans, sum);
return ;
}
for(int i=u; i; i=(i-)&u) {
dfs(i^u, cnt+, sum + mx[i]);
}
} int main() {
scanf("%d", &T);
while(T--) {
mes(mx, );
mes(node, );
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++) {
for(int j=; j<=; j++) {
scanf("%d", &node[i].a[j]);
}
}
if(m >= ) {
int r1, r2, r3, r4, r5;
r1 = r2 = r3 = r4 = r5 = ;
for(int i=; i<=n; i++) {
for(int j=; j<=; j++) {
r1 = max(r1, node[i].a[]);
r2 = max(r2, node[i].a[]);
r3 = max(r3, node[i].a[]);
r4 = max(r4, node[i].a[]);
r5 = max(r5, node[i].a[]);
}
}
printf("%d\n", r1+r2+r3+r4+r5);
continue;
} else {
for(int i=; i<=n; i++) {
for(int u=; u<=; u++) {
int sum = ;
for(int j=; j<=; j++) {
if(u & (<<(j-))) {
sum += node[i].a[j];
}
}
mx[u] = max(mx[u], sum);
}
}
ans = ;
dfs(, , );
printf("%d\n", ans);
}
}
return ;
}

Equipment UVA - 1508(子集补集)的更多相关文章

  1. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  2. UVA 1508 - Equipment dp状态压缩

    题意:  已知n个5元组,从中选出k组,使得这些组中5个位置,每个位置上最大数之和最大. 分析:当k>5时,就是n个5元组最大的数之和,当k<5时,就当做5元组,状态压缩,用00000表示 ...

  3. BZOJ 2560: 串珠子 (状压DP+枚举子集补集+容斥)

    (Noip提高组及以下),有意者请联系Lydsy2012@163.com,仅限教师及家长用户. 2560: 串珠子 Time Limit: 10 Sec Memory Limit: 128 MB Su ...

  4. Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记1

    (5)SELECT   (5-2) DISTINCT    (5-3)TOP(<top_specifications>)   (5-1)<select_list> (1)FRO ...

  5. UVa 11825 - Hackers' Crackdown DP, 枚举子集substa = (substa - 1)&sta 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. uva 11825 Hackers&#39; Crackdown (状压dp,子集枚举)

    题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有同样的n种服务),对每台计算机,你能够选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让很多其它的服务瘫痪( ...

  7. UVA 11825 - Hackers&#39; Crackdown 状态压缩 dp 枚举子集

    UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...

  8. UVA 11825 Hackers’ Crackdown(集合动态规划 子集枚举)

    Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...

  9. uva 11825 巧妙地子集枚举方法

    https://vjudge.net/problem/UVA-11825 题目大意,有n台服务器,有n种服务,每台服务器都运行着所有的服务,一台服务器可以被攻击一次其中的一种服务,当你选择攻击某台服务 ...

随机推荐

  1. MySQL索引初探

    一.什么是索引? 帮助数据库系统实现高效获取数据的数据结构 索引可以帮助我们快速地定位到数据而不需要每次搜索的时候都遍历数据库中的每一行. 二.常见实现方式有哪些? 常见索引模型有三种:哈希表.有序数 ...

  2. SpringBoot+MyBatis配置多数据源

    SpringBoot 可以支持多数据源,这是一个非常值得学习的功能,但是从现在主流的微服务的架构模式中,每个应用都具有唯一且准确的功能,多数据源的需求很难用到,考虑到实际情况远远比理论复杂的多,这里还 ...

  3. java:编程比赛中有用的方法整理(一)数组

    我曾经参加过几次编程比赛,但是当时用的是c语言,现在学习了java,打算专攻java组,故以此整理. 数组无论在哪里都必不可少. 一.数组的拷贝: 使用Arrays类的copyOf方法: 1.将一个数 ...

  4. 详细QRCode生成二维码和下载实现案例

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using ThoughtWo ...

  5. Win10系统下装Ubuntu虚拟机的遇到的问题总结

    环境和工具 win10操作系统 VMware Workstation 12 Ubuntu 14.0 64位 教程可参考:VMware Ubuntu安装详细过程(非常靠谱) [因为我的安装过程不是十分顺 ...

  6. 使用py2exe将python脚本转换成exe可执行文件

    Python(wiki en  chs)是一门弱类型解释型脚本语言,拥有动态类型系统和垃圾回收功能,支持多种编程范式:面向对象.命令式.函数式和过程式编程. 由于Python拥有一个巨大而广泛的标准库 ...

  7. mysql字段约束

    为了确保数据的完整性和唯⼀性,关系型数 据库通过约束机制来实现目. 一. unique 唯一性约束    : 值不可重复: 二. not null    非空约束    : 值不可为空: 三. def ...

  8. Gitlab源码库里代码提交后,如何触发jenkins自动构建?

    版本库里代码提交后,如何触发jenkins自动构建?这是一个面试题,感觉自己回答的并不好,因为并没有用过这个功能,之前公司实际项目用的是svn版本管理,一般都用立刻构建,和定时任务构建(不管代码是否有 ...

  9. JavaScript中编码函数escape,encodeURI,encodeURIComponent

    第一:escape():对字符串进行编码,escape()不编码的字符:@*/+ 第二:encodeURI() 函数可把字符串作为 URI 进行编码.不会进行转义的:;/?:@&=+$,# 第 ...

  10. 网络流 E - Escape HDU - 3605

    2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...