LZDFDSMLL吃批萨(easy)

版权声明:来自星空计算机团队(QQ群:134826825)——StarSky_STZG https://blog.csdn.net/weixin_43272781/article/details/86548056

http://murphyc.fun/contest/5/problem/A

hard version:https://blog.csdn.net/weixin_43272781/article/details/86554432

Description

LZDFDSMLL最近收到了一个批萨,这个批萨可以表示成n行m列的矩形,已知这个批萨上有k块被吃掉了。

LZDFDSMLL一定要吃一块完整的正方形的批萨,请问他有多少种不同的批萨可以吃。

不同批萨的定义:

两个正方形批萨只要左上角的点不一样, 或者正方形批萨的边长不一样就算不同。

Input

第一行两个正数n,m, k, 分别表示矩阵的行数、矩阵的列数、被吃掉的块数。

接下来有k行,每行有两个数x, y, 表示在x行y列的那块批萨被吃掉了。

(1 <= n, m <= 500, k <= min(500, n*m))

Output

一个整数表示LZDFDSMLL有多少种不同的正方形批萨可以吃。

Sample Input 1 

3 3 0

Sample Output 1

14

Sample Input 2 

3 3 1
2 2

Sample Output 2

8

题解:

DP

动态规划,状转方程:

if (a[i][j]==1) f[i][j]=min(min(f[i][j-1],f[i-1][j]),f[i-1][j-1])+1;

说明:

f[i][j]表示以节点i,j为右下角,可构成的最大正方形的边长。

只有a[i][j]==1时,节点i,j才能作为正方形的右下角;

对于一个已经确定的f[i][j]=x,它表明包括节点i,j在内向上x个节点,向左x个节点扫过的正方形中所有a值都为1;

对于一个待确定的f[i][j],我们已知f[i-1][j],f[i][j-1],f[i-1][j-1]的值,如下:

f数组:

? ? ? ?

? ? 2 1

? ? 3 ?

? ? ? ?

则说明原a数组:

1 1 1 0

1 1 1 1

1 1 1 1

? ? ? ?

由此得出状态转移方程:

if (a[i][j]==1) f[i][j]=min(min(f[i][j-1],f[i-1][j]),f[i-1][j-1])+1;

for example:

a[i][j]:

0 0 0 1

1 1 1 1

0 1 1 1

1 1 1 1

f[i][j]:

0 0 0 1

1 1 1 1

0 1 2 2

1 1 2 3

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=5000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,x,y;
ll ans,cnt,flag,temp;
int a[N][N];
int l[N][N];
int c[N][N];
int dp[N][N];
char str;
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    scanf("%d%d%d",&n,&m,&k);
    //scanf("%d",&t);
    //while(t--){}
    for(int i=1;i<=k;i++){
        scanf("%d%d",&x,&y);
        a[x][y]=1;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(!a[i][j])
            dp[i][j]=min(min(dp[i][j-1],dp[i-1][j]),dp[i-1][j-1])+1;
            //cout<<dp[i][j]<<endl;
            ans+=dp[i][j];
        }
    }
    cout<<ans<<endl;

    //cout << "Hello world!" << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43272781/article/details/86548056