Observing the data range, N*m is relatively small, so we pre-process the prefix and.
Then we can consider writing a function to calculate the two-dimensional prefix and (two-dimensional prefix and everyone will be the bar qwq, then I will not say, is to pay attention to which is the horizontal axis which is the longitudinal shaft)
Then the two-dimensional binary position, then check to see if the character does not meet the two-point ans.
- Note: Half of the problem of maximizing the minimum and minimizing the maximum value can be thought of in two. We can convert the optimization problem to two-point later check into a judgment question.
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include < Algorithm> #define LL Long long#define maxn 550using namespace Std;int n,m,lx,rx,ly,ry,aa,bb;ll SUM[MAXN][MAXN]; BOOL Calc (int lx,int down,int rx,int up,int num) {return (sum[rx][up]-sum[rx][down-1]-sum[lx-1][up]+sum[lx-1][down-1]) >=num;} BOOL Check2 (int num) {bool Yes=1;ly=ry=1; for (int j=1;j<=bb;j++) {while (Ry+1<=m&&!calc (lx,ly,rx,ry,num)) ry++; if (!calc (Lx,ly,rx,ry,num)) {yes=0;break;} Ly=++ry; } return yes; BOOL Check (int num) {bool flag=1; Lx=1,rx=1; for (int. i=1;i<=aa;i++) {while (Rx+1<=n&&!check2 (num)) rx++; if (!check2 (num)) {flag=0;break;} Lx=++rx; } if (flag==1) return true; else return false;} int main () {freopen ("champion.in", "R", stdin); Freopen ("Champion.out", "w", stdout); scanf ("%d%d%d%d", &N,&M,&AA,&BB); for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {int cur; scanf ("%d", &cur); Sum[i][j]=sum[i][j-1]+cur; } for (int j=1;j<=m;j++) SUM[I][J]+=SUM[I-1][J]; }//prefix and long long l=0,r=sum[n][m],mid; Two points can be taken to the answer QWQ while (l<r) {mid= (l+r) >>1; if (check (mid)) l=mid+1; else R=mid; } printf ("%lld\n", l-1); return 0;}
Cut the cake (two-dimensional prefix and)