【BZOJ4361】isn(DP,容斥)

Description

给出一个长度为n的序列A(A1,A2…AN)。如果序列A不是非降的,你必须从中删去一个数,这一操作,直到A非降为止。求有多少种不同的操作方案,答案模10^9+7。


Solution

在DYX的容斥小结中看到的题,感觉和容斥关系不大?

d p [ i ] [ j ] 表示 到第 i 位,序列长度为 j (一定取第 i 位)。对于每一个j以数值为下标建一个BIT优化转移。
f [ i ] 表示长度为 i 的序列有多少种,那么有 f [ i ] = d p [ j ] [ i ]
但题目中要求只要序列非降就停止操作,所以需要容斥一下:
A n s = f [ i ] × ( n i ) ! f [ i + 1 ] × ( n i 1 ) ! × ( i + 1 )


Code

/************************************************
 * Au: Hany01
 * Date: Jul 10th, 2018
 * Prob: BZOJ4361 isn
 * Email: [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 2005, Mod = 1e9 + 7;

int n, a[maxn], dp[maxn][maxn], f[maxn], Ans, ls[maxn], tot;
LL fac[maxn];

inline int ad(int x, int y) { if ((x += y) >= Mod) return x - Mod; return x; }

struct FenwickTree {
    int c[maxn];
#define lb(x) (x & -x)
    void update(int x, int dt) { for ( ; x <= tot; x += lb(x)) c[x] = ad(c[x], dt); }
    int query(int x) { static int ans; for (ans = 0; x; x -= lb(x)) ans = ad(ans, c[x]); return ans; }
}FT[maxn];

int main()
{
#ifdef hany01
    File("bzoj4361");
#endif

    n = read();
    For(i, 1, n) ls[i] = a[i] = read();
    sort(ls + 1, ls + 1 + n), tot = unique(ls + 1, ls + 1 + n) - ls - 1;
    For(i, 1, n) a[i] = lower_bound(ls + 1, ls + 1 + tot, a[i]) - ls;
    For(i, 1, n) {
        Fordown(j, i, 2) FT[j].update(a[i], dp[i][j] = FT[j - 1].query(a[i])), f[j] = ad(f[j], dp[i][j]);
        FT[1].update(a[i], dp[i][1] = 1), ++ f[1];
    }
    fac[0] = 1;
    For(i, 1, n) fac[i] = fac[i - 1] * i % Mod;
    For(i, 1, n) Ans = ad(Ans, ad(f[i] * fac[n - i] % Mod, Mod - f[i + 1] * fac[n - i - 1] % Mod * (LL)(i + 1) % Mod));
    printf("%d\n", Ans);

    return 0;
}
//蕙风如薰,甘露如醴。
//    -- 左思《三都赋》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/80985512
今日推荐