Question D: 30 appointments-UPC (prefix and)

Title description
Once upon a time, the little rabbit discovered a mysterious garden.

The garden is a matrix with n rows and m columns. The beauty of the flowers in the i-th row and j-column is ai, j. A legal dating place is any square sub-matrix. The romance of the sub-matrix is ​​defined as two sub-matrices. The sum of the beauty of the flowers on the diagonal.

Now Xiaotu wants to choose a dating place with an area equal to 1 to maximize the romance of the place, so as to date Xiaolu, because Xiaotu is busy with AKIOI, so she leaves this question to you.
Enter the
first line, two positive integers n, m.
Next is a matrix of n rows and m columns, representing the beauty of the flowers at each position.
The output
is only one line, a positive integer, indicating the maximum degree of romance.
Sample input Copy
3 3
2 -1 3
-4 2 1
1 2 -1
Sample output Copy
7
Prompt
For 40% of data, n,m≤10.
For 100% data, 1≤n, m≤300, ∣ai∣≤104.

The idea is that
if direct violence can pass, you and I will not meet here at this moment,
but it is no different from violence. Maintaining the prefix of the diagonal and
using the B array to maintain the prefix sum of the main diagonal, and using the C array to maintain the sub The prefix of the diagonal and
then enumerate the upper left corner of the point and the side length of the square,
then O(1) to get the degree of romance, and
finally take max

Code

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdio>
#include <bitset>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define UpMing main
#define re register
#pragma GCC optimize(2)
#define Accept return 0;
#define lowbit(x) ((x)&(-(x)))
#define mst(x, a) memset(x,a,sizeof(x))
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
const int inf =0x3f3f3f3f;
const int maxn=2e6+7;
const ll mod = 998244353;
const int N =1e6+7;
inline ll read() {
    
    
	ll  x=0;
	bool f=0;
	char ch=getchar();
	while (ch<'0'||'9'<ch)    f|=ch=='-', ch=getchar();
	while ('0'<=ch && ch<='9')
		x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}
void out(ll x) {
    
    
	int stackk[20];
	if(x<0) {
    
    
		putchar('-');
		x=-x;
	}
	if(!x) {
    
    
		putchar('0');
		return;
	}
	int top=0;
	while(x) stackk[++top]=x%10,x/=10;
	while(top) putchar(stackk[top--]+'0');
}
ll a[1333][1333],n,m,ans,b[1311][1313],c[1311][1313];
ll f(ll x,ll y,ll z) {
    
    
	if(z%2)
		return (b[x+z][y+z]-b[x-1][y-1])+(c[x+z][y]-c[x-1][y+z+1]);
	else
		return (b[x+z][y+z]-b[x-1][y-1])+(c[x+z][y]-c[x-1][y+z+1])-a[x+z/2][y+z/2];
}
int UpMing() {
    
    
	cin>>n>>m;
	for(int i=1 ; i<=n ; i++)
		for(int j=1 ; j<=m ; j++) {
    
    
			cin>>a[i][j];
			ans=max(ans,a[i][j]);
		}
	for(int i=1 ; i<=n ; i++)
		for(int j=1 ; j<=m ; j++) {
    
    
			for(int x=i,y=j; x<=n&&y<=m ; y++,x++)
				b[x][y]=b[x-1][y-1]+a[x][y];
		}
		
	for(int i=1 ; i<=n ; i++)
		for(int j=1 ; j<=m ; j++) {
    
    
			for(int x=i,y=j; x<=n&&y>=1 ; y--,x++)
				c[x][y]=c[x-1][y+1]+a[x][y];
		}

	for(int len=1 ; len<min(n,m); len++)
		for(int i=1 ; i<=n-len ; i++)
			for(int j=1 ; j<=m-len; j++)
				ans=max(ans,f(i,j,len));
				
	out(ans);
	Accept;
}

/*
3 3
2 -1 3
-4 2 1
1 2 -1

*/


Guess you like

Origin blog.csdn.net/wmy0536/article/details/106423207