結果

問題 No.1668 Grayscale
ユーザー taiga0629kyoprotaiga0629kyopro
提出日時 2021-09-03 23:33:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,401 ms / 4,000 ms
コード長 1,401 bytes
コンパイル時間 456 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 366,824 KB
最終ジャッジ日時 2023-08-22 01:44:54
合計ジャッジ時間 7,915 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,268 KB
testcase_01 AC 69 ms
71,524 KB
testcase_02 AC 69 ms
71,556 KB
testcase_03 AC 69 ms
71,608 KB
testcase_04 AC 71 ms
71,592 KB
testcase_05 AC 70 ms
71,420 KB
testcase_06 AC 766 ms
366,504 KB
testcase_07 AC 785 ms
366,824 KB
testcase_08 AC 384 ms
241,772 KB
testcase_09 AC 1,401 ms
362,920 KB
testcase_10 AC 648 ms
235,724 KB
testcase_11 AC 84 ms
76,252 KB
testcase_12 AC 106 ms
77,960 KB
testcase_13 AC 131 ms
89,236 KB
testcase_14 AC 280 ms
133,276 KB
testcase_15 AC 177 ms
90,484 KB
testcase_16 AC 159 ms
84,928 KB
testcase_17 AC 128 ms
84,588 KB
testcase_18 AC 72 ms
71,532 KB
testcase_19 AC 72 ms
71,320 KB
testcase_20 AC 94 ms
76,892 KB
testcase_21 AC 111 ms
78,264 KB
testcase_22 AC 72 ms
71,388 KB
testcase_23 AC 72 ms
71,232 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