結果

問題 No.4 おもりと天秤
コンテスト
ユーザー keib
提出日時 2019-03-06 10:33:21
言語 Swift
(6.2.4)
コンパイル:
swiftc _filename_ -Ounchecked -o a.out
実行:
./a.out
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,021 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,400 ms
コンパイル使用メモリ 230,240 KB
実行使用メモリ 17,920 KB
最終ジャッジ日時 2026-05-24 05:09:23
合計ジャッジ時間 3,335 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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