FZU 2060 The Sum of Sub-matrices(状态压缩DP)
The Sum of Sub-matrices
Description
Seen draw a big 3*n matrix , whose entries Ai,j are all integer numbers ( 1 <= i <= 3, 1 <= j <= n ). Now he selects k sub-matrices( each Aij only belong one sub-matrices ), hoping to find the largest sum of sub-matrices’ elements.
Input
There are multiple test cases.
For each test case, the first line contains an integer n, k (1 <= n <= 100, 1 <= k <= 5, 3 * n >= k). The next three lines with n integers each gives the elements of the matrix ( | Ai,j | <= 10000).
Output
Sample Input
Sample Output
求3*n的矩阵里面的 k个子矩阵的最大和。
对列进行状态压缩
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm> using namespace std; typedef long long LL;
typedef pair<int,int> pii;
const int mod = 1e9+;
const int N = ;
const int M = ;
#define X first
#define Y second int st1[M] = { , , , , , , , , , , , , };
int st2[M] = { , , , , , , , , , , , , };
int num[M] = { , , , , , , , , , , , , };
int A[][N] , dp[N][][M] , n , k ; int Sum( int colum , int st ) {
int res = ;
for( int i = ; i < ; ++i ) if( st&(<<i) )res += A[i][colum];
return res ;
} void Run() {
memset( dp , 0x80 , sizeof dp ) ;
dp[][][] = ; for( int i = ; i < ; ++i )
for( int j = ; j <= n ; ++j )
cin >> A[i][j]; for( int i = ; i <= n ; ++i ) {
for( int cs = ; cs < M ; ++cs ) {
for( int ps = ; ps < M ; ++ps ) {
int w = Sum(i,st2[cs]);
for( int k1 = ; k1 <= k ; ++k1 ) {
int low = num[cs] , up = num[cs] ;
for( int z = ; z < ; ++z ) if( st1[cs] & st1[ps] &(<<z) ) low--;
for( int k2 = low ; k1 + k2 <= k && k2 <= up ; ++k2 ){
dp[i][k1+k2][cs] = max( dp[i][k1+k2][cs] , dp[i-][k1][ps] + w );
}
}
}
}
}
cout << *max_element( dp[n][k] , dp[n][k]+M ) << endl ;
} int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
while( cin >> n >> k )Run();
}
FZU 2060 The Sum of Sub-matrices(状态压缩DP)的更多相关文章
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- hdu 4057(ac自动机+状态压缩dp)
题意:容易理解... 分析:题目中给的模式串的个数最多为10个,于是想到用状态压缩dp来做,它的状态范围为1-2^9,所以最大为2^10-1,那我们可以用:dp[i][j][k]表示长度为i,在tri ...
- HDU1565+状态压缩dp
简单的压缩状态 dp /* 状态压缩dp 同hdu2167 利用滚动数组!! */ #include<stdio.h> #include<string.h> #include& ...
- POJ1185 - 炮兵阵地(状态压缩DP)
题目大意 中文的..直接搬过来... 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平 ...
- HDU2167+状态压缩DP
状态压缩dp 详见代码 /* 状态压缩dp dp[ i ][ j ]:第i行j状态的最大和 dp[i][j] = max( dp[i-1][k]+sum[i][j] ); 题意:给定一个N*N的方格, ...
- POJ 3254 Corn Fields (状态压缩DP)
题意:在由方格组成的矩形里面种草,相邻方格不能都种草,有障碍的地方不能种草,问有多少种种草方案(不种也算一种方案). 分析:方格边长范围只有12,用状态压缩dp好解决. 预处理:每一行的障碍用一个状态 ...
- hdu4336 Card Collector 状态压缩dp
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- poj 1185(状态压缩DP)
poj 1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...
随机推荐
- Python之获取文件夹中文件列表以及glob与fnmatch模块的使用
获取文件夹中的文件列表 print(os.listdir("../secondPackage")) # ['__init__.py', 'secondCookBook.py', ' ...
- jquery+ajax获取本地json对应数据
首先,记得导入jquery.js文件. json内容: var obj123=[ {"option":"2,3,9,14,19,24,32",&q ...
- vue 实现模糊检索,并根据其他字符的首字母顺序排列
昨天让我做一个功能,实现一个模糊检索,我就想,那做呗,然后开始正常的开发 代码如下: HTML VUE 因为是实时的,所以写了将逻辑写到了watch中 五分钟搞定. 我以为这就完了,然而产品的需求 ...
- JS中去除字符串空白符
海纳百川,有容乃大 1.通过原型创建字符串的trim() //去除字符串两边的空白 String.prototype.trim=function(){ return this.replace(/(^\ ...
- elasticsearch 基础 —— Explain、Version、min_score、query rescorer
Explain 相关度得分计算: GET /_search { "explain": true, "query" : { "term" : ...
- R语言数据类型与数据结构
一.数据类型 5种 1.character 字符 2.numeric 数值 3.integer 整数 一般数字的存储会默认为数值类型,如果要强调是整数,需要在变量值后面加上 L. x <- 5L ...
- 03.LNMP架构-PHP源码包编译部署详细步骤
一.环境准备 操作系统:CentOS_Server_7.5_x64_1804.iso 部署组件:yasm+libmcrypt+libvpx+tiff+libpng+freetype+jpeg+libg ...
- React(7) --react父子组件传参
react父子组件传参 父级向子级传参:在父组件中,我们引入子组件,通过给子组件添加属性,来起到传参的作用,子组件可以通过props获取父组件传过来的参数. 在父组件中: import React f ...
- 微信小程序(18)-- 自定义头部导航栏
最近做的项目涉及相应的页面显示相应的顶部标题,所以就需要自定义头部导航了. 首先新建一个顶部导航公用组件topnav,导航高度怎么计算? 1.wx.getSystemInfo 和 wx.getSyst ...
- Spring之控制反转——IoC、面向切面编程——AOP
控制反转——IoC 提出IoC的目的 为了解决对象之间的耦合度过高的问题,提出了IoC理论,用来实现对象之间的解耦. 什么是IoC IoC是Inversion of Control的缩写,译为控制 ...