結果
問題 | No.1804 Intersection of LIS |
ユーザー |
![]() |
提出日時 | 2022-08-27 20:06:58 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,769 ms / 2,000 ms |
コード長 | 2,908 bytes |
コンパイル時間 | 2,657 ms |
コンパイル使用メモリ | 204,848 KB |
最終ジャッジ日時 | 2025-01-31 06:31:21 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include <bits/stdc++.h>/*template <typename T, class Compare>std::vector<T> longest_increasing_subsequence(const std::vector<T> &v,Compare comp) {const int n = v.size();std::vector<T> dp;std::vector<int> id(n);for (int i = 0; i < n; i++) {typename std::vector<T>::iterator it =std::lower_bound(dp.begin(), dp.end(), v[i], comp);id[i] = std::distance(dp.begin(), it);if (it == dp.end()) {dp.push_back(v[i]);} else {*it = v[i];}}std::vector<T> lis(dp.size());for (int i = n - 1, j = lis.size() - 1; i >= 0; i--) {if (id[i] == j) {lis[j--] = i;}}return lis;}template <typename T>std::vector<T> longest_increasing_subsequence(const std::vector<T> &v) {return longest_increasing_subsequence(v, std::less<T>());} */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();elseinf = 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];if (i < int(ans.size()) - 1) cout << ' ';}cout << '\n';}