結果

問題 No.992 最長増加部分列の数え上げ
ユーザー Plan8Plan8
提出日時 2020-05-05 16:41:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,602 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 214,776 KB
実行使用メモリ 716,436 KB
最終ジャッジ日時 2023-09-09 04:01:04
合計ジャッジ時間 32,407 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 419 ms
174,924 KB
testcase_05 AC 271 ms
112,032 KB
testcase_06 AC 568 ms
238,752 KB
testcase_07 AC 362 ms
149,984 KB
testcase_08 AC 144 ms
59,148 KB
testcase_09 AC 372 ms
154,324 KB
testcase_10 AC 559 ms
235,020 KB
testcase_11 AC 776 ms
331,836 KB
testcase_12 AC 86 ms
35,304 KB
testcase_13 AC 339 ms
140,888 KB
testcase_14 AC 344 ms
142,556 KB
testcase_15 AC 87 ms
35,884 KB
testcase_16 MLE -
testcase_17 AC 149 ms
60,032 KB
testcase_18 AC 330 ms
136,380 KB
testcase_19 AC 763 ms
323,960 KB
testcase_20 MLE -
testcase_21 MLE -
testcase_22 MLE -
testcase_23 MLE -
testcase_24 MLE -
testcase_25 MLE -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 MLE -
testcase_29 MLE -
testcase_30 AC 144 ms
26,796 KB
testcase_31 AC 143 ms
26,672 KB
testcase_32 AC 140 ms
26,736 KB
testcase_33 AC 137 ms
26,672 KB
testcase_34 AC 136 ms
26,796 KB
testcase_35 AC 137 ms
26,732 KB
testcase_36 AC 127 ms
26,676 KB
testcase_37 AC 137 ms
26,796 KB
testcase_38 AC 147 ms
26,708 KB
testcase_39 AC 123 ms
26,720 KB
testcase_40 AC 136 ms
27,324 KB
testcase_41 AC 139 ms
27,592 KB
testcase_42 AC 139 ms
27,236 KB
testcase_43 AC 138 ms
27,500 KB
testcase_44 AC 139 ms
27,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.141592653589793238462643383279;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<typename T>
struct BIT{
    using Func = function<T(T, T)>;

    int n;
    vector<T> node;

    T init_v;
    Func func;

    public:

        BIT(int N, Func _func, T _init_v) {
            n = N + 1;
            init_v = _init_v;
            func = _func;

            node.resize(n, init_v);
            for(int i=1; i<n; i++) if((i+(i&-i)) < n) node[i+(i&-i)] = func(node[i+(i&-i)], node[i]);
        }

        BIT() {}

        void add(int pos, T v) {
            // 1-indexed
            if(pos == 0) return;
            while(pos < n){
                node[pos] = func(node[pos], v);
                pos += pos & -pos;
            }
        }

        T sum(int pos){
            // 1-indexed
            T res = init_v;
            while(pos > 0){
                res = func(res, node[pos]);
                pos = pos & (pos-1);
            }
            return res;
        }

        T get(int pos){
            // 1-indexed
            return node[pos];
        }
};

void compress(vector<int> &v){
    int N = v.size(), mn = 1000000000;
    vector<pair<int,int>> a(N);
    for(int i=0; i<N; ++i){
        a[i].first = v[i];
        a[i].second = i;
        mn = min(mn, v[i]);
    }
    sort(a.begin(), a.end());
    
    int prev = mn-1, n = -1;
    for(int i=0; i<N; ++i){
        if(a[i].first == prev) v[a[i].second] = n;
        else{
            n++;
            prev = a[i].first;
            v[a[i].second] = n;
        }
    }
}

int main(void){
    int N; cin >> N;
    VI A(N); REP(i,N) cin >> A[i];
    compress(A);
    VI dp, len(N), mx(N+1,0);
    int maxlen = 0;
    REP(i,N){
        int t = LB(dp, A[i]);
        if(t < dp.size()) chmin(dp[t], A[i]);
        else dp.push_back(A[i]);
        len[i] = t+1;
        chmax(mx[len[i]], A[i]+1);
        chmax(maxlen, len[i]);
    }

    vector<BIT<int>> v(N+1);
    REP(i,N+1) v[i] = BIT<int>(mx[i], [](int a, int b){return (a+b)%MOD;}, 0);

    REP(i,N){
        int idx = min(v[len[i]-1].n-1, A[i]);
        ll n = len[i]>1 ? v[len[i]-1].sum(idx) : 1;
        v[len[i]].add(A[i]+1, n);
    }

    ll ans = v[maxlen].sum(mx[maxlen]);
    cout << ans << en;
    return 0;
}
0