BZOJ2013 [Ceoi2010]A huge tower 乱搞

来源:互联网 发布:名不虚传软件官网 编辑:程序博客网 时间:2024/05/20 18:16

题意:n块砖各有宽度,A在B的上边当且仅当A不比B的宽度+D长,求放在一起方案数

Sol:

先排序,考虑从小到大一个个插入

因为当前插入的一定最大,比她小的一定能在它上面,只需考虑她能放在谁上面

显然可以放在底下,此外还有一段连续区间可以放

那么可以每次二分区间左端点然后乘法原理

发现左端点是单调不降的,可以维护指针扫过去,复杂度瓶颈在于排序

如果采取更优秀的排序算法可以做到线性时间复杂度

Code:

#include<bits/stdc++.h>using namespace std;const int maxn = 700009;const int mod = 1e9+9;int a[maxn];int n,d,ans;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 main(){n=read();d=read();for(int i=1;i<=n;i++) a[i]=read();sort(a+1,a+1+n);ans=1;int pos=1;for(int i=2;i<=n;i++){while(a[pos]<a[i]-d) pos++;ans=1ll*ans*(i-pos+1)%mod;}cout<<ans<<endl;return 0;}


原创粉丝点击