Algorithm/백준

[백준] 11054번 가장 긴 바이토닉 부분 수열

sirius 2021. 3. 9. 10:05
https://www.acmicpc.net/problem/11054
 

11054번: 가장 긴 바이토닉 부분 수열

첫째 줄에 수열 A의 크기 N이 주어지고, 둘째 줄에는 수열 A를 이루고 있는 Ai가 주어진다. (1 ≤ N ≤ 1,000, 1 ≤ Ai ≤ 1,000)

www.acmicpc.net

 

왼쪽부터 증가하는 수열 길이의 최대값 + 오른쪽부터 증가하는 수열 길이의 최대값

플러스 했으므로 중복이라 -1 해주면 됨

 

import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine());
        int[] arr = new int[n+1];
        int[] dpLeft = new int[n+1];
        int[] dpRight = new int[n+1];
        for(int i=1; i<=n; i++) {
        	arr[i] = Integer.parseInt(st.nextToken());
        }

        dpLeft[1] = 1;
        dpRight[n] = 1;
        
        for(int i =2; i<=n; i++) {
            dpLeft[i] = 1;
            for(int j = 1; j < i; j++) {
                if(arr[i] > arr[j]) {
                	dpLeft[i] = Math.max(dpLeft[i], dpLeft[j] + 1);
                }
            }
        }

        for(int i=n-1; i >=0; i--) {
            dpRight[i] = 1;
            for(int j = n; j > i ; j--) {
                if(arr[i] > arr[j]) {
                	dpRight[i] = Math.max(dpRight[i], dpRight[j] + 1);
                }
            }
        }

        int max = 0;
        for(int i = 0; i <= n; i++) {
        	max = Math.max(max, dpLeft[i] + dpRight[i]);
        }

        System.out.println(max - 1);
    }

}