結果

問題 No.992 最長増加部分列の数え上げ
ユーザー knshnbknshnb
提出日時 2020-02-14 23:31:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 3,830 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 216,420 KB
実行使用メモリ 16,144 KB
最終ジャッジ日時 2023-07-28 19:01:33
合計ジャッジ時間 6,316 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 23 ms
7,940 KB
testcase_05 AC 17 ms
6,732 KB
testcase_06 AC 29 ms
8,908 KB
testcase_07 AC 21 ms
7,652 KB
testcase_08 AC 12 ms
5,416 KB
testcase_09 AC 22 ms
7,580 KB
testcase_10 AC 28 ms
8,888 KB
testcase_11 AC 36 ms
10,528 KB
testcase_12 AC 8 ms
4,568 KB
testcase_13 AC 21 ms
7,496 KB
testcase_14 AC 21 ms
7,352 KB
testcase_15 AC 9 ms
4,620 KB
testcase_16 AC 53 ms
14,988 KB
testcase_17 AC 12 ms
5,360 KB
testcase_18 AC 20 ms
7,208 KB
testcase_19 AC 35 ms
10,492 KB
testcase_20 AC 59 ms
15,780 KB
testcase_21 AC 59 ms
15,780 KB
testcase_22 AC 58 ms
16,144 KB
testcase_23 AC 58 ms
15,912 KB
testcase_24 AC 59 ms
15,856 KB
testcase_25 AC 59 ms
16,112 KB
testcase_26 AC 58 ms
16,060 KB
testcase_27 AC 59 ms
16,104 KB
testcase_28 AC 60 ms
16,028 KB
testcase_29 AC 59 ms
16,096 KB
testcase_30 AC 43 ms
14,344 KB
testcase_31 AC 43 ms
14,260 KB
testcase_32 AC 43 ms
14,428 KB
testcase_33 AC 44 ms
14,412 KB
testcase_34 AC 44 ms
14,304 KB
testcase_35 AC 42 ms
15,656 KB
testcase_36 AC 42 ms
15,588 KB
testcase_37 AC 42 ms
15,584 KB
testcase_38 AC 42 ms
15,748 KB
testcase_39 AC 41 ms
15,648 KB
testcase_40 AC 44 ms
15,244 KB
testcase_41 AC 44 ms
15,120 KB
testcase_42 AC 44 ms
14,988 KB
testcase_43 AC 44 ms
15,120 KB
testcase_44 AC 44 ms
15,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>  // clang-format off
using namespace std;
using Int = long long;
#define REP2(i, n) for (Int i = 0, max_i = (n); i < max_i; i++)
#define REP3(i, a, b) for (Int i = (a), max_i = (b); i < max_i; i++)
#define OVERLOAD2(_1, _2, _3, name, ...) name
#define REP(...) OVERLOAD2(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
struct SetupIO { SetupIO() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Fri Feb 14 23:02:23 JST 2020
 **/

template <class T> T pow(T x, int n, const T UNION = 1) {
    T ret = UNION;
    while (n) {
        if (n & 1) ret *= x;
        x *= x;
        n >>= 1;
    }
    return ret;
}

template <int MD> struct ModInt {
    int x;
    static unordered_map<int, int> to_inv;
    ModInt() : x(0) {}
    ModInt(long long x_) {
        if ((x = x_ % MD + MD) >= MD) x -= MD;
    }

    ModInt& operator+=(ModInt that) {
        if ((x += that.x) >= MD) x -= MD;
        return *this;
    }
    ModInt& operator*=(ModInt that) {
        x = (unsigned long long)x * that.x % MD;
        return *this;
    }
    ModInt& operator-=(ModInt that) {
        if ((x -= that.x) < 0) x += MD;
        return *this;
    }
    ModInt& operator/=(ModInt that) {
        x = (unsigned long long)x * that.inv().x % MD;
        return *this;
    }
    ModInt operator-() const { return -x < 0 ? MD - x : -x; }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    bool operator<(ModInt that) const { return x < that.x; }
    ModInt inv() const { return to_inv.count(this->x) ? to_inv[this->x] : (to_inv[this->x] = pow(*this, MD - 2).x); }

    friend ostream& operator<<(ostream& s, ModInt<MD> a) {
        s << a.x;
        return s;
    }
    friend istream& operator>>(istream& s, ModInt<MD>& a) {
        long long tmp;
        s >> tmp;
        a = ModInt<MD>(tmp);
        return s;
    }
    friend string to_string(ModInt<MD> a) { return to_string(a.x); }
};
template <int MD> unordered_map<int, int> ModInt<MD>::to_inv;
using mint = ModInt<1000000007>;

vector<mint> fact, fact_inv;
void init_factorial(int n) {
    fact = vector<mint>(n + 1, 1);
    fact_inv = vector<mint>(n + 1);
    for (int i = 0; i < n; i++) fact[i + 1] = fact[i] * (i + 1);
    fact_inv[n] = mint(1) / fact[n];
    for (int i = n - 1; i >= 0; i--) fact_inv[i] = fact_inv[i + 1] * (i + 1);
    // for (int i = 0; i < n + 1; i++) assert(fact[i] * fact_inv[i] == 1);
}
mint comb(int n, int r) { return fact[n] * fact_inv[r] * fact_inv[n - r]; }

using pii = pair<Int, mint>;
signed main() {
    Int n;
    cin >> n;
    vector<Int> a(n);
    REP(i, n) cin >> a[i];
    vector<Int> dp(n, 1e18);
    vector<vector<pii>> g(n);
    for (Int x : a) {
        Int j = lower_bound(dp.begin(), dp.end(), x) - dp.begin();
        dp[j] = x;
        mint cnt = 0;
        if (j > 0) {
            auto it = upper_bound(g[j - 1].begin(), g[j - 1].end(), pii(-x + 1, 0));
            mint minus = it == g[j - 1].begin() ? 0 : prev(it)->second;
            mint plus = g[j - 1].empty() ? 0 : g[j - 1].back().second;
            cnt += plus - minus;
        } else {
            cnt = 1;
        }
        mint prv = g[j].empty() ? 0 : g[j].back().second;
        g[j].push_back({-x, cnt + prv});
    }
    Int idx = lower_bound(dp.begin(), dp.end(), 1e18) - dp.begin();
    cout << g[idx - 1].back().second << endl;
}
0