結果

問題 No.4 おもりと天秤
ユーザー keibkeib
提出日時 2019-03-06 10:33:21
言語 Swift
(5.8.1)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,021 bytes
コンパイル時間 2,680 ms
コンパイル使用メモリ 178,456 KB
実行使用メモリ 13,844 KB
最終ジャッジ日時 2023-08-20 12:02:42
合計ジャッジ時間 3,443 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,236 KB
testcase_01 AC 7 ms
13,200 KB
testcase_02 AC 6 ms
13,708 KB
testcase_03 AC 6 ms
13,532 KB
testcase_04 AC 6 ms
13,232 KB
testcase_05 AC 7 ms
13,476 KB
testcase_06 AC 6 ms
13,236 KB
testcase_07 AC 6 ms
13,324 KB
testcase_08 AC 6 ms
13,772 KB
testcase_09 AC 7 ms
13,844 KB
testcase_10 AC 6 ms
13,348 KB
testcase_11 AC 6 ms
13,492 KB
testcase_12 AC 7 ms
13,488 KB
testcase_13 AC 6 ms
13,840 KB
testcase_14 AC 6 ms
13,496 KB
testcase_15 AC 7 ms
13,308 KB
testcase_16 AC 6 ms
13,244 KB
testcase_17 AC 6 ms
13,816 KB
testcase_18 AC 7 ms
13,324 KB
testcase_19 AC 7 ms
13,840 KB
testcase_20 AC 6 ms
13,256 KB
testcase_21 AC 6 ms
13,812 KB
testcase_22 AC 7 ms
13,252 KB
権限があれば一括ダウンロードができます

ソースコード

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