-
G - Minimax
- Gym - 101972G
- 题意:Your goal is to delete a row x (1 < x < n) and a column y (1 < y < m),
- after this the matrix will be divided into 4 parts. Let us define the beauty of each part as the maximum value inside it.
- 就是选定一个元素删除其所在行所在列把图分为四部分,(很明显不能在边界选择),求一个点分割后的四部分
- 每一部分的最大值,取出来得到四个,这四个求一下最大最小的差,让这个差尽可能的小
- 思路:这个点肯定是要枚举的,所以求最值的过程必须预处理好,然后就想到了从四个定点开始dp维护一个最大值
- 然后枚举删除点求最优解即可
-
#include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f #define maxn 555 int a1[maxn][maxn],t; int a2[maxn][maxn],n; int a3[maxn][maxn],m; int a4[maxn][maxn]; int mmp[maxn][maxn]; int main() { scanf("%d",&t); while(t--) { int ans=inf; memset(mmp,0,sizeof(mmp)); memset(a1,0,sizeof(a1)); memset(a2,0,sizeof(a2)); memset(a3,0,sizeof(a3)); memset(a4,0,sizeof(a4)); scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { scanf("%d",&mmp[i][j]); a1[i][j]=a2[i][j]=a3[i][j]=a4[i][j]=mmp[i][j]; } for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) a1[i][j]=max(max(a1[i-1][j],a1[i][j-1]),a1[i][j]); for(int i=n; i>=1; i--) for(int j=1; j<=m; j++) a2[i][j]=max(max(a2[i+1][j],a2[i][j-1]),a2[i][j]); for(int i=1; i<=n; i++) for(int j=m; j>=1; j--) a3[i][j]=max(max(a3[i-1][j],a3[i][j+1]),a3[i][j]); for(int i=n; i>=1; i--) for(int j=m; j>=1; j--) a4[i][j]=max(max(a4[i+1][j],a4[i][j+1]),a4[i][j]); for(int i=2; i<n; i++) { for(int j=2; j<m; j++) { int ma=0,mi=inf; ma=max(ma,a1[i-1][j-1]); ma=max(ma,a2[i+1][j-1]); ma=max(ma,a3[i-1][j+1]); ma=max(ma,a4[i+1][j+1]); mi=min(mi,a1[i-1][j-1]); mi=min(mi,a2[i+1][j-1]); mi=min(mi,a3[i-1][j+1]); mi=min(mi,a4[i+1][j+1]); ans=min(ans,ma-mi); } } printf("%d\n",ans); } return 0; }
G - Minimax Gym - 101972G -dp-维护子矩阵最值
最新推荐文章于 2021-07-13 23:27:05 发布