-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB11054_2.java
More file actions
66 lines (54 loc) · 1.48 KB
/
B11054_2.java
File metadata and controls
66 lines (54 loc) · 1.48 KB
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
62
63
64
65
66
package Practice;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B11054_2 { //가장 긴 바이토닉 부분 수열
static int N;
static int[] num, dp1, dp2, temp;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
num = new int[N];
dp1 = new int[N];
dp2 = new int[N];
temp = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++) num[i] = Integer.parseInt(st.nextToken());
int index = 0;
temp[0] = num[0];
dp1[0] = 1;
for(int i = 1; i < N; i++) {
dp1[i] = dp1[i - 1];
if(temp[index] < num[i]) {
dp1[i]++;
temp[++index] = num[i];
}
else LowerBound(num[i], 0, index);
}
temp = new int[N];
index = 0;
temp[0] = num[N - 1];
dp2[N - 1] = 1;
for(int i = N - 2; i >= 0; i--) {
dp2[i] = dp2[i + 1];
if(temp[index] < num[i]) {
dp2[i]++;
temp[++index] = num[i];
}
else LowerBound(num[i], 0, index);
}
int max = 0;
for(int i = 0; i < N; i++) max = Math.max(max, dp1[i] + dp2[i]);
System.out.println(max - 1);
}
public static void LowerBound(int key, int start, int end) {
if(start >= end) {
temp[end] = key;
return;
}
int mid = (start + end) / 2;
if(temp[mid] >= key) LowerBound(key, start, mid);
else LowerBound(key, mid + 1, end);
}
}