結果
| 問題 |
No.1668 Grayscale
|
| コンテスト | |
| ユーザー |
taiga0629kyopro
|
| 提出日時 | 2021-09-03 23:33:15 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,464 ms / 4,000 ms |
| コード長 | 1,401 bytes |
| コンパイル時間 | 405 ms |
| コンパイル使用メモリ | 82,560 KB |
| 実行使用メモリ | 364,976 KB |
| 最終ジャッジ日時 | 2024-12-15 18:35:06 |
| 合計ジャッジ時間 | 6,425 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
h,w,n=map(int,input().split())
c=[[] for i in range(h)]
for i in range(h):
c[i]=list(map(int,input().split()))
root=[[] for i in range(n+1)]
class topological_sort():
#1-indexed
#0-indexedはバグる
def __init__(self,n):
self.n=n
self.root=[[] for _ in range(self.n+1)]
self.deg=[0]*(self.n+1)
def add(self,a,b):
#有向辺 a->b の追加
self.root[a].append(b)
self.deg[b]+=1
def const(self):
#トポロジカルソートした結果を返す
#不可能なら [] を返す
deg2=self.deg[:]
search=[]
res=[]
for i in range(1,self.n+1):
if deg2[i]==0:search.append(i)
while search:
now=search.pop()
res.append(now)
for next in self.root[now]:
deg2[next]-=1
if deg2[next]==0:
search.append(next)
if len(res)==self.n:
return res
return []
delta=[(1,0),(-1,0),(0,1),(0,-1)]
tp=topological_sort(n)
for i in range(h):
for j in range(w):
for di,dj in delta:
if i+di<0 or i+di>=h or j+dj<0 or j+dj>=w:continue
root[c[i][j]].append(c[i+di][j+dj])
p=tp.const()
p.reverse()
dp=[10**18]*(n+1)
dp[1]=1
for i in range(2,n+1):
dp[i]=dp[i-1]
for x in root[i]:
if x<i:dp[i]=max(dp[i],dp[x]+1)
print(dp[n])
taiga0629kyopro