Animal Protection题解

Animal Protection

https://ac.nowcoder.com/acm/contest/5929/F

这个主要是为了收获更多粉丝,我会认真的讲下单调栈和如何处理这个问题的...
下面贴上ac代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
const int N=1e3+5;
char a[N][N];
int cnt[N][N];
int stk[N];
int main()
{
    ll ans=0;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
        ll sum=0,pos=0;
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]=='O') cnt[i][j]=cnt[i-1][j]+1;
            sum+=cnt[i][j];
            while(pos&&cnt[i][stk[pos]]>cnt[i][j])
            {
                sum-=(stk[pos]-stk[pos-1])*(cnt[i][stk[pos]]-cnt[i][j]);
                pos--;
            }
            stk[++pos]=j;
            sum%=mod;
            ans=(ans+sum)%mod;
        }
    }
    cout<<ans<<endl;
    return 0;
}
lpt的小屋 文章被收录于专栏

我想要一份甜甜的爱情

全部评论

相关推荐

10-22 19:44
门头沟学院 Java
面了100年面试不知...:那我得去剪个头
点赞 评论 收藏
分享
10-13 13:49
南京大学 财务
饿魔:笑死我了,你简直是个天才
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务