結果
| 問題 |
No.421 しろくろチョコレート
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-02-05 06:28:15 |
| 言語 | Nim (2.2.0) |
| 結果 |
AC
|
| 実行時間 | 12 ms / 2,000 ms |
| コード長 | 1,979 bytes |
| コンパイル時間 | 2,721 ms |
| コンパイル使用メモリ | 64,888 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-23 07:22:37 |
| 合計ジャッジ時間 | 4,257 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 65 |
ソースコード
import sequtils
template times*(n:int,body) = (for _ in 0..<n: body)
template `max=`*(x,y) = x = max(x,y)
template `min=`*(x,y) = x = min(x,y)
type BipartiteMatch = seq[seq[int]]
proc initBipartiteMatch(maxSize:int): BipartiteMatch = newSeqWith(maxSize,newSeq[int]())
proc add(B:var BipartiteMatch,src,dst:int) = (B[dst] &= src;B[src] &= dst)
proc bipartiteMatching(B:var BipartiteMatch) : int =
var match = newSeqWith(B.len,-1)
var used : seq[bool]
proc dfs(B:var BipartiteMatch,src:int) : bool =
# 交互にペアを結んでいく
used[src] = true
for dst in B[src]:
if match[dst] >= 0 :
if used[match[dst]] : continue
if not B.dfs(match[dst]) : continue
match[src] = dst
match[dst] = src
return true
return false
for src in 0..<B.len:
if match[src] >= 0 : continue
used = newSeq[false](B.len)
if B.dfs(src) : result += 1
proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
while true:
let k = getchar_unlocked()
if k < '0': return
result = 10 * result + k.ord - '0'.ord
let h = scan()
let w = scan()
proc encode(x,y:int):int = x * h + y
let n = h * w
var C = newSeqWith(w,newSeqWith(h,'.'))
for y in 0..<h:
for x in 0..<w:
C[x][y] = getchar_unlocked()
discard getchar_unlocked()
var F = initBipartiteMatch(n)
var wSum = 0
var bSum = 0
for x in 0..<w:
for y in 0..<h:
if C[x][y] == '.' : continue
const dxdy4 :seq[tuple[x,y:int]] = @[(0,1),(1,0),(0,-1),(-1,0)]
if C[x][y] == 'w' : wSum += 1
else : bSum += 1
if C[x][y] != 'w' : continue
for d in dxdy4:
let nx = x + d.x
let ny = y + d.y
if nx < 0 or ny < 0 or nx >= w or ny >= h : continue
if C[nx][ny] == '.' : continue
F.add(encode(x,y),encode(nx,ny))
var ans = F.bipartiteMatching()
wSum -= ans
bSum -= ans
ans *= 100
let pair = min(bSum,wSum)
ans = ans + 10 * pair
wSum -= pair
bSum -= pair
echo ans + wSum + bSum