Algorithm/Baekjoon
[Algorithm]Baekjoon 18353번 :: 병사 배치하기
MaxLevel
2024. 3. 19. 23:15
https://www.acmicpc.net/problem/18353
18353번: 병사 배치하기
첫째 줄에 N이 주어진다. (1 ≤ N ≤ 2,000) 둘째 줄에 각 병사의 전투력이 공백을 기준으로 구분되어 차례대로 주어진다. 각 병사의 전투력은 10,000,000보다 작거나 같은 자연수이다.
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
|
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_map>
#include <stack>
#include <numeric>
using namespace std;
int n;
int arr[2001] = { 0 };
int dp[2001] = { 0 };
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> arr[i];
}
arr[0] = 0x3f3f3f3f;
int maxSoldierCount = 0; // 최대내림차순길이
for (int i = 1; i <= n; ++i)
{
for (int j = i - 1; j >= 0; --j)
{
if (arr[i] < arr[j])
{
dp[i] = max(dp[i], dp[j] + 1);
maxSoldierCount = max(maxSoldierCount, dp[i]);
}
}
}
cout << n - maxSoldierCount;
}
|
cs |
기본적인 LIS문제인데, 오름차순이 아니라 내림차순을 구하는 문제이다.
그러니 arr[0]에 0이 아닌 큰 수를 넣어놓고 시작하면 된다.