[Vijos] 1764 dual matrices (DP)

Source: Internet
Author: User

Https://vijos.org/p/1764

Since the mentality is much better, it is really easy to do questions.

This type of question directly considers how much space I can get from the current one.

Obviously, we enumerate each vertex and take out a rectangle (This vertex is used as the bottom right corner). Then we only need to consider the remaining space I + 1 ~ N and J + 1 ~ M space (as for why Qaq)

Therefore, we maintain the I + 1 ~ N and J + 1 ~ The maximum rectangle that m can obtain.

Obviously, the two-dimensional prefix and, then maintain a two-dimensional

MX [I] [J] = max {Get (I, j), MX [I + 1] [J], MX [I] [J + 1]}, then, each time you find a vertex I, you only need to add max {MX [I + 1] [1], MX [1] [J + 1]} to J.

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=1005, oo=~0u>>2;int arr[N][N], sum[2][N][N], mx[N][N], n, m, a, b, ans=-oo;int get(int x, int y, int k) {int ret=-oo, fx=x-(k*a)+k, fy=y-(k*b)+k, now=(k==1?0:1);if(fx>=1 && fy>=1 && fx<=n && fy<=m) ret=max(ret, sum[now][x][y]-sum[now][fx-k][y]-sum[now][x][fy-k]+sum[now][fx-k][fy-k]);fx=x-(k*b)+k, fy=y-(k*a)+k;if(fx>=1 && fy>=1 && fx<=n && fy<=m) ret=max(ret, sum[now][x][y]-sum[now][fx-k][y]-sum[now][x][fy-k]+sum[now][fx-k][fy-k]);return ret;}int main() {read(n); read(m); read(a); read(b);for1(i, 1, n) for1(j, 1, m) read(arr[i][j]);for1(i, 1, n) for1(j, 1, m) sum[0][i][j]=sum[0][i-1][j]+sum[0][i][j-1]-sum[0][i-1][j-1]+arr[i][j];for3(i, n, 1) for3(j, m, 1) sum[1][i][j]=sum[1][i+1][j]+sum[1][i][j+1]-sum[1][i+1][j+1]+arr[i][j];for3(i, n+1, 0) for3(j, m+1, 0) mx[i][j]=-oo;for3(i, n, 1) for3(j, m, 1) mx[i][j]=max(get(i, j, -1), max(mx[i][j+1], mx[i+1][j]));for1(i, 1, n) for1(j, 1, m) ans=max(ans, get(i, j, 1)+max(mx[i+1][1], mx[1][j+1]));if(ans==-oo) puts("Impossible");else print(ans);return 0;}

 

 

 

 

Description

A two-dimensional matrix of N rows and M columns. Each position of the matrix is an integer whose absolute values do not exceed 1000.

You need to find two non-Intersecting a * B rectangles so that the sum of the elements contained in the two rectangles is as large as possible.

Note: The rectangle of a * B Refers to the Child matrix of Row A and column B, or the child matrix of Row B and column. A non-Intersecting rectangle indicates that two rectangles have no common elements.

Format input format

The first line contains four positive integers n, m, A, and B, which are separated by spaces.

In the following n rows, each row has m integers separated by spaces, which represent each element of the Two-dimensional matrix in sequence.

Output Format

Output a row with an integer representing the answer. If two non-Intersecting a * B rectangles are not found, the output is "impossible ".

Example 1 input 1 [copy]
3 4 1 21-1 3 4-1 9-1 19 8 5 2
Sample output 1 [copy]
25
Restrictions

1 s

Prompt

30% of data is satisfied, 1 <= n, m <= 50.
In addition, 30% of the data meets the requirements. A = B.
100% of data is satisfied, 1 <= n, m <= 1 000, 1 <= A, B <= n.

[Vijos] 1764 dual matrices (DP)

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.