首页 > 其他 > 详细

I. Sequence 题解(思维+矩阵内全0正方形的数量)

时间:2021-08-16 22:43:38      阅读:25      评论:0      收藏:0      [点我收藏+]

题目链接

题目思路

看起来很难,其实转换问题就是个矩阵内全0正方形的数量

代码

#include<bits/stdc++.h>
#define fi first
#define se second
#define debug cout<<"I AM HERE"<<endl;
using namespace std;
typedef long long ll;
const int maxn=5e3+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-6;
int n,m;
int a[maxn][maxn];
int h[maxn][maxn];
int l[maxn];
int dp[maxn];
signed main(){
    scanf("%d%d",&n,&m);
    for(int i=1,u,v;i<=m;i++){
        scanf("%d%d",&u,&v);
        a[u][v]=1;
    }
    for(int j=1;j<=n;j++){
        for(int i=1;i<=n;i++){
            if(a[i][j]){
                h[i][j]=0;
            }else{
                h[i][j]=h[i-1][j]+1;
            }
        }
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        stack<int> sta;
        for(int j=1;j<=n;j++){
            while(!sta.empty()&&h[i][sta.top()]>=h[i][j]){
                sta.pop();
            }
            l[j]=sta.empty()?0:sta.top();
            sta.push(j);
            dp[j]=(j-l[j])*h[i][j]+dp[l[j]];
            ans=ans+dp[j];
        }
    }
    printf("%lld\n",ans);
    return 0;
}


I. Sequence 题解(思维+矩阵内全0正方形的数量)

原文:https://www.cnblogs.com/hunxuewangzi/p/15150184.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!