The Sum of Sub-matrices
Description
Seen draw a big 3*n matrix, whose entries ai,j is all integer numbers (1 <= i <= 3, 1 <= J <= N). Now he selects K sub-matrices (each Aij is belong one sub-matrices), hoping to find the largest sum of Sub-matrices ' El Ements.
Input
There is multiple test cases.
For each test case, the first line contains an integer n, k (1 <= n <=, 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, the print one line with an integer.
Sample Input
5 31 2-10 3 53-2-10 2-10-10-10 1-10-10 2 3-1-2-3-42 3
Sample Output
144
The maximum and the K-sub-matrices inside the 3*n matrix are obtained.
State compression on a column
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<map>#include<Set>#include<stack>#include<algorithm>using namespaceStd;typedefLong LongLl;typedef pair<int,int>PII;Const intMoD = 1e9+7;Const intN = the ;Const intM = -;#defineX First#defineY SecondintSt1[m] = {0,1,2,4,3,5,6,7,8, -, A, -, + };intSt2[m] = {0,1,2,4,3,5,6,7,3,6,7,7,7 };intNum[m] = {0,1,1,1,2,2,2,3,1,1,2,2,1 };inta[3][n], dp[n][6][m], N, K;intSum (intColum,intSt) { intres =0 ; for(inti =0; I <3; ++i)if(st& (1<<i)) Res + =A[i][colum]; returnRes;}voidRun () {memset (DP,0x80,sizeofDP); dp[0][0][0] =0; for(inti =0; I <3; ++i) for(intj =1; J <= N; ++j) Cin>>A[i][j]; for(inti =1; I <= N; ++i) { for(intCS =0; CS < M; ++CS) { for(intPS =0; PS < M; ++PS) { intW =Sum (I,st2[cs]); for(intK1 =0; K1 <= k; ++K1) { intLow = Num[cs], up =Num[cs]; for(intz =0; Z <6; ++Z)if(St1[cs] & St1[ps] & (1<<Z)) low--; for(intK2 = low; K1 + K2 <= k && K2 <= up; ++K2) {DP[I][K1+K2][CS] = max (Dp[i][k1+k2][cs], dp[i-1][K1][PS] +W); } }}} cout<< *max_element (Dp[n][k], dp[n][k]+m) <<Endl;}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin); #endif //LOCALIos::sync_with_stdio (false); while(Cin >> N >>k) Run ();}View Code
Fzu 2060 the Sum of sub-matrices (state compression DP)