結果

問題 No.110 しましまピラミッド
ユーザー takeya_okinotakeya_okino
提出日時 2017-06-15 19:13:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 5,000 ms
コード長 1,973 bytes
コンパイル時間 2,150 ms
コンパイル使用メモリ 74,400 KB
実行使用メモリ 56,068 KB
最終ジャッジ日時 2023-08-30 04:00:32
合計ジャッジ時間 7,380 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,704 KB
testcase_01 AC 131 ms
55,588 KB
testcase_02 AC 127 ms
55,836 KB
testcase_03 AC 129 ms
55,936 KB
testcase_04 AC 131 ms
55,660 KB
testcase_05 AC 131 ms
55,812 KB
testcase_06 AC 130 ms
55,996 KB
testcase_07 AC 130 ms
55,820 KB
testcase_08 AC 131 ms
55,820 KB
testcase_09 AC 130 ms
55,788 KB
testcase_10 AC 132 ms
55,828 KB
testcase_11 AC 129 ms
55,904 KB
testcase_12 AC 130 ms
55,760 KB
testcase_13 AC 132 ms
55,720 KB
testcase_14 AC 134 ms
55,808 KB
testcase_15 AC 129 ms
55,744 KB
testcase_16 AC 130 ms
55,800 KB
testcase_17 AC 129 ms
55,880 KB
testcase_18 AC 129 ms
55,708 KB
testcase_19 AC 127 ms
56,068 KB
testcase_20 AC 134 ms
55,820 KB
testcase_21 AC 130 ms
55,664 KB
testcase_22 AC 129 ms
55,684 KB
testcase_23 AC 128 ms
55,536 KB
testcase_24 AC 128 ms
55,628 KB
testcase_25 AC 129 ms
55,644 KB
testcase_26 AC 127 ms
53,776 KB
testcase_27 AC 129 ms
55,668 KB
testcase_28 AC 129 ms
55,704 KB
testcase_29 AC 128 ms
55,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int Nw = sc.nextInt();
    int[] w = new int[Nw];
    for(int i = 0; i < Nw; i++) {
      w[i] = sc.nextInt();
    }
    int Nb = sc.nextInt();
    int[] b = new int[Nb];
    for(int i = 0; i < Nb; i++) {
      b[i] = sc.nextInt();
    }
    Arrays.sort(w);
    Arrays.sort(b);
    int[] lowW = new int[21];
    int[] lowB = new int[21];
    lowW[0] = -1;
    lowW[1] = -1;
    lowB[0] = -1;
    lowB[1] = -1;
    for(int i = 2; i < 21; i++) {
      boolean flg = true;
      for(int j = Nw - 1; j >= 0; j--) {
        if(w[j] < i) {
          lowW[i] = j;
          flg = false;
          break;
        }
      }
      if(flg) lowW[i] = -1;
      flg = true;
      for(int j = Nb - 1; j >= 0; j--) {
        if(b[j] < i) {
          lowB[i] = j;
          flg = false;
          break;
        }        
      }
      if(flg) lowB[i] = -1;
    }
    // dpw[i]はw[i]を一番下とした時のピラミッドの高さの最大値
    // dpb[i]はb[i]を一番下とした時のピラミッドの高さの最大値
    int[] dpw = new int[Nw];
    int[] dpb = new int[Nb];
    dpw[0] = 1;
    dpb[0] = 1;
    if(lowB[w[0]] >= 0) dpw[0] = 2;
    if(lowW[b[0]] >= 0) dpb[0] = 2;
    int indexw = 1;
    int indexb = 1;
    for(int i = 0; i < Nw + Nb - 2; i++) {
      if(indexw < Nw) {
        if(lowB[w[indexw]] == -1) {
          dpw[indexw] = 1;
          indexw++;
        } else if(lowB[w[indexw]]  < indexb) {
          dpw[indexw] = 1 + dpb[lowB[w[indexw]]];
          indexw++;
        }
      }
      if(indexb < Nb) {
        if(lowW[b[indexb]] == -1) {
          dpb[indexb] = 1;
          indexb++;
        } else if(lowW[b[indexb]]  < indexw) {
          dpb[indexb] = 1 + dpw[lowW[b[indexb]]];
          indexb++;
        }        
      }
    }
    int ans = Math.max(dpw[Nw - 1], dpb[Nb - 1]);
    System.out.println(ans);
  }
}
0