結果

問題 No.1188 レベルX門松列
ユーザー tenten
提出日時 2020-12-29 16:22:32
言語 Java
(openjdk 23)
結果
AC  
実行時間 768 ms / 2,000 ms
コード長 2,638 bytes
コンパイル時間 2,630 ms
コンパイル使用メモリ 77,412 KB
実行使用メモリ 52,312 KB
最終ジャッジ日時 2024-10-05 17:35:05
合計ジャッジ時間 13,902 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] lds = new int[n + 1];
        int maxD = 0;
        lds[0] = Integer.MAX_VALUE;
        int[] lis = new int[n + 1];
        Arrays.fill(lis, Integer.MAX_VALUE);
        lis[0] = 0;
        int maxI = 0;
        int[] leftMaxesI = new int[n];
        int[] leftMaxesD = new int[n];
        int left;
        int right;
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
            left = 0;
            right = n;
            while (right - left > 1) {
                int m = (left + right) / 2;
                if (lis[m] < arr[i]) {
                    left = m;
                } else {
                    right = m;
                }
            }
            lis[right] = Math.min(lis[right], arr[i]);
            maxI = Math.max(maxI, right);
            leftMaxesI[i] = maxI;
            left = 0;
            right = n;
            while (right - left > 1) {
                int m = (left + right) / 2;
                if (lds[m] > arr[i]) {
                    left = m;
                } else {
                    right = m;
                }
            }
            lds[right] = Math.max(lds[right], arr[i]);
            maxD = Math.max(maxD, right);
            leftMaxesD[i] = maxD;
        }
        Arrays.fill(lds, 0);
        lds[0] = Integer.MAX_VALUE;
        Arrays.fill(lis, Integer.MAX_VALUE);
        lis[0] = 0;
        maxD = 0;
        maxI = 0;
        int ans = 0;
        for (int i = n - 1; i >= 0; i--) {
            left = 0;
            right = n;
            while (right - left > 1) {
                int m = (left + right) / 2;
                if (lis[m] < arr[i]) {
                    left = m;
                } else {
                    right = m;
                }
            }
            lis[right] = Math.min(lis[right], arr[i]);
            maxI = Math.max(maxI, right);
            ans = Math.max(ans, Math.min(leftMaxesI[i], maxI) - 1);
            left = 0;
            right = n;
            while (right - left > 1) {
                int m = (left + right) / 2;
                if (lds[m] > arr[i]) {
                    left = m;
                } else {
                    right = m;
                }
            }
            lds[right] = Math.max(lds[right], arr[i]);
            maxD = Math.max(maxD, right);
            ans = Math.max(ans, Math.min(leftMaxesD[i], maxD) - 1);
        }
        System.out.println(ans);
    }
}
0