Cut the cake (two-dimensional prefix and)

Source: Internet
Author: User

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 ("", "R", stdin);    Freopen ("Champion.out", "w", stdout); scanf ("%d%d%d%d", &AMP;N,&AMP;M,&AMP;AA,&AMP;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)

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: 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.