※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 12355번 문제인 Ocean View (Large)이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/12355
12355번: Ocean View (Large)
The first line of the input gives the number of test cases, T. T test cases follow. Each test case will consist of two lines. The first line will contain a single integer N, the number of houses on Awesome Boulevard. The next line will list the height of e
www.acmicpc.net
가장 적게 건물을 부수는 것은 결국 가장 많은 건물을 살리는 것과 같은 문제이다.
남은 건물은 strict하게 높이가 증가해야하므로, 가장 많은 건물을 살린다면 그 개수는 LIS의 길이와 같게 된다.
LIS를 구해 문제를 해결하자.
글쓴이는 세그먼트트리를 이용해 LIS의 길이를 구했다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <cstring>
using namespace std;
int seg[2049];
int upd(int L, int R, int qI, int qVal, int sI) {
if (R < qI || qI < L) return seg[sI];
if (L == R) return seg[sI] = qVal;
return seg[sI] = max(upd(L, (L + R) / 2, qI, qVal, sI * 2), upd((L + R) / 2 + 1, R, qI, qVal, sI * 2 + 1));
}
int rangemax(int L, int R, int qL, int qR, int sI) {
if (R < qL || qR < L) return 0;
if (qL <= L && R <= qR) return seg[sI];
return max(rangemax(L, (L + R) / 2, qL, qR, sI * 2), rangemax((L + R) / 2 + 1, R, qL, qR, sI * 2 + 1));
}
void solve() {
memset(seg, 0, sizeof(seg));
int N; cin >> N;
int NN = N;
while (NN--) {
int x; cin >> x;
int mx = rangemax(1, 1000, 1, x - 1, 1);
upd(1, 1000, x, mx + 1, 1);
}
cout << N - seg[1] << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T; cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": ";
solve();
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 8217 // C++] 유성 (0) | 2022.07.25 |
---|---|
[BOJ 15811 // C++] 복면산?! (0) | 2022.07.24 |
[BOJ 15564 // C++] Äventyr 2 (0) | 2022.07.24 |
[BOJ 23080 // C++] 스키테일 암호 (0) | 2022.07.24 |
[BOJ 12354 // C++] Ocean View (Small) (0) | 2022.07.24 |