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. 微信小程序(16)-- bindtap,catchtap事件绑定的区别

    bindtap,catchtap事件绑定的区别,这里就涉及冒泡事件了.bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡. logs.wxml <view cl ...

  2. 使用eclipse创建Spring Boot项目

    环境介绍 1.jdk1.8 2.eclipse 3.maven 3.5.0 创建项目 eclectic 左上角 file -> new -> maven project 出现下图默认就好, ...

  3. python数字图像处理(三)边缘检测常用算子

    在该文将介绍基本的几种应用于边缘检测的滤波器,首先我们读入saber用来做为示例的图像 #读入图像代码,在此之前应当引入必要的opencv matplotlib numpy saber = cv2.i ...

  4. java 大数运算,高精度模板

    转自:https://blog.csdn.net/stffer/article/details/46382949 有修改 关于BigInteger类更详细的用法请移步官方文档 package prac ...

  5. Code Festival 2017 Qual B E Popping Balls

    传送门 神仙计数! 我的计数真的好差啊= = 不过这个题真的神仙 看了题解把整个过程在草稿纸上重写了一遍才想明白= =(一张草稿纸就没有了!!!) 计数的关键就是在于 枚举的有效性和独立性[不能重复计 ...

  6. IncepText算法笔记

    源头:场景文本经常受到aspect ratio, scal, and orientation的影响 为了解决这个问题,提出了inceptext,主要贡献在于添加了inception-text modu ...

  7. threeJS射线拾取机制及案例

    前言 在浏览器中浏览三维图形的时候,有时想要与三维图形之间做一些点击事件和交互操作,其中比较常用的一个解决方案就是使用Raycaster对象来实现(射线拾取). 基础知识 世界坐标系:webGL中,世 ...

  8. STM32时钟设置

    一.使用外部时钟,并设置为72MHz void SetSysClockToHSE(void) { ErrorStatus HSEStartUpStatus; /* SYSCLK, HCLK, PCLK ...

  9. soj#551 loj#2833 帐篷

    传送门 分析 dp[i][j]表示考虑了i行j列的方案数 我们每次考虑三种情况: 一个点自己放 两个点在同一行 两个点在同一列 代码 #include<bits/stdc++.h> usi ...

  10. How to show out three rows from the same databand On A4?

    How to show out three rows from the same databand On A4? Quote Post by DoraHuang » Tue Mar 13, 2018 ...