forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4.cpp
41 lines (33 loc) Β· 916 Bytes
/
4.cpp
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
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v;
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
// μμλ₯Ό λ€μ§μ΄ 'μ΅μ₯ μ¦κ° λΆλΆ μμ΄' λ¬Έμ λ‘ λ³ν
reverse(v.begin(), v.end());
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°μ μν 1μ°¨μ DP ν
μ΄λΈ μ΄κΈ°ν
int dp[2000];
for (int i = 0; i < n; i++) {
dp[i] = 1;
}
// κ°μ₯ κΈ΄ μ¦κ°νλ λΆλΆ μμ΄(LIS) μκ³ λ¦¬μ¦ μν
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (v[j] < v[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
// μ΄μΈν΄μΌ νλ λ³μ¬μ μ΅μ μλ₯Ό μΆλ ₯
int maxValue = 0;
for (int i = 0; i < n; i++) {
maxValue = max(maxValue, dp[i]);
}
cout << n - maxValue << '\n';
}