結果

問題 No.1668 Grayscale
ユーザー taiga0629kyoprotaiga0629kyopro
提出日時 2021-09-03 23:17:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,609 bytes
コンパイル時間 249 ms
コンパイル使用メモリ 82,460 KB
実行使用メモリ 353,932 KB
最終ジャッジ日時 2024-05-09 07:05:05
合計ジャッジ時間 9,321 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 30 ms
52,992 KB
testcase_02 AC 30 ms
52,608 KB
testcase_03 AC 33 ms
52,480 KB
testcase_04 WA -
testcase_05 AC 30 ms
52,480 KB
testcase_06 WA -
testcase_07 AC 893 ms
353,872 KB
testcase_08 AC 167 ms
85,544 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 32 ms
52,608 KB
testcase_19 AC 30 ms
52,480 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 32 ms
52,480 KB
testcase_23 AC 30 ms
52,608 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
            if c[i][j]==c[i+di][j+dj]:continue
            if c[i][j]>c[i+di][j+dj]:
                root[c[i+di][j+dj]].append(c[i][j])
                tp.add(c[i+di][j+dj],c[i][j])
            else:
                root[c[i][j]].append(c[i+di][j+dj])
                tp.add(c[i][j ], c[i+di][j+dj])

p=tp.const()
p.reverse()
dp=[1]*(n+1)
for x in p:
    for y in root[x]:
        dp[x]=max(dp[x],dp[y]+1)

print(max(dp))
0