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--PDF

题意: 

给出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的更多相关文章

  1. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  2. 状态压缩+枚举 UVA 11464 Even Parity

    题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...

  3. 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 ...

  4. 洛谷P1036 选数 题解 简单搜索/简单状态压缩枚举

    题目链接:https://www.luogu.com.cn/problem/P1036 题目描述 已知 \(n\) 个整数 \(x_1,x_2,-,x_n\) ,以及 \(1\) 个整数 \(k(k& ...

  5. hdu 4033 状态压缩枚举

    /* 看别人的的思路 搜索搜不出来我太挫了 状态压缩枚举+好的位置 */ #include<stdio.h> #include<string.h> #define N 20 i ...

  6. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  7. UVA 1508 - Equipment dp状态压缩

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

  8. UVA 11825 状态压缩DP+子集思想

    很明显的状态压缩思想了.把全集分组,枚举每个集合的子集,看一个子集是否能覆盖所有的点,若能,则f[s]=max(f[s],f[s^s0]+1).即与差集+1比较. 这种枚举集合的思想还是第一次遇到,果 ...

  9. 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 ...

随机推荐

  1. Java第三阶段学习(六、多线程)

    一.进程和线程的区别: 进程:指正在运行的程序,当一个程序进入内存运行,就变成一个进程. 线程:线程是进程的一个执行单元. 总结:一个程序运行后至少会有一个进程,一个进程可以有多个线程. 多线程:多线 ...

  2. 用 scikit-learn 和 pandas 学习线性回归

      用 scikit-learn 和 pandas 学习线性回归¶ from https://www.cnblogs.com/pinard/p/6016029.html 就算是简单的算法,也需要跑通整 ...

  3. windows下解决PyCharm控制台中文输出乱码

    我用的PyCharm是2018.2版本 在调用os.system()的过程中遇到了控制台中文乱码的问题,具体如下 网上说的将两个Encoding格式都设置为UTF-8并没有解决问题,后来我将Proje ...

  4. jQuery中click(),bind(),live()的区别(转)

    原文:http://www.jquery001.com/click%28%29-bind%28%29-live%28%29-delegate%28%29.html click(),bind(),liv ...

  5. Hive项目开发环境搭建(Eclipse\MyEclipse + Maven)

    写在前面的话 可详细参考,一定得去看 HBase 开发环境搭建(Eclipse\MyEclipse + Maven) Zookeeper项目开发环境搭建(Eclipse\MyEclipse + Mav ...

  6. python、Java、大数据和Android的薪资如何?

    莫名其妙,从去年年底开始,Python这个东西在中国,突然一下子就火起来了,直至现在,他的热度更是超越了java,成为软件工程师最为关注的话题.Python之所以能火起来,很大一方面是因为大数据.人工 ...

  7. 【Ray Tracing The Next Week 超详解】 光线追踪2-2

    Chapter 2:Bounding Volume Hierarchies 今天我们来讲层次包围盒,乍一看比较难,篇幅也多,但是咱们一步一步来,相信大家应该都能听懂 BVH 和 Perlin text ...

  8. SQL Server密码爆破工具SQLdict

    SQL Server密码爆破工具SQLdict SQL Server是Windows系统常用的数据库服务器.它广泛采用用户名和密码方式,进行身份认证.Kali Linux提供一款专用的数据库密码爆破工 ...

  9. 获取当前页面url中的参数 coffeescript+node.js+angular

    获取当前url:@$window.alert @$location.url()获取参数(json格式)@$window.alert @$location.search().channel

  10. 面向对象设计原则 里氏替换原则(Liskov Substitution Principle)

    里氏替换原则(Liskov Substitution Principle LSP)面向对象设计的基本原则之一. 里氏替换原则中说,任何基类可以出现的地方,子类一定可以出现. LSP是继承复用的基石,只 ...