【bzoj4352】 Tower

来源:互联网 发布:adobe br是什么软件 编辑:程序博客网 时间:2024/05/01 22:55

  这不是sb题么我还想了这么久。。。TAT
  先把长度都排个序
  然后考虑插入第i个
  显然前面i个都可以插在它上面
  然后就是要考虑前面有哪些可以放在它下面,也就是Aj+D>=Ai的j。随便就好了,然后答案乘上ij+1
  claris怎么做到的又快又短QAQ
  

#include <bits/stdc++.h>#define rep(i,a,b) for(int i=a;i<=b;i++)#define per(i,a,b) for(int i=a;i>=b;i--)#define maxn 700007inline int rd() {    char c = getchar();    while (!isdigit(c) && c != '-') c = getchar() ; int x = 0 , f = 1;    if (c == '-') f = -1 ; else x = c - '0';    while (isdigit(c = getchar())) x = x * 10 + c - '0';    return x * f;}typedef long long ll;const int mod = 998244353;int n , d , a[maxn];void input() {    n = rd() , d = rd();    rep (i , 1 , n) a[i] = rd();    std::sort(a + 1 , a + n + 1);}void solve() {    int ans = 1;    for (int i = 2 , j = 1 , *t = a + 2 ; i <= n ; i ++ , t ++) {        for (; a[j] + d < *t ; j ++) ;        ans = (ll) ans * (i - j + 1) % mod;    }    printf("%d\n" , ans);}int main() {    input();    solve();    return 0;}
0 0