Baekjoon
[#2565] 전깃줄
강람이
2020. 3. 12. 09:55
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
49
|
/*
A에 연결된 위치 번호를 기준으로 정렬한다.
전선 꼬이지 않으려면 B에 연결된 위치 번호가 증가하는 수열이여야 한다.
따라서 B 위치 번호의 LIS를 구한다
구한 LIS는 연결된 전선의 갯수이고 출력해야 하는 것은 없애야 하는 전선의 갯수이기때문에
n-LIS를 출력한다.
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int,int>> connect(100);
vector<int> dp(100);
for (int i = 0; i < n; i++){
cin >> connect[i].first >> connect[i].second;
}
sort(connect.begin(), connect.begin() + n); //A를 기준으로 정렬
//B의 LIS 구하기
dp[0] = 1;
for (int i = 1; i < n; i++) {
int maxVal = 0;
for (int j = 0; j <= i - 1; j++) {
if (connect[j].second < connect[i].second)
maxVal = max(maxVal, dp[j]);
}
dp[i] = maxVal + 1;
}
int LIS = INT_MIN;
for (int i = 0; i < n; i++) {
LIS = max(LIS, dp[i]);
}
cout << n - LIS;
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
A를 기준으로 정렬해서 하자! 까지는 생각했지만 B의 LIS를 구해야한다까지 생각이 못갔다...