Skip to content

Commit 6a000f9

Browse files
committed
[Gold IV] Title: 가장 긴 바이토닉 부분 수열, Time: 140 ms, Memory: 14628 KB -BaekjoonHub
1 parent 5734022 commit 6a000f9

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Gold IV] 가장 긴 바이토닉 부분 수열 - 11054
2+
3+
[문제 링크](https://www.acmicpc.net/problem/11054)
4+
5+
### 성능 요약
6+
7+
메모리: 14628 KB, 시간: 140 ms
8+
9+
### 분류
10+
11+
다이나믹 프로그래밍
12+
13+
### 제출 일자
14+
15+
2025년 8월 17일 21:33:34
16+
17+
### 문제 설명
18+
19+
<p>수열 S가 어떤 수 S<sub>k</sub>를 기준으로 S<sub>1</sub> < S<sub>2</sub> < ... S<sub>k-1</sub> < S<sub>k</sub> > S<sub>k+1</sub> > ... S<sub>N-1</sub> > S<sub>N</sub>을 만족한다면, 그 수열을 바이토닉 수열이라고 한다.</p>
20+
21+
<p>예를 들어, {10, 20, <strong>30</strong>, 25, 20}과 {10, 20, 30, <strong>40</strong>}, {<strong>50</strong>, 40, 25, 10} 은 바이토닉 수열이지만, {1, 2, 3, 2, 1, 2, 3, 2, 1}과 {10, 20, 30, 40, 20, 30} 은 바이토닉 수열이 아니다.</p>
22+
23+
<p>수열 A가 주어졌을 때, 그 수열의 부분 수열 중 바이토닉 수열이면서 가장 긴 수열의 길이를 구하는 프로그램을 작성하시오.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 수열 A의 크기 N이 주어지고, 둘째 줄에는 수열 A를 이루고 있는 A<sub>i</sub>가 주어진다. (1 ≤ N ≤ 1,000, 1 ≤ A<sub>i</sub> ≤ 1,000)</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 수열 A의 부분 수열 중에서 가장 긴 바이토닉 수열의 길이를 출력한다.</p>
32+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
public static void main(String[] args) throws IOException {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
int N = Integer.parseInt(br.readLine());
8+
9+
// 수열 입력을 저장할 배열
10+
int[] A = new int[N];
11+
// A[i]를 마지막으로 하는 가장 긴 증가하는 부분 수열의 길이
12+
int[] dp_increase = new int[N];
13+
// A[i]를 시작으로 하는 가장 긴 감소하는 부분 수열의 길이
14+
int[] dp_decrease = new int[N];
15+
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
for (int i = 0; i < N; i++) {
18+
A[i] = Integer.parseInt(st.nextToken());
19+
}
20+
21+
// 1. LIS 계산 (왼쪽 -> 오른쪽 순방향)
22+
for (int i = 0; i < N; i++) {
23+
dp_increase[i] = 1; // 자기 자신만으로도 길이는 1
24+
for (int j = 0; j < i; j++) {
25+
// j번째 원소가 i번째 원소보다 작고, 새로운 길이가 더 길다면 갱신
26+
if (A[j] < A[i]) {
27+
dp_increase[i] = Math.max(dp_increase[i], dp_increase[j]+1);
28+
}
29+
}
30+
}
31+
32+
// 2. LDS 계산 (오른쪽 -> 왼쪽 역방향)
33+
for (int i = N - 1; i >= 0; i--) {
34+
dp_decrease[i] = 1; // 자기 자신만으로도 길이는 1
35+
for (int j = N - 1; j > i; j--) {
36+
// j번째 원소가 i번째 원소보다 작고, 새로운 길이가 더 길다면 갱신
37+
// (거꾸로 가는 LIS와 원리가 같음)
38+
if (A[j] < A[i]) {
39+
dp_decrease[i] = Math.max(dp_decrease[i], dp_decrease[j]+1);
40+
}
41+
}
42+
}
43+
44+
// 3. 결과 합산 및 최댓값 찾기
45+
int max_length = 0;
46+
for (int i = 0; i < N; i++) {
47+
// 증가하는 부분 길이 + 감소하는 부분 길이 - 1 (peak 중복 제거)
48+
int bitonic_length = dp_increase[i] + dp_decrease[i] - 1;
49+
max_length = Math.max(bitonic_length, max_length);
50+
}
51+
52+
System.out.println(max_length);
53+
}
54+
}

0 commit comments

Comments
 (0)