結果

問題 No.1804 Intersection of LIS
ユーザー gyouzasushigyouzasushi
提出日時 2022-08-27 20:05:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,970 bytes
コンパイル時間 2,869 ms
コンパイル使用メモリ 204,676 KB
実行使用メモリ 14,020 KB
最終ジャッジ日時 2024-04-22 17:22:23
合計ジャッジ時間 11,230 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 148 ms
6,912 KB
testcase_04 AC 148 ms
6,912 KB
testcase_05 AC 148 ms
6,784 KB
testcase_06 AC 150 ms
6,912 KB
testcase_07 AC 145 ms
6,912 KB
testcase_08 AC 146 ms
6,784 KB
testcase_09 AC 145 ms
6,912 KB
testcase_10 AC 146 ms
6,912 KB
testcase_11 AC 146 ms
6,784 KB
testcase_12 AC 148 ms
6,912 KB
testcase_13 AC 151 ms
6,784 KB
testcase_14 AC 150 ms
6,784 KB
testcase_15 AC 147 ms
6,656 KB
testcase_16 AC 147 ms
6,912 KB
testcase_17 AC 151 ms
6,912 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 WA -
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1,665 ms
13,892 KB
testcase_36 AC 1,705 ms
14,020 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <typename T, typename Compare>
vector<int> LongestIncreasingSubsequence(vector<T> &A, Compare comp) {
    int N = (int)A.size();
    vector<T> DP;
    vector<int> nxt(N, N);
    vector<int> mem(N);
    T inf;
    if (comp(0, numeric_limits<T>::max()))
        inf = numeric_limits<T>::min();
    else
        inf = numeric_limits<T>::max();
    auto comp2 = [&](T l, T r) { return !comp(l, r); };
    for (int i = N - 1; i >= 0; i--) {
        auto it = upper_bound(DP.begin(), DP.end(), A[i], comp2);
        int d = it - DP.begin();
        if (it == DP.end())
            DP.emplace_back(A[i]);
        else
            *it = A[i];
        mem[i] = d;
    }
    stack<int> st;
    for (int i = N - 1; i >= 0; i--) {
        while (!st.empty() && !comp(A[i], A[st.top()])) st.pop();
        if (!st.empty()) nxt[i] = st.top();
        st.emplace(i);
    }
    int len = (int)DP.size();
    vector<int> ret;
    for (int i = 0; len && i < N; i++) {
        if (mem[i] == len - 1) {
            ret.emplace_back(i);
            len--;
        }
    }
    return ret;
}

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    auto lis_min = LongestIncreasingSubsequence(v, less<int>());
    reverse(v.begin(), v.end());
    auto lis_max = LongestIncreasingSubsequence(v, greater<int>());
    reverse(lis_max.begin(), lis_max.end());
    for (int i = 0; i < lis_max.size(); i++) lis_max[i] = n - 1 - lis_max[i];
    reverse(v.begin(), v.end());

    vector<int> ans;
    int m = lis_min.size();
    assert(int(lis_max.size()) == m);
    for (int i = 0; i < m; i++) {
        cerr << lis_min[i] << " " << lis_max[i] << '\n';
        if (lis_min[i] == lis_max[i]) {
            ans.push_back(v[lis_min[i]]);
        }
    }
    cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << " \n"[i == int(ans.size()) - 1];
    }
}
0