【笛卡尔树+DP】【SP3734】 PERIODNI - Periodni

题目

题目描述
English VietnameseLuka 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.

输入格式
The first line contains the integers N and K separated by a space (1

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 the number of ways for Luka to fill his table with noble gases, modulo 1 000 000 007.

题意翻译
给定一个N列的表格,每列的高度各不相同,但底部对齐,然后向表格中填入K个相同的数,填写时要求不能有两个数在同一列,或同一行,下图中b是错误的填写,a是正确的填写,因为两个a虽然在同一行,但它们中间的表格断开。

输出所有填写方案数对1 000 000 007的余数。

输入: 第一行两个整数 N 和 K (1 ≤ N ≤ 500, 1 ≤ K ≤ 500),表示表格的列数和要填写的数的个数。 接下来一行N个数,表示每列的高度。高度不超过 1 000 000.

输出: 一个整数,方案总数对1000 000 007的余数。

注意: 对于 40% 的数据, 所有数值小于15. 对于70% 的数据,所有数值小于100.

Translated by @yijan

输入输出样例
输入 #1复制
5 2
2 3 1 2 4
输出 #1复制
43

思路

显然是先建出小根笛卡尔树,考虑每个矩形内部的答案。
d p [ u ] [ i ] dp[u][i] dp[u][i] 表示 u u u 子树内放 i i i 个数的方案数, d p 1 [ i ] dp1[i] dp1[i] 表示 当前子树 u u u 内不考虑当前矩形,放 i i i 个数的方案数,设 H [ i ] H[i] H[i] 为当前矩阵可行高度(即 A [ u ] − A [ f a [ u ] ] A[u]-A[fa[u]] A[u]A[fa[u]] )。
显然有 d p 1 [ ] = f [ l s ] ∗ f [ r s ] dp1[] = f[ls]*f[rs] dp1[]=f[ls]f[rs] ,即左右子树的卷积。
接下来就是背包的转移了,枚举当前矩形内有多少列还是空的进行转移。
设当前子树放置 i i i 个棋子,有 j j j 个在当前矩阵放置。
d p [ u ] [ i ] + = ∑ j = 0 i d p 1 [ i − j ] ∗ C ( S z [ u ] − ( i − j ) , j ) ∗ C ( H [ x ] , j ) ∗ j ! dp[u][i]+=\sum_{j=0}^idp1[i-j]*C(Sz[u]-(i-j),j)*C(H[x],j)*j! dp[u][i]+=j=0idp1[ij]C(Sz[u](ij),j)C(H[x],j)j!
第一个组合数是枚举矩阵所剩的行,第二个组合数是枚举矩阵所剩的列。
最后乘上 j! 是因为横纵坐标是两两组合的,因此匹配的方案数为 j!。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define N 5077
#define mod 1000000007
ll n,k,top,root;
ll ls[N],rs[N],st[N];
ll f[N][N],h[N],siz[N],fac[1000050],inv[1000050];
ll qp(ll x,ll y)
{
    ll ans=1;
    while(y)
    {
        if(y&1) ans=(ans*x)%mod;
        x=(x*x)%mod;
        y>>=1;
    }
    return ans%mod;
}
void init()
{
    fac[0]=fac[1]=inv[0]=inv[1]=1;
	fac[2]=2,inv[2]=qp(2,mod-2);
	for(int i=3;i<=1000000;++i)
	{
		fac[i]=(fac[i-1]*i)%mod;
		inv[i]=qp(fac[i],mod-2);
	}
}
ll C(ll n,ll m)
{
    if(n<m) return 0;
    return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int build()
{
    for(int i=1;i<=n;++i)
    {
        while(top&&h[st[top]]>h[i]) ls[i]=st[top--];
        if(top) rs[st[top]]=i;
        st[++top]=i;
    }
    return st[1];
}
void dfs(int x,int val)
{
    f[x][0]=siz[x]=1;
    ll high=h[x]-val;
    if(ls[x])
    {
        ll y=ls[x];
        dfs(y,h[x]),siz[x]+=siz[y];
        for(ll i=min(siz[x],k);i>=0;--i)
            for(ll j=1;j<=min(siz[y],i);++j)
                f[x][i]=(f[x][i]+f[y][j]*f[x][i-j]%mod)%mod;
    }
    if(rs[x])
    {
        ll y=rs[x];
        dfs(y,h[x]),siz[x]+=siz[y];
        for(ll i=min(siz[x],k);i>=0;--i)
            for(ll j=1;j<=min(siz[y],i);++j)
                f[x][i]=(f[x][i]+f[y][j]*f[x][i-j]%mod)%mod;
    }
    for(ll i=min(siz[x],k);i>=0;--i)
        for(ll j=1;j<=min(high,i);++j)
            f[x][i]=(f[x][i]+f[x][i-j]*fac[j]%mod*C(high,j)%mod*C(siz[x]-(i-j),j)%mod)%mod;
}
int main()
{
    init();
    read(n),read(k);
    for(int i=1;i<=n;++i) read(h[i]);
    root=build();
    dfs(root,0);
    printf("%lld",f[root][k]);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值