結果
問題 | No.2639 Longest Increasing Walk |
ユーザー | kokosei |
提出日時 | 2024-02-19 21:43:35 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 44 ms / 2,000 ms |
コード長 | 1,474 bytes |
コンパイル時間 | 2,777 ms |
コンパイル使用メモリ | 252,008 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-09-29 01:38:31 |
合計ジャッジ時間 | 4,536 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; int H, W; int A[510][510]; int indeg[510][510]; int df[5] = {0, 1, 0, -1}; int dist[510][510]; bool inside(int x, int y){ return 0 <= x && x < H && 0 <= y && y < W; } int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> H >> W; for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ cin >> A[i][j]; } } for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ for(int d = 0;d < 4;d++){ int x = i + df[d], y = j + df[d + 1]; if(!inside(x, y))continue; if(A[x][y] < A[i][j])indeg[i][j]++; } } } queue<pair<int, int>> que; for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ if(indeg[i][j] == 0)que.push({i, j}); } } while(que.size()){ auto [x, y] = que.front(); que.pop(); for(int d = 0;d < 4;d++){ int nx = x + df[d], ny = y + df[d + 1]; if(!inside(nx, ny))continue; if(A[x][y] >= A[nx][ny])continue; dist[nx][ny] = max(dist[nx][ny], dist[x][y] + 1); if(--indeg[nx][ny] == 0){ que.push({nx, ny}); } } } int ans = 0; for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ ans = max(ans, dist[i][j]); } } cout << ans + 1 << endl; return 0; }