|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + static int[] arr,lis,lds; |
| 10 | + static int len,ans; |
| 11 | + |
| 12 | + |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + init(); |
| 15 | + process(); |
| 16 | + print(); |
| 17 | + } |
| 18 | + |
| 19 | + private static void init() throws IOException { |
| 20 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 21 | + len = Integer.parseInt(br.readLine()); |
| 22 | + arr = new int[len]; |
| 23 | + lis = new int[len]; |
| 24 | + lds = new int[len]; |
| 25 | + ans = 0; |
| 26 | + |
| 27 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 28 | + for (int i = 0; i < len; i++) { |
| 29 | + arr[i] = Integer.parseInt(st.nextToken()); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private static void process() throws IOException { |
| 34 | + Arrays.fill(lis, 1); |
| 35 | + Arrays.fill(lds, 1); |
| 36 | + |
| 37 | + for (int i = 1; i < len; i++) { |
| 38 | + for (int j = 0; j < i; j++){ |
| 39 | + if (arr[i] > arr[j]) lis[i] = Math.max(lis[i], lis[j] + 1); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + for (int i = len -1; i >= 0; i--) { |
| 44 | + for (int j = len-1; j > i; j--){ |
| 45 | + if (arr[i] > arr[j]) lds[i] = Math.max(lds[i], lds[j] + 1); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + for (int i = 0; i < len; i++){ |
| 50 | + ans = Math.max(ans, lis[i] + lds[i] - 1); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static void print() { |
| 55 | + System.out.println(ans); |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
0 commit comments