SPOJ 3734: Periodni 笛卡尔树 树形dp 组合数学

PERIODNI - Periodni

Luka is bored in chemistry class so he is staring at a large periodic table of chemical elements hanging from a wall above the blackboard. To kill time, Luka decided to make his own table completely different from the one in the classroom.

His table consists of N columns, each with some height, aligned at the bottom (see example below). After he draws the table he needs to fill it with elements. He first decided to enter the noble gases of which there are K. Luka must put them in the table so that no two noble gases are close to each other.

Two squares in the table are close to each other if they are in the same column or row, and all squares between them exist. In the example below, the 'a' squares are not close, but the 'b' squares are.

Write a program that, given N, K and the heights of the N columns, calculates the total number of ways for Luka to place the noble gases into the table. This number can be large, so output it modulo 1 000 000 007.

Input

The first line contains the integers N and K separated by a space (1 ≤ N ≤ 500, 1 ≤ K ≤ 500), the number of columns in Luka's table and the number of noble gases.

The next line contains N positive integers, separated by spaces. These are heights of the columns from left to right. The heights will be at most 1 000 000.

Output

Output the number of ways for Luka to fill his table with noble gases, modulo 1 000 000 007.

Example

Input:
5 2
2 3 1 2 4

Output:
43

这可真是一道好题


我们发现啊 存在参差不齐的情况不好做 而矩形就相对简单

对于一个 n*m 的矩形 在里面放 k 个 noble gases 的方案数是

之后我们思考怎么把这个参差不齐都东西搞掉

倒过来看,可以发现

每次在最上面割掉一个极大子矩形可以形成一个树形结构

那么 我们就可以树形dp辣

这个过程我们可以用笛卡尔树

// 肯定也可以不用...但肯定没这个好写

令列数满足平衡树性质、列的高度满足堆性质

f[i][j] 表示 i 的子树里放 j 个 noble gases 的方案数

每次转移先令该矩形内不放 noble gases 乘法原理把子树一乘

之后再枚举该矩形放了几个就行啦


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=10*x+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=510,mod=int(1e9)+7,M=1001000;

int fac[M],inv[M];

void initial()
{
	register int i;
	fac[0]=1;for(i=1;i<M;++i) fac[i]=1ll*i*fac[i-1]%mod;
	inv[1]=1;for(i=2;i<M;++i) inv[i]=1ll*inv[mod%i]*(mod-mod/i)%mod;
	inv[0]=1;for(i=1;i<M;++i) inv[i]=1ll*inv[i-1]*inv[i]%mod;
}

inline int C(int n,int m)
{return m>n ? 0 : 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;}

int n,K,h[N];
int root,st[N],fa[N],ch[N][2];

int f[N][N],size[N];

void dfs(int u)
{
	int column(h[u]-h[fa[u]]);
	size[u]=f[u][0]=1;
	for(int i=0,j,k,v;i<2;++i)
		if(v=ch[u][i])
		{
			dfs(v);
			for(j=min(K,size[u]+size[v]);~j;--j)
				for(k=1;k<=min(size[v],j);++k)
					(f[u][j]+=1ll*f[v][k]*f[u][j-k]%mod)%=mod;
			size[u]+=size[v];
		}
	for(int i=min(K,size[u]),j,tmp;~i;--i)
	{
		for(j=1,tmp=f[u][i];j<=min(i,column);++j)
			(tmp+=1ll*f[u][i-j]*fac[j]%mod*C(column,j)%mod*C(size[u]-i+j,j)%mod)%=mod;
		f[u][i]=tmp;
	}
}

int main()
{
	initial();
	n=read();K=read();
	register int i,top(0),tmp,rs;
	for(i=1;i<=n;++i) h[i]=read();
	for(i=1;i<=n;++i)
	{
		while(top && h[st[top]]>h[i]) top--;
		tmp=st[top];rs=ch[tmp][1];
		fa[i]=tmp;ch[tmp][1]=i;
		fa[rs]=i;ch[i][0]=rs;
		st[++top]=i;
	}
	root=st[1];dfs(root);
	cout<<f[root][K]<<endl;
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值