結果
問題 | No.1804 Intersection of LIS |
ユーザー |
![]() |
提出日時 | 2022-01-07 22:44:41 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,736 bytes |
コンパイル時間 | 1,987 ms |
コンパイル使用メモリ | 199,664 KB |
最終ジャッジ日時 | 2025-01-27 09:40:16 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 WA * 1 |
other | AC * 34 WA * 3 |
ソースコード
#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;for(int i = 0; i < ans.size(); i++) {cout << ans[i] << ((i+1 == ans.size())?"\n":" ");}}