Fzu2056 maximum square (second answer)

Source: Internet
Author: User
Maximum problem 2056 SquareAccept: 171 submit: 516
Time Limit: 1000 msec memory limit: 32768 kb Problem description now has an N * M matrix A. Find an H * H square in, make it have the largest area and the sum of the square element is not greater than limit. Input

The first line is an integer T, indicating that there are T groups of data.

Three non-negative integers n m limit in the first row of each group of data

Next, N rows. Each row has m integers.

 

0 <n <= 1000, 0 <m <= 1000, 0 <= Limit <= 100000000 0 <= A [I] <= 1000

Output outputs H * h for each group of data. Sample input22 2 21 11 12 2 41 11 1 sample output14
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;typedef pair<int,int> pii;const int INF = 1e9;const double eps = 1e-6;const int N = 1010;int cas = 1;int n,m;ll w[N][N],sum[N][N],limit;void pre(){    memset(sum,0,sizeof(sum));    scanf("%d%d%I64d",&n,&m,&limit);    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        {            scanf("%I64d",&w[i][j]);            sum[i][j]=sum[i-1][j]+w[i][j];        }}bool judge(int h){    ll s;    for(int r=1;r+h-1<=n;r++)    {        s=0;        for(int c=1;c<=h;c++)            s+=sum[r+h-1][c]-sum[r-1][c];        if(s<=limit) return 1;        for(int c=1;c+h<=m;c++)        {            s-=sum[r+h-1][c]-sum[r-1][c];            s+=sum[r+h-1][c+h]-sum[r-1][c+h];            if(s<=limit) return 1;        }    }    return 0;}int solve(int l,int r){    int ans = l;    while(l<=r)    {        int mid = (l+r)/2;        if(judge(mid)) ans=mid,l=mid+1;        else r=mid-1;    }    return ans*ans;}void run(){    pre();    printf("%d\n",solve(0,min(n,m)));}int main(){    #ifdef LOCAL    freopen("case.txt","r",stdin);    #endif    int _;    scanf("%d",&_);    while(_--)        run();    return 0;}

 

Fzu2056 maximum square (second answer)

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.