agc026_d

· · 题解

思路

考虑相邻两列的相接处,仅有如下几种可能:

考虑记录当前列状态:已经考虑了前 n 列,第 n 列当前两种颜色交替分布一直到高度 m,且更高部分不合法的方案数。

假设这样的方案数为 f_{n,m},其中 1\le m\le h_n。

然后转移时直接枚举下一项的状态:是和自己相同,还是相反。

如果 h_{n+1}\le h_n,则

f_{n+1,m}\leftarrow f_{n,m}\quad(1\le m<h_{n+1}) f_{n+1,h_{n+1}}\leftarrow2f_{n,m}\quad(h_{n+1}\le m\le h_n)

如果 h_{n+1}>h_n,则

f_{n+1,m}\leftarrow2^{h_{n+1}-h_n}f_{n,m}\quad(1\le m<h_n) f_{n+1,m}\leftarrow2^{h_{n+1}-m}f_{n,h_n}\quad(h_n\le m<h_{n+1}) f_{n+1,h_{m+1}}\leftarrow2f_{n,h_n}

初始值 h_0=1,f_{0,1}=1 即可。

直接 dp 的复杂度是 O(nh) 的,无法通过。

考虑本题的特殊性质:n 很小。

考虑直接对值域分段 dp,我们预先排序,记录下每个区间 [h,H) 对应的 dp 值之和即可。

等比数列赋值的部分可以预处理系数。

总复杂度 O(n^2+n\log v)。

使用线段树维护即可做到 O(n\log n)。

Code

const ullt Mod=1e9+7,inv2=5e8+4;
typedef ConstMod::mod_ullt<Mod>modint;
typedef std::vector<modint>modvec;
uint H[105],S[105];
modint Pow[105],PowInv[105],Dp[105],D[105];
int main()
{
#ifdef MYEE
    freopen("QAQ.in","r",stdin);
    freopen("QAQ.out","w",stdout);
#endif
    uint n,m;scanf("%u",&n),S[n]=1;
    for(uint i=0;i<n;i++)scanf("%u",H+i),S[i]=H[i];
    std::sort(S,S+n+1),m=std::unique(S,S+n+1)-S;
    for(uint i=0;i<m;i++)Pow[i]=modint(2)^S[i],PowInv[i]=modint(inv2)^S[i];
    for(uint i=0;i+1<m;i++)D[i]=2*(PowInv[i]-PowInv[i+1]);
    Dp[0]=1;
    uint tp=0;
    for(uint i=0;i<n;i++){
        uint h=std::lower_bound(S,S+m,H[i])-S;
        if(h<=tp)
        {
            for(uint i=h+1;i<=tp;i++)Dp[h]+=Dp[i];
            Dp[h]*=2;
        }
        else
        {
            modint v=Dp[tp];
            Dp[h]=v*2;
            for(uint i=tp;i<h;i++)Dp[i]=D[i]*Pow[h]*v;
            v=Pow[h]*PowInv[tp];
            for(uint i=0;i<tp;i++)Dp[i]*=v;
        }
        tp=h;
    }
    modint ans;
    for(uint i=0;i<=tp;i++)ans+=Dp[i];
    ans.println();
    return 0;
}