Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 다이나믹 프로그래밍
- 투 포인터
- c++
- 스택
- 유니온 파인드
- Team Fortress 2
- Unreal Engine 5
- 수학
- 그래프
- 재귀
- 다익스트라
- 그리디 알고리즘
- DFS
- 구현
- BFS
- 백준
- 백트래킹
- 우선순위 큐
- VR
- 트리
- ue5
- 누적 합
- 유니티
- 문자열
- 자료구조
- 알고리즘
- 브루트포스
- XR Interaction Toolkit
- 정렬
- 시뮬레이션
Archives
- Today
- Total
1일1알
백준 14402번 가장 긴 증가하는 부분 수열 4 C++ 본문
lis 문제랑 똑같이 었는데, parent 배열을 하나 만들어서 수열의 인덱스를 저장해서 어떤 수열인지 추적하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> v(n);
vector<int> dp(n, 1);
vector<int> parent(n);
int ans = 1;
int end = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
}
for (int i = 0; i < n; i++) parent[i] = i;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (v[i] <= v[j]) continue;
if (dp[j] + 1 <= dp[i]) continue;
dp[i] = dp[j] + 1;
parent[i] = j;
if (dp[i] > ans) {
ans = dp[i];
end = i;
}
}
}
vector<int> ansVec;
while (true) {
ansVec.push_back(v[end]);
if (end == parent[end]) break;
end = parent[end];
}
reverse(ansVec.begin(), ansVec.end());
cout << ans << "\n";
for (auto a : ansVec) {
cout << a << " ";
}
}
'알고리즘' 카테고리의 다른 글
백준 13913번 숨바꼭질 4 C++ (0) | 2022.08.15 |
---|---|
백준 5052번 전화번호 목록 C++ (0) | 2022.08.14 |
백준 1339번 단어 수학 C++ (0) | 2022.08.12 |
백준 1261번 알고스팟 C++ (0) | 2022.08.11 |
백준 3055번 탈출 C++ (0) | 2022.08.10 |