feat: new dp problem formatted
This commit is contained in:
47
longest-increasing-subsequence-II/src/TLE.cpp
Normal file
47
longest-increasing-subsequence-II/src/TLE.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(){
|
||||
int N; cin >> N;
|
||||
vector<int> a(N);
|
||||
for (int i = 0; i < N; i++) {
|
||||
cin >> a[i];
|
||||
}
|
||||
|
||||
vector<int> d(N, 1);
|
||||
vector<int> p(N, -1);
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (a[j] < a[i] && d[j] + 1 > d[i]) {
|
||||
d[i] = d[j] + 1;
|
||||
p[i] = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ans = d[0], pos = 0;
|
||||
for (int i = 1; i < N; i++) {
|
||||
if (d[i] > ans) {
|
||||
ans = d[i];
|
||||
pos = i;
|
||||
}
|
||||
}
|
||||
|
||||
vector<int> subseq;
|
||||
while (pos != -1) {
|
||||
subseq.push_back(a[pos]);
|
||||
pos = p[pos];
|
||||
}
|
||||
reverse(subseq.begin(), subseq.end());
|
||||
|
||||
cout << ans << endl;
|
||||
for (int i = 0; i < ans; i++) {
|
||||
if (i != 0) cout << " ";
|
||||
cout << subseq[i];
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user