結果

問題 No.335 門松宝くじ
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-06-01 08:26:30
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 3,194 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 108,616 KB
実行使用メモリ 20,528 KB
最終ジャッジ日時 2023-09-04 01:28:01
合計ジャッジ時間 1,897 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 14 ms
8,936 KB
testcase_06 AC 18 ms
9,056 KB
testcase_07 AC 36 ms
13,900 KB
testcase_08 AC 25 ms
13,848 KB
testcase_09 AC 54 ms
19,324 KB
testcase_10 AC 53 ms
19,928 KB
testcase_11 AC 53 ms
20,528 KB
testcase_12 AC 54 ms
20,300 KB
testcase_13 AC 55 ms
19,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int N, M;
int[][] E;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      M = readInt();
      E = new int[][](M, N);
      foreach (m; 0 .. M) {
        foreach (i; 0 .. N) {
          E[m][i] = readInt();
        }
      }
      
      int maxSum = -1;
      int ans = -1;
      foreach (m; 0 .. M) {
        auto mn = new int[][](N + 1, N + 1);
        auto mx = new int[][](N + 1, N + 1);
        foreach (i; 0 .. N + 1) {
          mn[i][] = N + 1;
          mx[i][] = 0;
        }
        foreach (i; 0 .. N + 1) {
          foreach (j; i .. N) {
            mn[i][j + 1] = min(mn[i][j], E[m][j]);
            mx[i][j + 1] = max(mx[i][j], E[m][j]);
          }
        }
        int sum;
        foreach (i; 0 .. N) foreach (j; i + 1 .. N) {
          int win;
          if (E[m][i] < E[m][j]) {
            // x i<j
            if (mx[0][i] > E[m][i]) {
              chmax(win, max(mx[0][i], E[m][j]));
            }
            // i x j
            if (mn[i + 1][j] < E[m][i]) {
              chmax(win, E[m][j]);
            }
            if (mx[i + 1][j] > E[m][j]) {
              chmax(win, mx[i + 1][j]);
            }
            // i<j x
            if (mn[j + 1][N] < E[m][j]) {
              chmax(win, E[m][j]);
            }
          } else {
            // x i>j
            if (mn[0][i] < E[m][i]) {
              chmax(win, E[m][i]);
            }
            // i x j
            if (mn[i + 1][j] < E[m][j]) {
              chmax(win, E[m][i]);
            }
            if (mx[i + 1][j] > E[m][i]) {
              chmax(win, mx[i + 1][j]);
            }
            // i>j x
            if (mx[j + 1][N] > E[m][j]) {
              chmax(win, max(mx[j + 1][N], E[m][i]));
            }
          }
          debug {
            writeln(m, " ", E[m][i], " ", E[m][j], ": ", win);
          }
          sum += win;
        }
        debug {
          writeln(m, ": ", sum);
        }
        if (chmax(maxSum, sum)) {
          ans = m;
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0