結果

問題 No.2639 Longest Increasing Walk
ユーザー tnakao0123tnakao0123
提出日時 2024-02-20 17:50:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,306 bytes
コンパイル時間 721 ms
コンパイル使用メモリ 59,096 KB
実行使用メモリ 19,792 KB
最終ジャッジ日時 2024-02-20 17:50:08
合計ジャッジ時間 3,141 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,984 KB
testcase_01 AC 4 ms
11,984 KB
testcase_02 AC 3 ms
11,984 KB
testcase_03 AC 3 ms
11,984 KB
testcase_04 AC 40 ms
12,880 KB
testcase_05 AC 67 ms
19,792 KB
testcase_06 AC 63 ms
19,664 KB
testcase_07 AC 75 ms
19,536 KB
testcase_08 AC 65 ms
19,664 KB
testcase_09 AC 76 ms
19,664 KB
testcase_10 AC 41 ms
15,568 KB
testcase_11 AC 38 ms
15,184 KB
testcase_12 AC 9 ms
12,496 KB
testcase_13 AC 44 ms
15,824 KB
testcase_14 AC 27 ms
14,160 KB
testcase_15 AC 4 ms
11,984 KB
testcase_16 AC 5 ms
11,984 KB
testcase_17 AC 27 ms
14,160 KB
testcase_18 AC 34 ms
14,672 KB
testcase_19 AC 11 ms
12,624 KB
testcase_20 AC 20 ms
13,520 KB
testcase_21 AC 37 ms
15,184 KB
testcase_22 AC 15 ms
13,136 KB
testcase_23 AC 4 ms
11,984 KB
testcase_24 AC 4 ms
11,984 KB
testcase_25 AC 4 ms
11,984 KB
testcase_26 AC 4 ms
11,984 KB
testcase_27 AC 4 ms
11,984 KB
testcase_28 AC 3 ms
11,984 KB
testcase_29 AC 3 ms
11,984 KB
testcase_30 AC 3 ms
11,984 KB
testcase_31 AC 3 ms
11,984 KB
testcase_32 AC 3 ms
11,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2639.cc:  No.2639 Longest Increasing Walk - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_H = 500;
const int MAX_W = 500;
const int MAX_N = MAX_H * MAX_W;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;

/* global variables */

int as[MAX_N], pns[MAX_N], ds[MAX_N];
vi nbrs[MAX_N];

/* subroutines */

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  int n = h * w;
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  for (int i = 0, u = 0; i < h; i++)
    for (int j = 0; j < w; j++, u++) {
      if (i + 1 < h) {
	int v = u + w;
	if (as[u] < as[v]) nbrs[u].push_back(v), pns[v]++;
	else if (as[u] > as[v]) nbrs[v].push_back(u), pns[u]++;
      }
      if (j + 1 < w) {
	int v = u + 1;
	if (as[u] < as[v]) nbrs[u].push_back(v), pns[v]++;
	else if (as[u] > as[v]) nbrs[v].push_back(u), pns[u]++;
      }
    }

  qi q;
  for (int u = 0; u < n; u++)
    if (pns[u] == 0) q.push(u), ds[u] = 1;

  int maxd = 0;
  while (! q.empty()) {
    int u = q.front(); q.pop();
    maxd = max(maxd, ds[u]);
    
    for (auto v: nbrs[u]) {
      ds[v] = max(ds[v], ds[u] + 1);
      if (--pns[v] == 0) q.push(v);
    }
  }

  printf("%d\n", maxd);

  return 0;
}
0