結果

問題 No.1804 Intersection of LIS
ユーザー ぷらぷら
提出日時 2022-01-07 22:59:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 1,790 bytes
コンパイル時間 2,718 ms
コンパイル使用メモリ 205,160 KB
実行使用メモリ 18,068 KB
最終ジャッジ日時 2023-08-09 03:32:05
合計ジャッジ時間 9,703 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 291 ms
13,904 KB
testcase_04 AC 290 ms
13,464 KB
testcase_05 AC 283 ms
13,476 KB
testcase_06 AC 290 ms
13,420 KB
testcase_07 AC 290 ms
13,964 KB
testcase_08 AC 294 ms
13,476 KB
testcase_09 AC 298 ms
13,968 KB
testcase_10 AC 289 ms
13,820 KB
testcase_11 AC 284 ms
13,416 KB
testcase_12 AC 295 ms
13,416 KB
testcase_13 AC 293 ms
13,520 KB
testcase_14 AC 296 ms
13,508 KB
testcase_15 AC 290 ms
13,460 KB
testcase_16 AC 292 ms
13,820 KB
testcase_17 AC 286 ms
13,468 KB
testcase_18 AC 3 ms
4,384 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 3 ms
4,384 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,384 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,384 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 246 ms
18,068 KB
testcase_36 AC 252 ms
18,004 KB
testcase_37 AC 217 ms
13,464 KB
testcase_38 AC 218 ms
13,424 KB
testcase_39 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int inf = 1001001001;

template <typename T>
struct SegmentTree {
    int n;
    vector<T> dat;
    SegmentTree() {}
    SegmentTree(int N) {
        n = 1;
        while(n < N) {
            n *= 2;
        }
        dat.resize(2*n-1,-inf);
    }
    void update(int x, T val) {
        x += (n-1);
        dat[x] = val;
        while(x > 0) {
            x = (x-1)/2;
            dat[x] = max(dat[2*x+1],dat[2*x+2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if(r <= a || b <= l) return -inf;
        if(a <= l && r <= b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return max(vl, vr);
    }
    T query(int a,int b) {//[a,b)
        return query(a,b,0,0,n);
    }
};

int main() {
    int N;
    cin >> N;
    vector<int>P(N);
    for(int i = 0; i < N; i++) {
        cin >> P[i];
    }
    SegmentTree<int>Seg1(N+1);
    Seg1.update(0,0);
    vector<int>mx1(N);
    for(int i = 0; i < N; i++) {
        int q = Seg1.query(0,P[i]);
        mx1[i] = q;
        Seg1.update(P[i],q+1);
    }
    int LIS = Seg1.query(0,N+1);
    SegmentTree<int>Seg2(N+2);
    Seg2.update(N+1,0);
    vector<int>ans,used(LIS),rev(LIS);
    for(int i = N-1; i >= 0; i--) {
        int q = Seg2.query(P[i],N+2);
        if(mx1[i]+q+1 == LIS) {
            used[mx1[i]]++;
            rev[mx1[i]] = P[i];
        }
        Seg2.update(P[i],q+1);
    }
    for(int i = 0; i < LIS; i++) {
        if(used[i] == 1) {
            ans.push_back(rev[i]);
        }
    }
    cout << ans.size() << endl;
    if(ans.size() == 0) {
        cout << endl;
    }
    for(int i = 0; i < ans.size(); i++) {
        cout << ans[i] << ((i+1 == ans.size())?"\n":" ");
    }
}
0