考虑一个类似笛卡尔树的 dp 顺序,先确定下面那个公有的大矩形然后递归处理问题。 中 以下的部分已经被钦定了( 是 这种东西)且对上面的染色没有影响,设这个方案数为 ,假设 是区间 中严格最矮的且 (如果没有就递归到有)。我们先钦定 这一列高度为 的格子的颜色,然后考虑 中除了 别的列高度为 这部分(下文称之为染色地方)的染色,分类讨论:
-
如果存在 , 的钦定方案数为 , 颜色固定,推下去要染色的地方都被确定了,且相邻两位顶的颜色相异(),设 表示 以上的部分在钦定了 的颜色且保证钦定方式为相邻两列的钦定不同的情况下的方案数,还要乘子问题 和 。
-
如果不存在 , 的钦定方案数为 ,此时可以随便钦定 ,剩下的部分推下去可以被确定,发现是子问题 和 。
现在问题只剩 怎么求了,设 是 中 最小的,满足 不然递归到满足,任意钦定 的颜色别的可以推出,得到子问题 和 。哦 和 的边界是 。
#include<bits/stdc++.h>
#define int long long
#define up(i,l,r) for(int i=l; i<=r; ++i)
#define dn(i,r,l) for(int i=r; i>=l; --i)
using namespace std;
const int N=105, P=1e9+7;
int ksm(int a,int b) {
int ret=1;
for( ; b; b>>=1) {
if(b&1) ret=ret*a%P;
a=a*a%P;
}
return ret;
}
int n, h[N];
int ask(int l,int r) {
int ret=0;
up(i,l,r) if(!ret||h[i]<h[ret]) ret=i;
return ret;
}
int g(int l,int r,int delta) {
if(l>r) return 1;
int p=ask(l,r), ret=g(l,p-1,h[p])*g(p+1,r,h[p])%P;
if(h[p]-delta>0) ret=ksm(2,h[p]-delta)*ret%P;
return ret;
}
int f(int l,int r,int delta) {
if(l>r) return 1;
int p=ask(l,r), ret=f(l,p-1,h[p]-1)*f(p+1,r,h[p]-1)%P;
if(h[p]-delta>0) ret=2*ret%P;
if(h[p]-delta>1) {
int bur=(ksm(2,h[p]-delta)-2)%P;
bur=g(l,p-1,h[p])*g(p+1,r,h[p])%P*bur%P;
ret=(ret+bur)%P;
}
return ret;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
up(i,1,n) cin >> h[i];
cout << (f(1,n,0)%P+P)%P;
return 0;
}