UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs
ACM
题目地址: option=com_onlinejudge&Itemid=8&category=457&page=show_problem&problem=4254" target="_blank" style="color:rgb(0,136,204); text-decoration:none">UVA 1508 - Equipment
题意:
给出n个5元组,从中选出k组。使得这些组中5个位置,每一个位置上最大数之和最大。
分析:
想了好久...最后还是參考了别人的题解...
只是思路非常棒,值得学习。
因为n的范围为1,10000,所以从n考虑是非常难解出来的。
于是我们从5元组考虑。
每组5元组,最后可能被选择作为和的一部分,就是[11111],即[所有被选中做和]的子集,一共同拥有31种情况。
我们仅仅要预处理这31种情况可能得到的最大的和。
然后dfs遍历子集即可了。
详细见代码。
代码:
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: 1508.cpp
* Create Date: 2014-06-28 20:55:20
* Descripton:
*/ #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = 10010;
int n, t, k, ans;
int m[5], r[N][5], mmax[40]; int dfs(int S, int num) { // find num different subset in S, return the max sum
if (num == 0) {
return 0;
} int tmp = 0;
for (int S0 = S; S0; S0 = (S0-1)&S) {
tmp = max(tmp, mmax[S0] + dfs((S0^S), num - 1));
}
return tmp;
} int main() {
scanf("%d", &t);
while (t--) {
memset(m, 0, sizeof(m)); // input
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 5; j++) {
scanf("%d", &r[i][j]);
m[j] = max(m[j], r[i][j]);
}
} if (k >= 5) { // just the max int sum = 0;
for (int i = 0; i < 5; i++) {
sum += m[i];
}
printf("%d\n", sum); } else { memset(mmax, 0, sizeof(mmax)); for (int i = 0; i < n; i++) { // for every one
for (int S = 0; S <= 31; S++) { // every situation, 00000 to 11111
int tmp = 0;
for (int k = 0; k < 5; k++) {
if (S&(1<<k)) {
tmp += r[i][k];
}
mmax[S] = max(mmax[S], tmp); // update the max of every situation
}
}
}
printf("%d\n", dfs(31, k)); // find the max sum in 11111 }
}
return 0;
}
UVA 1508 - Equipment 状态压缩 枚举子集 dfs的更多相关文章
- codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...
- 状态压缩+枚举 UVA 11464 Even Parity
题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...
- POJ 1873 UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)
题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a ...
- 洛谷P1036 选数 题解 简单搜索/简单状态压缩枚举
题目链接:https://www.luogu.com.cn/problem/P1036 题目描述 已知 \(n\) 个整数 \(x_1,x_2,-,x_n\) ,以及 \(1\) 个整数 \(k(k& ...
- hdu 4033 状态压缩枚举
/* 看别人的的思路 搜索搜不出来我太挫了 状态压缩枚举+好的位置 */ #include<stdio.h> #include<string.h> #define N 20 i ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- UVA 1508 - Equipment dp状态压缩
题意: 已知n个5元组,从中选出k组,使得这些组中5个位置,每个位置上最大数之和最大. 分析:当k>5时,就是n个5元组最大的数之和,当k<5时,就当做5元组,状态压缩,用00000表示 ...
- UVA 11825 状态压缩DP+子集思想
很明显的状态压缩思想了.把全集分组,枚举每个集合的子集,看一个子集是否能覆盖所有的点,若能,则f[s]=max(f[s],f[s^s0]+1).即与差集+1比较. 这种枚举集合的思想还是第一次遇到,果 ...
- 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 ...
随机推荐
- 【Java】 大话数据结构(16) 排序算法(3) (堆排序)
本文根据<大话数据结构>一书,实现了Java版的堆排序. 更多:数据结构与算法合集 基本概念 堆排序种的堆指的是数据结构中的堆,而不是内存模型中的堆. 堆:可以看成一棵完全二叉树,每个结点 ...
- HNOI2018酱油记
按照惯例,每次比赛完以后都要写酱油记. Day0: 明天就要省选了,今天同学们都回去了(因为后天要去春游),整个年级只剩下竞赛生.本来打算晚上好好复习一下,结果......颓了一晚上......(好吧 ...
- pymysql模块 执行sql封装
封装pymysql模块执行sql class HandCost(object): """ 处理数据库中的数据 """ def __init_ ...
- HTML中元素的定位方式
初中物理就学过,位置是相对的,要有参照物,因此,所有定位都是相对参照物的定位. position 属性: 规定元素的定位类型,该属性的可选值有static.relative.absolute.fixe ...
- 024.Zabbix告警等级机制
一 等级告警 告警升级可以对告警结果按自定义的时间段进行进行消息发送,并执行命令,形成一个梯度的告警处理. 二 按时间自定义告警梯度 2.1 添加时间发生的时间戳和发送时间 2.2 设置三个等级梯度 ...
- CAB归档文件提取工具cabextract
CAB归档文件提取工具cabextract 在对Windows系统进行数字取证中,经常会遇到.cab的文件.该文件是Windows的压缩格式,一般是作为安装包文件.Kali Linux预置了专用的 ...
- [POI2013]Morskie opowieści
[POI2013]Morskie opowieści 题目大意: 一个\(n(n\le5000)\)点\(m(m\le5000)\)边无向图,边权均为\(1\),有\(k(k\le10^6)\)个询问 ...
- C#、Java、Javascript获取Unix时间戳
背景: 因为项目需要,需要几种语言联动开发,日期字段设计的数字型 获取Unix时间戳代码: Java System.currentTimeMillis() Javascript new Date(). ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp
Problem H. Password Service 题目连接: http://www.codeforces.com/gym/100253 Description Startups are here ...
- oracle A用户访问B用户的表aa
在B中:grant select on aa to A; (还可以配置insert,update,delete权限)