2020 ICPC·Xiaomi Online Trial Game J Questions

Topic link

Link: https://ac.nowcoder.com/acm/contest/7501/J
Source: Niuke

Topic: Matrix Subtraction
Analysis: Medium-difficulty problems,
you can use difference to solve, you can also directly set the template of a two-dimensional tree array, or you can use dp to solve.
Note, reduce from the beginning to the end, pay attention to the processing when the rectangle crosses the boundary.

AC code:

Insert picture description here

#include <cstdio>
#include <iostream>

using namespace std;

const int N = 1001;

inline int getInt(void)
{
    
    
    char c=getchar();
    int ret=0;
    while(c<'0' && c>'9')
    {
    
    
        c=getchar();
    }
    while(c>='0' && c<='9')
    {
    
    
        ret=ret*10+c-'0';
        c=getchar();
    }
    return ret;
}

int f[N][N],g[N][N];

inline int getsum(int x0,int y0,int x1,int y1)
{
    
    
    return g[x1][y1]-g[x0-1][y1]-g[x1][y0-1]+g[x0-1][y0-1];
}

bool solve(void)
{
    
    
    int n=getInt(),m=getInt(),a=getInt(),b=getInt();
    for(int i=1;i<=n;++i)
    {
    
    
        for(int j=1;j<=m;++j)
        {
    
    
            f[i][j]=getInt();

        }
    }
    for(int i=1;i<=n;++i)
    {
    
    
        for(int j=1;j<=m;++j)
        {
    
    
            g[i][j]=g[i][j-1]+g[i-1][j]-g[i-1][j-1];
            f[i][j]-=getsum(max(1,i-a+1),max(1,j-b+1),i,j);
            if(f[i][j]<0)
                return false;
            if((i+a-1>n || j+b-1>m) && f[i][j]>0)
            {
    
    
                return false;
            }
            g[i][j]+=f[i][j];
        }
    }
    return true;
}


int main()
{
    
    
    for(int i=getInt();i;--i)
    {
    
    
        if(solve())
        {
    
    
            puts("^_^");
        }
        else
        {
    
    
            puts("QAQ");
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_41563270/article/details/109333175