【bzoj2013】[Ceoi2010]A huge tower

来源:互联网 发布:微信网页授权 多域名 编辑:程序博客网 时间:2024/06/07 19:53

Description

有N(2<=N<=620000)快砖,要搭一个N层的塔,要求:如果砖A在砖B上面,那么A不能比B的长度+D要长。问有几种方法,输出 答案 mod 1000000009的值
Input

第一行: N,D 第二行: N个数,表示每块砖的长度。
Output

方案数。输出要mod1000000009
Sample Input

4 1

1 2 3 100

Sample Output

4

题解
考虑从小到大放入积木,i能放在j上满足a[i]-d<=a[j],维护处满足这个条件的个数,运用乘法原理计算答案。

代码

#include<bits/stdc++.h>#define mod 1000000009#define inf 10000000#define N 1000105#define pa pair<long long,int>typedef long long ll;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>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int n,d,ans,a[620005];int main(){    n=read();d=read();    for (int i=1;i<=n;i++) a[i]=read();    sort(a+1,a+n+1);    int p=1;ans=1;    for (int i=2;i<=n;i++)    {        while (a[p]+d<a[i]) p++;        ans=(ll)ans*(i-p+1)%mod;    }    printf("%d",ans);    return 0;}
原创粉丝点击