結果

問題 No.4 おもりと天秤
ユーザー keib
提出日時 2019-03-06 10:33:21
言語 Swift
(6.0.3)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 1,021 bytes
コンパイル時間 1,798 ms
コンパイル使用メモリ 185,588 KB
実行使用メモリ 15,616 KB
最終ジャッジ日時 2024-11-30 13:42:06
合計ジャッジ時間 2,830 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import Foundation

let n = Int(readLine()!)!
let weights = readLine()!.split(separator:" ").map { Int($0)! }
var weightSum = 0
var maxWeight = 0
weights.forEach { weight in
    weightSum += weight
    maxWeight = Swift.max(weight, maxWeight)
}
if weightSum % 2 != 0 {
    print("impossible")
    exit(0)
}
let goal = weightSum / 2
if maxWeight > goal {
    print("impossible")
    exit(0)
}

var possibilities = Array<Int>(repeating: -1, count: goal + 1)

weights.enumerated().forEach { i, weight in 
    if possibilities[weight] < 0  {
        possibilities[weight] = i
    }
    for j in 1 ..< Swift.min(goal - weight + 1, possibilities.count) {
        if possibilities[j] < 0 {
            continue
        }
        if possibilities[j] >= i {
            continue
        }
        if possibilities[j + weight] >= 0  {
            continue
        }
        if j + weight == goal {
            print("possible")
            exit(0)
        }
        possibilities[j + weight] = i
    }
}
print("impossible")
exit(0)
0