728x90
https://www.acmicpc.net/problem/11054
11054번: 가장 긴 바이토닉 부분 수열
첫째 줄에 수열 A의 크기 N이 주어지고, 둘째 줄에는 수열 A를 이루고 있는 Ai가 주어진다. (1 ≤ N ≤ 1,000, 1 ≤ Ai ≤ 1,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
54
55
56
57
58
59
|
import java.util.*;import java.io.*;
class Main{
static int N;
static int[] arr;
static int[] lis_dp;
static int[] lds_dp;
public static void main(String[] args) throws Exception{
// Please Enter Your Code Here
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N =Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
arr = new int[N];
// LIS dp
lis_dp = new int[N];
// LDS dp
lds_dp = new int[N];
for(int i=0; i<N; ++i) arr[i] = Integer.parseInt(st.nextToken());
LIS();
LDS();
int max = 0;
for(int i = 0; i < N; ++i) max = Math.max(max, lis_dp[i] + lds_dp[i]);
bw.write(String.valueOf(max - 1));
bw.flush();
bw.close();
br.close();
}
static void LIS() {
for(int i = 0; i < N; ++i) {
lis_dp[i] = 1;
for(int j = 0; j < i; ++j)
if(arr[i] > arr[j] && lis_dp[i] < lis_dp[j] + 1)
lis_dp[i] = lis_dp[j] + 1;
}
}
static void LDS() {
for(int i = N - 1; i > -1; --i) {
lds_dp[i] = 1;
for(int j = N - 1; j > i; --j)
if(arr[j] < arr[i] && lds_dp[i] < lds_dp[j] + 1)
lds_dp[i] = lds_dp[j] + 1;
}
}
}
|
cs |
반응형
'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글
연속합 2 (0) | 2021.10.21 |
---|---|
연속합 (0) | 2021.10.20 |
가장 긴 감소하는 부분 수열 (0) | 2021.10.20 |
가장 큰 증가 부분 수열 (0) | 2021.10.19 |
가장 긴 증가하는 부분 수열 4 (0) | 2021.10.19 |