【BZOJ3235】好方的蛇(AHOI2013)-DP+单调栈+容斥

测试地址:好方的蛇
做法:本题需要用到DP+单调栈+容斥。
首先,容易想到预处理出某个顶点(左上、左下、右上、右下)为 (i,j) ( i , j ) 的合法矩形数目。怎么预处理呢?以处理右下顶点为 (i,j) ( i , j ) 的合法矩形数目为例,我们要数的实际上是合法的左上顶点的数目,我们发现这个数目可以这样计算:令 up(i,j) u p ( i , j ) 为从点 (i,j) ( i , j ) 向上最长的连续黑色段长度,则我们需要枚举左上端点所在列 k k ,累加min{up(i,p)|kpj}。发现这个东西可以用单调栈维护求出,因此我们就能 O(n2) O ( n 2 ) 处理出上述东西了。
然后就是如何求解的问题了。易知,两个不相交的矩形必定要么被一条横线分隔,要么被一条竖线分隔。那么我们对于每个合法的矩形,累加在它下边或右边的矩形数目即可,这显然可以用前缀和优化到 O(n2) O ( n 2 )
但这样有一个问题,那就是当两个矩形既能被一条横线分隔,又能被一条竖线分隔,那么这种情况会被算两次,我们要减去这种情况的贡献。于是我们枚举在上面的那个矩形,对每个矩形,减去右上顶点在这个矩形左下顶点的左下方的矩形数目,再减去左上顶点在这个矩形右下顶点的右下方的矩形数目即可。这也能用前缀和优化成 O(n2) O ( n 2 ) ,于是我们就在 O(n2) O ( n 2 ) 的时间内完成了这道题。
以下是本人代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=10007;
int n;
ll st[1010],h[1010],top;
ll up[1010][1010]={0},down[1010][1010]={0};
ll lu[1010][1010]={0},ld[1010][1010]={0},ru[1010][1010]={0},rd[1010][1010]={0};
char s[1010][1010];

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%s",&s[i][1]);

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if (s[i][j]=='B') up[i][j]=up[i-1][j]+1;
            else up[i][j]=0;
        }
    }
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=n;j++)
        {
            if (s[i][j]=='B') down[i][j]=down[i+1][j]+1;
            else down[i][j]=0;
        }
    }

    for(int i=1;i<=n;i++)
    {
        top=0;
        for(int j=1;j<=n;j++)
        {
            ll cnt=1,ans=lu[i][j-1]+up[i][j];
            while(top&&st[top]>up[i][j])
            {
                ans-=h[top]*(st[top]-up[i][j]);
                cnt+=h[top];
                top--;
            }
            if (!top||st[top]!=up[i][j]) st[++top]=up[i][j],h[top]=cnt;
            else h[top]+=cnt;
            lu[i][j]=ans;
        }
    }
    for(int i=1;i<=n;i++)
    {
        top=0;
        for(int j=n;j>=1;j--)
        {
            ll cnt=1,ans=ru[i][j+1]+up[i][j];
            while(top&&st[top]>up[i][j])
            {
                ans-=h[top]*(st[top]-up[i][j]);
                cnt+=h[top];
                top--;
            }
            if (!top||st[top]!=up[i][j]) st[++top]=up[i][j],h[top]=cnt;
            else h[top]+=cnt;
            ru[i][j]=ans;
        }
    }
    for(int i=n;i>=1;i--)
    {
        top=0;
        for(int j=1;j<=n;j++)
        {
            ll cnt=1,ans=ld[i][j-1]+down[i][j];
            while(top&&st[top]>down[i][j])
            {
                ans-=h[top]*(st[top]-down[i][j]);
                cnt+=h[top];
                top--;
            }
            if (!top||st[top]!=down[i][j]) st[++top]=down[i][j],h[top]=cnt;
            else h[top]+=cnt;
            ld[i][j]=ans;
        }
    }
    for(int i=n;i>=1;i--)
    {
        top=0;
        for(int j=n;j>=1;j--)
        {
            ll cnt=1,ans=rd[i][j+1]+down[i][j];
            while(top&&st[top]>down[i][j])
            {
                ans-=h[top]*(st[top]-down[i][j]);
                cnt+=h[top];
                top--;
            }
            if (!top||st[top]!=down[i][j]) st[++top]=down[i][j],h[top]=cnt;
            else h[top]+=cnt;
            rd[i][j]=ans;
        }
    }

    for(int i=n;i>=1;i--)
        for(int j=n;j>=1;j--)
        {
            if (rd[i][j]>0)
                rd[i][j]=rd[i+1][j]+rd[i][j+1]-rd[i+1][j+1]+rd[i][j]-1ll;
            else rd[i][j]=rd[i+1][j]+rd[i][j+1]-rd[i+1][j+1];
        }
    for(int i=n;i>=1;i--)
        for(int j=1;j<=n;j++)
        {
            if (ld[i][j]>0)
                ld[i][j]=ld[i+1][j]+ld[i][j-1]-ld[i+1][j-1]+ld[i][j]-1ll;
            else ld[i][j]=ld[i+1][j]+ld[i][j-1]-ld[i+1][j-1];
        }
    ll ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if (lu[i][j]>0) ans+=(lu[i][j]-1ll)*(rd[1][j+1]+rd[i+1][1]-rd[i+1][j+1]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if (ru[i][j]>0) ans-=(ru[i][j]-1ll)*ld[i+1][j-1];
    printf("%lld",ans%mod);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一道经典的单调栈问题。题目描述如下: 有 $n$ 个湖,第 $i$ 个湖有一个高度 $h_i$。现在要在这些湖之间挖一些沟渠,使得相邻的湖之间的高度差不超过 $d$。请问最少需要挖多少个沟渠。 这是一道单调栈的典型应用题。我们可以从左到右遍历湖的高度,同时使用一个单调栈来维护之前所有湖的高度。具体来说,我们维护一个单调递增的栈,栈中存储的是湖的下标。假设当前遍历到第 $i$ 个湖,我们需要在之前的湖中找到一个高度最接近 $h_i$ 且高度不超过 $h_i-d$ 的湖,然后从这个湖到第 $i$ 个湖之间挖一条沟渠。具体的实现可以参考下面的代码: ```c++ #include <cstdio> #include <stack> using namespace std; const int N = 100010; int n, d; int h[N]; stack<int> stk; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); int ans = 0; for (int i = 1; i <= n; i++) { while (!stk.empty() && h[stk.top()] <= h[i] - d) stk.pop(); if (!stk.empty()) ans++; stk.push(i); } printf("%d\n", ans); return 0; } ``` 这里的关键在于,当我们遍历到第 $i$ 个湖时,所有比 $h_i-d$ 小的湖都可以被舍弃,因为它们不可能成为第 $i$ 个湖的前驱。因此,我们可以不断地从栈顶弹出比 $h_i-d$ 小的湖,直到栈顶的湖高度大于 $h_i-d$,然后将 $i$ 入栈。这样,栈中存储的就是当前 $h_i$ 左边所有高度不超过 $h_i-d$ 的湖,栈顶元素就是最靠近 $h_i$ 且高度不超过 $h_i-d$ 的湖。如果栈不为空,说明找到了一个前驱湖,答案加一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值