luogu 2331 [SCOI2005] Maximum submatrix

Title Description

There is a matrix of n * m, where you select k sub-matrix, such that the sum of k sub-matrix is ​​the maximum value. Note: k selected sub-matrix can not overlap each other.

Input Format

First row n, m, k (1≤n≤100,1≤m≤2,1≤k≤10), scoring matrix is ​​described next n lines in each row of each element (for each sub-element does not exceed the absolute value of the value 32767).

Output Format

Only a behavior k sub-matrix and the maximum number of scores.

Sample input and output

Input # 1
3 2 2
1 -3
2 3
-2 3
Output # 1
9

analysis

method 1

O (N 3 * K): Luogu 2331题解

This problem, the empty matrix can be considered sub-matrix

when m == 1 DP [column before i] [j matrix is ​​selected from a], into the selected cell is not selected from the two kinds of current, the time can be selected as a preceding matrix attached

 

m == 2 m == practice practice analogy 1,

F [1 selected from the first column before the i-th] [Second column gate before the j] [k sub-matrix is ​​selected from a] = the maximum and

For the current to selected sub-matrix, the election situation there are four kinds:

1. Do not choose

2. Select the first column

3. select the second row

3.i == j when the two can be considered as a matrix

 

Code:

 1 /***********************
 2 User:Mandy.H.Y
 3 Language:c++
 4 Problem:
 5 Algorithm: 
 6 ***********************/
 7 
 8 #include<bits/stdc++.h>
 9 
10 using namespace std;
11 
12 const int maxn = 105;
13 
14 int n,m,K;
15 int a[105][5];
16 int sum[105][5];
17 int dp[maxn][15];
18 int f[maxn][maxn][15];
19 
20 template<class T>inline void read(T &x){
21     x = 0;bool flag = 0;char ch = getchar();
22     while(!isdigit(ch)) flag |= ch == '-',ch = getchar();
23     while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
24     if(flag) x = -x;
25 }
26 
27 template<class T>void putch(const T x){
28     if(x > 9) putch(x / 10);
29     putchar(x % 10 | 48);
30 }
31 
32 template<class T>void put(const T x){
33     if(x < 0) putchar('-'),putch(-x);
34     else putch(x);
35 }
36 
37 void file(){
38     freopen("2331","r",stdin);
39 }
40 
41 void readdata(){
42     read(n);read(m);read(K);
43     for(int i = 1;i <= n; ++ i){
44         for(int j = 1;j <= m; ++ j){
45             read(a[i][j]);
46             SUM [I] [J] = SUM [I - . 1 ] [J] + A [I] [J];
 47          }
 48      }
 49  }
 50  
51 is  void Work () {
 52 is      IF (m == . 1 ) { // In fact, this is not necessary, because it can do when m == 2 except that the second column is equal to 0; 
53 is          for ( int I = . 1 ; I <= n-; ++ I)
 54 is              for ( int J = . 1 ; J <= K; ++ J) { 
 55                  DP [I] [J] DP = [I- . 1 ] [J]; // no option 
56 is                  for (int L = 0 ; L <= I; L ++) // L may be equal to i, the matrix can be considered null sub-matrix 
57 is                      DP [I] [J] = max (DP [I] [J], DP [L] [J- . 1 ] + [I] SUM [ . 1 ] - SUM [L] [ . 1 ]);
 58              }
 59          PUT (DP [n-] [K]);
 60      } the else {
 61 is          for ( int I = . 1 ; I <= n-; ++ I)
 62 is              for ( int J = . 1 ; J <= n-; ++ J)
 63 is                  for ( int K = . 1 ; K <= K; ++K) {
 64                      F [I] [J] [K] = max (F [I- . 1 ] [J] [K], F [I] [J- . 1 ] [K]); // not selected 
65                      for ( int L = 0 ; L <= I; L ++) // selected from the first column 
66                          F [I] [J] [K] = max (F [I] [J] [K], F [L] [J] [- K- . 1 ] + 
 67                                           SUM [I] [ . 1 ] - SUM [L] [ . 1 ]);
 68                      
69                      for ( int L = 0 ; L <= J; L ++) // second election column 
70                         f[i][j][k] = max(f[i][j][k],f[i][l][k-1] + 
71                                          sum[j][2] - sum[l][2]);
72                     
73                     if(i == j)
74                         for(int l = 0;l <= i; ++ l)//两列一起 
75                             f[i][j][k] = max(f[i][j][k],f[l][l][k-1] + 
76                                              sum[i][1] - sum[l][1] + 
77                                              sum[j][2] - sum[l][2]);
78                 }
79         put(f[n][n][K]);
80     } 
81 }
82 
83 int main(){
84 //    file();
85     readdata();
86     work();
87     return 0;
88 }
O(n^3*k)

 

Guess you like

Origin www.cnblogs.com/Mandy-H-Y/p/11470109.html