반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <iostream> #include <stack> #include <algorithm> #define rep(i,n) for(int i=0;i<n;i++) using namespace std; stack<int> s; int n, ans, idx, arr[1001], dp[1001], index[1001]; void func(int idx) { if (idx == -1) return; s.push(arr[idx]); func(index[idx]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; rep(i, n) { cin >> arr[i]; dp[i] = 1; index[i] = -1; } rep(i, n) { rep(j,i){ if (arr[i] > arr[j]) { if (dp[j] + 1 > dp[i]) { dp[i] = dp[j] + 1; index[i] = j; } } } } // dp 최댓값, 인덱스 찾기 rep(i, n) { if (dp[i] > ans) { ans = dp[i]; idx = i; } } cout << ans << '\n'; // index 배열 탐색 func(idx); while (!s.empty()) { cout << s.top() << ' '; s.pop(); } } | cs |
반응형