結果

問題 No.2157 崖
ユーザー tnakao0123tnakao0123
提出日時 2022-12-11 21:36:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 395 ms / 6,000 ms
コード長 1,350 bytes
コンパイル時間 541 ms
コンパイル使用メモリ 62,676 KB
実行使用メモリ 21,248 KB
最終ジャッジ日時 2024-04-23 16:33:31
合計ジャッジ時間 7,736 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 270 ms
15,744 KB
testcase_14 AC 393 ms
21,248 KB
testcase_15 AC 251 ms
17,224 KB
testcase_16 AC 254 ms
17,024 KB
testcase_17 AC 305 ms
19,456 KB
testcase_18 AC 296 ms
19,328 KB
testcase_19 AC 314 ms
17,664 KB
testcase_20 AC 395 ms
20,864 KB
testcase_21 AC 391 ms
21,120 KB
testcase_22 AC 296 ms
19,072 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 6 ms
15,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2157.cc:  No.2157 崖 - yukicoder
 */

#include<cstdio>
#include<deque>
#include<algorithm>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 1500;
const int MAX_M = 1500;
const int INF = 1 << 30;

/* typedef */

typedef pair<int,int> pii;
typedef deque<pii> dqpii;

/* global variables */

int ds[MAX_N][MAX_M], dp[MAX_N][MAX_M];

/* subroutines */

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) scanf("%d", ds[i] + j);
    sort(ds[i], ds[i] + m);
  }

  for (int i = 1; i < n; i++) {
    dqpii q;
    for (int j0 = 0, j1 = 0; j1 < m; j1++) {
      while (j0 < m && ds[i - 1][j0] <= ds[i][j1]) {
	while (! q.empty() && q.back().first >= dp[i - 1][j0]) q.pop_back();
	q.push_back(pii(dp[i - 1][j0], ds[i - 1][j0]));
	j0++;
      }

      dp[i][j1] = INF;
      if (! q.empty()) {
	pii u = q.front(); q.pop_front();
	int ud = max(u.first, ds[i][j1] - u.second);
	while (! q.empty()) {
	  pii v = q.front();
	  int vd = max(v.first, ds[i][j1] - v.second);
	  if (ud >= vd) {
	    q.pop_front();
	    u = v, ud = vd;
	  }
	  else
	    break;
	}
	q.push_front(u);
	dp[i][j1] = ud;
      }
    }
  }

  int mind = *min_element(dp[n - 1], dp[n - 1] + m);
  printf("%d\n", (mind < INF) ? mind : -1);
  
  return 0;
}
0