結果

問題 No.1668 Grayscale
ユーザー taiga0629kyoprotaiga0629kyopro
提出日時 2021-09-03 23:33:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,532 ms / 4,000 ms
コード長 1,401 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 364,880 KB
最終ジャッジ日時 2024-05-09 07:39:37
合計ジャッジ時間 6,519 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,608 KB
testcase_01 AC 38 ms
52,352 KB
testcase_02 AC 38 ms
52,608 KB
testcase_03 AC 36 ms
52,352 KB
testcase_04 AC 35 ms
52,480 KB
testcase_05 AC 35 ms
52,352 KB
testcase_06 AC 697 ms
364,880 KB
testcase_07 AC 706 ms
364,628 KB
testcase_08 AC 336 ms
239,468 KB
testcase_09 AC 1,532 ms
353,484 KB
testcase_10 AC 609 ms
237,000 KB
testcase_11 AC 50 ms
63,872 KB
testcase_12 AC 71 ms
73,984 KB
testcase_13 AC 102 ms
89,472 KB
testcase_14 AC 224 ms
126,424 KB
testcase_15 AC 148 ms
90,752 KB
testcase_16 AC 124 ms
84,352 KB
testcase_17 AC 95 ms
84,264 KB
testcase_18 AC 36 ms
52,608 KB
testcase_19 AC 36 ms
52,224 KB
testcase_20 AC 58 ms
68,352 KB
testcase_21 AC 72 ms
73,728 KB
testcase_22 AC 36 ms
52,352 KB
testcase_23 AC 36 ms
52,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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])
0