結果

問題 No.335 門松宝くじ
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-07 11:41:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 964 ms / 2,000 ms
コード長 2,987 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 77,616 KB
実行使用メモリ 64,956 KB
最終ジャッジ日時 2024-04-15 01:27:12
合計ジャッジ時間 10,822 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
53,988 KB
testcase_01 AC 137 ms
54,152 KB
testcase_02 AC 140 ms
53,880 KB
testcase_03 AC 139 ms
54,332 KB
testcase_04 AC 139 ms
53,928 KB
testcase_05 AC 453 ms
61,044 KB
testcase_06 AC 517 ms
60,884 KB
testcase_07 AC 765 ms
64,384 KB
testcase_08 AC 718 ms
64,956 KB
testcase_09 AC 964 ms
64,304 KB
testcase_10 AC 912 ms
64,128 KB
testcase_11 AC 903 ms
64,364 KB
testcase_12 AC 915 ms
64,300 KB
testcase_13 AC 952 ms
64,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  static int N;
  static long[] dat1;
  static long[] dat2;

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    init(n);
    int m = sc.nextInt();
    long[] ki = new long[m];
    long ma = 0;

    for(int i = 0; i < m; i++) {
      long[] E = new long[n];
      for(int j = 0; j < n; j++) {
        E[j] = sc.nextLong();
        update(j, E[j]);
      }
      long t = 0;
      for(int j = 0; j < n - 1; j++) {
        for(int k = j + 1; k < n; k++) {
          long[] r = query(j, k + 1, 0, 0, N);
          long re = 0;
          if((r[1] > E[j]) && (r[1] > E[k])) {
            long m1 = Math.max(E[j], E[k]);
            if((r[0] < E[j]) && (r[0] < E[k])) {
              if(r[1] >= m1) {
                re = Math.max(re, r[1]);
              } else {
                re = Math.max(re, m1);
              }
            } else {
              re = Math.max(re, r[1]);
            }
          } else {
            if((r[0] < E[j]) && (r[0] < E[k])) re = Math.max(re, Math.max(E[j], E[k]));
          }

          if(j > 0) {
            long[] g = query(0, j, 0, 0, N);
            if(E[j] > E[k]) {
              if(g[0] < E[j]) re = Math.max(re, E[j]);
            } else {
              if(g[1] > E[j]) re = Math.max(re, Math.max(g[1], E[k]));
            }
          }
          if(k < n - 1) {
            long[] h = query(k + 1, N, 0, 0, N);    
            if(E[j] > E[k]) {
              if(h[1] > E[k]) re = Math.max(re, Math.max(h[1], E[j]));
            } else {
              if(h[0] < E[k]) re = Math.max(re, E[k]);
            }        
          }

          t += re;
        }
      }
      ki[i] = t;
      ma = Math.max(ma, t);
    }
    
    for(int i = 0; i < m; i++) {
      if(ki[i] == ma) {
        System.out.println(i);
        break; 
      }
    }
  }

  public static void init(int n_) {
    int n = 1;
    while(n < n_) {
      n *= 2;
    }
    N = n;
    dat1 = new long[2 * N - 1];
    dat2 = new long[2 * N - 1];
    for(int i = 0; i < dat1.length; i++) {
      dat1[i] = 100000;
      dat2[i] = (-1) * 100000;
    }
  }

  public static void update(int k, long x) {
    int a = k + N - 1;
    dat1[a] = x;
    dat2[a] = x;
    while(a > 0) {
      a = (a - 1) / 2;
      dat1[a] = Math.min(dat1[2 * a + 1], dat1[2 * a + 2]);
      dat2[a] = Math.max(dat2[2 * a + 1], dat2[2 * a + 2]);
    }
  }

  public static long[] query(int a, int b, int k, int l, int r) {
    long[] ret = new long[2];
    if((r <= a) || (l >= b)) {
      ret[0] = 100000;
      ret[1] = (-1) * 100000;
      return ret;
    }
    if((a <= l) && (r <= b)) {
      ret[0] = dat1[k];
      ret[1] = dat2[k];
      return ret;
    } else {
      long[] retl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      long[] retr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      ret[0] = Math.min(retl[0], retr[0]);
      ret[1] = Math.max(retl[1], retr[1]);
      return ret;
    }
  }
}
0