Popularity NOIP 2014 sub-matrices

Source: Internet
Author: User
Tags abs min

Title Description

The following definitions are given:

Sub-matrices: A new matrix that selects the intersection of some rows and some columns from a matrix (preserving the relative order of rows and columns) is called a sub-matrix of the original matrix.

For example, the left-hand image below selects the intersection of the 2nd, 4, and 2nd, 4, and 5 columns to get a 2*3 sub-matrix as shown in the image on the right.

9 3 3) 3 9

9 4 8) 7 4

1 7 4) 6 6

6 8 5) 6 9

7 4 5) 6 1

One of the 2*3 's sub-matrices is

4 7 4

8 6 9

Adjacent elements: an element in the matrix is adjacent to the left and right four elements of it, if one exists.
the value of the matrix: the sum of the absolute values of the difference between each pair of adjacent elements in the matrix.

Task: Given a positive integer matrix of n rows M column, you select a sub-matrix of the R row C column from this matrix, making the sub-matrix the smallest score and outputting this score.

(This topic is 2014NOIP popularization T4)
Input/output format
Input format:

The first line contains four integers separated by spaces n,m,r,c, meaning as described in the problem description, separated by a space between every two integers.

The next n rows, each containing an integer of M separated by spaces, are used to represent the matrix of the N-row m-column in the problem description.

Output format:

Output a total of 1 rows, containing 1 integers, representing the minimum score of the sub-matrix satisfying the topic description.

Input/Output sample
Input Sample # #:

5 5 2 3
9 3 3) 3 9
9 4 8) 7 4
1 7 4) 6 6
6 8 5) 6 9
7 4 5) 6 1

Sample # # of output:

6

Input Sample #:

7 7 3 3
7 7 7 6 2 10 5
5 8 8 2 1 6 2
2 9 5 5 6 1 7
7 9 3 6 1 7 8
1 9 1 4 7 8 8
10 5 9 1 1 8 10
1 3 1 5 4 8 6

Output Example #:

16

Description

"Input and Output Example 1 description"

The sub-matrices of the 2 rows and 3 columns with the smallest points in the matrix consist of the elements of the 4th row of the original matrix, the 5th row and the 1th column, the 3rd column, and the 4th column.

6 5 6

7 5 6

, its score is

|6−5| + |5−6| + |7−5| + |5−6| + |6−7| + |5−5| + |6−6| = 6.

"Input and Output Example 2 description"

The sub-matrices of the 3 rows and 3 columns with the smallest points in the matrix consist of the elements of the 4th, 5th, 6th and 2nd, 6th, and 7th columns of the original matrix, and the smallest sub-matrices of the selected scores are

9 7 8 9 8 8 5 8 10

"Data description"

For 50% of the data, 1≤n≤12,1≤m≤12, each element in the matrix is 1≤a[i][j]≤20;

For 100% of the data, 1≤n≤16,1≤m≤16, each element in the matrix is 1≤a[i][j]≤1,000,

1≤r≤n,1≤c≤m.

Analysis
Row with DFS enumeration, column with linear DP O (N^3)

Code

[Popularity] NOIP 2014 sub-matrix #include <cmath> #include <cstdio> #include <cstring> #include <iostream> # Include<algorithm> #define LL Long #define M (a) memset (a,0,sizeof a) #define FO (i,j,k) for (i=j;i<=k;i++) US
ing namespace std;
int n,m,r,c,ans=1e9; int dp[20][20];
I was selected, the last column is J's score int map[20][20],num[20],w[20][20];
    inline void dynamic () {int i,j,k;
    M (w);
    memset (dp,0x3f,sizeof DP);
        Fo (j,1,m) {dp[1][j]=0;
    Fo (i,2,r) dp[1][j]+=abs (Map[num[i]][j]-map[num[i-1]][j]);
    } fo (j,1,m) fo (k,j+1,m) fo (i,1,r) w[j][k]+=abs (Map[num[i]][j]-map[num[i]][k]);
    Fo (i,2,c) fo (j,i,m) fo (k,i-1,j-1) dp[i][j]=min (Dp[i][j],dp[i-1][k]+w[k][j]+dp[1][j]);
Fo (i,c,m) ans=min (Ans,dp[c][i]);
        } inline void Dfs (int dep) {if (dep>r) {dynamic ();
    Return
} for (int i=num[dep-1]+1;i<=n-(R-DEP); i++) Num[dep]=i,dfs (dep+1);
    } int main () {int i,j;
    scanf ("%d%d%d%d", &n,&m,&r,&c);
    Fo (i,1,n) fo (j,1,m) scanf ("%d", &map[i][j]);
    DFS (1);
    printf ("%d\n", ans);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.