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

For each test case, print one line with an integer.

Sample Input

5 3
1 2 -10 3 5
3 -2 -10 2 -10
-10 -10 1 -10 -10
 
 
2 3
-1 -2
-3 -4
2 3

Sample Output

14
4

求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)的更多相关文章

  1. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  2. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  3. hdu 4057(ac自动机+状态压缩dp)

    题意:容易理解... 分析:题目中给的模式串的个数最多为10个,于是想到用状态压缩dp来做,它的状态范围为1-2^9,所以最大为2^10-1,那我们可以用:dp[i][j][k]表示长度为i,在tri ...

  4. HDU1565+状态压缩dp

    简单的压缩状态 dp /* 状态压缩dp 同hdu2167 利用滚动数组!! */ #include<stdio.h> #include<string.h> #include& ...

  5. POJ1185 - 炮兵阵地(状态压缩DP)

    题目大意 中文的..直接搬过来... 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平 ...

  6. HDU2167+状态压缩DP

    状态压缩dp 详见代码 /* 状态压缩dp dp[ i ][ j ]:第i行j状态的最大和 dp[i][j] = max( dp[i-1][k]+sum[i][j] ); 题意:给定一个N*N的方格, ...

  7. POJ 3254 Corn Fields (状态压缩DP)

    题意:在由方格组成的矩形里面种草,相邻方格不能都种草,有障碍的地方不能种草,问有多少种种草方案(不种也算一种方案). 分析:方格边长范围只有12,用状态压缩dp好解决. 预处理:每一行的障碍用一个状态 ...

  8. hdu4336 Card Collector 状态压缩dp

    Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  10. poj 1185(状态压缩DP)

    poj  1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...

随机推荐

  1. metasploit下Windows的多种提权方法

    metasploit下Windows的多种提权方法 前言 当你在爱害者的机器上执行一些操作时,发现有一些操作被拒绝执行,为了获得受害机器的完全权限,你需要绕过限制,获取本来没有的一些权限,这些权限可以 ...

  2. axios 如何获取下载文件的进度条

    exportFun(){         let _that = this         const instance = this.axios.create({           onDownl ...

  3. c++求中位数

    #include <iostream> #include <cassert> #include <stack> #include <math.h> us ...

  4. 八核浮点型DSP的双千兆网接口设计方案

    千兆网络接口具有数据传输速率快.连接方便.可以即插即用的优点,使得其应用较为广泛.随着电子技术和处理器的发展,很多应用场合的数据通信速率超过千兆网口的实际传输速率.例如,在A/D采样中,需要直接存储A ...

  5. Linux之scp命令的使用

    Linux之scp命令的使用 1. scp简介 1.1 命令功能: scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令.linux的scp命令可 ...

  6. Codeforces Round #420 (Div. 2) - E

    题目链接:http://codeforces.com/contest/821/problem/E 题意:起初在(0,0),现在要求走到(k,0),问你存在多少种走法. 其中有n条线段,每条线段为(a, ...

  7. Oracle 反键索引/反向索引

    反键索引又叫反向索引,不是用来加速数据访问的,而是为了均衡IO,解决热块而设计的比如数据这样: 1000001 1000002 1000005 1000006 在普通索引中会出现在一个叶子上,如果部门 ...

  8. 【LeetCode】贪心 greedy(共38题)

    [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...

  9. Center os6.5设置静态ip

    DEVICE="eth0"BOOTPROTO=staticHWADDR="00:0C:29:95:89:35"IPV6INIT="yes"N ...

  10. cocos2D-X 打包

    { //首先有java jdk,android sdk,android ndk //用android studio  import //匹配gradle 的版本 有些gradle可能下载不下来,不用慌 ...