Algorithm/Baekjoon
[Algorithm] Baekjoon 2565번 : 전깃줄
MaxLevel
2023. 7. 25. 20:54
https://www.acmicpc.net/problem/2565
2565번: 전깃줄
첫째 줄에는 두 전봇대 사이의 전깃줄의 개수가 주어진다. 전깃줄의 개수는 100 이하의 자연수이다. 둘째 줄부터 한 줄에 하나씩 전깃줄이 A전봇대와 연결되는 위치의 번호와 B전봇대와 연결되는
www.acmicpc.net
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
50
51
52
53
54
55
56
57
58
59
60
61
|
struct Node
{
int num;
int toNum;
};
bool cmp(const Node& a, const Node& b)
{
return a.num < b.num;
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
vector<Node> v;
int dp[501] = { 0 };
int arr[501] = { 0 };
cin >> n;
for (int i = 0; i < n; ++i)
{
int a, b;
cin >> a >> b;
v.push_back({ a,b });
}
sort(v.begin(), v.end(),cmp);
for (int i = 0; i < v.size(); ++i)
{
arr[i] = v[i].toNum;
}
int answer = 0;
for (int i = 0; i < n; ++i)
{
dp[i] = 1;
int maxNum = 0;
for (int j = 0; j < i; ++j)
{
if (arr[j] < arr[i])
{
maxNum = max(maxNum, dp[j]);
}
}
dp[i] += maxNum;
answer = max(answer, dp[i]);
}
cout << n - answer;
return 0;
}
|
cs |
문제는 삭제해야할 전깃줄의 최소개수를 구해야 하지만, 역으로 생각하면 쉽게 풀 수 있는 문제이다.
역으로 생각하면 올바른 형태의 최대전깃줄 개수를 구하는 문제가 되고, 이 형태는 결국 가장 긴 증가하는 부분수열을 구하는 문제가 된다.