結果

問題 No.1296 OR or NOR
ユーザー semisagisemisagi
提出日時 2020-11-20 23:14:36
言語 Swift
(5.8.1)
結果
AC  
実行時間 2,116 ms / 3,000 ms
コード長 1,896 bytes
コンパイル時間 5,557 ms
コンパイル使用メモリ 140,036 KB
実行使用メモリ 41,656 KB
最終ジャッジ日時 2023-09-30 20:01:04
合計ジャッジ時間 44,051 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,160 KB
testcase_01 AC 3 ms
8,056 KB
testcase_02 AC 606 ms
39,212 KB
testcase_03 AC 582 ms
38,540 KB
testcase_04 AC 1,198 ms
39,756 KB
testcase_05 AC 894 ms
40,096 KB
testcase_06 AC 851 ms
39,896 KB
testcase_07 AC 816 ms
36,616 KB
testcase_08 AC 826 ms
38,544 KB
testcase_09 AC 860 ms
37,940 KB
testcase_10 AC 915 ms
39,228 KB
testcase_11 AC 780 ms
39,616 KB
testcase_12 AC 759 ms
37,952 KB
testcase_13 AC 812 ms
39,820 KB
testcase_14 AC 2,112 ms
41,024 KB
testcase_15 AC 2,116 ms
41,052 KB
testcase_16 AC 1,997 ms
40,232 KB
testcase_17 AC 704 ms
40,224 KB
testcase_18 AC 721 ms
41,656 KB
testcase_19 AC 700 ms
40,620 KB
testcase_20 AC 615 ms
34,220 KB
testcase_21 AC 612 ms
35,484 KB
testcase_22 AC 519 ms
36,336 KB
testcase_23 AC 517 ms
35,692 KB
testcase_24 AC 568 ms
36,844 KB
testcase_25 AC 559 ms
34,668 KB
testcase_26 AC 623 ms
34,632 KB
testcase_27 AC 617 ms
35,104 KB
testcase_28 AC 590 ms
35,576 KB
testcase_29 AC 609 ms
34,280 KB
testcase_30 AC 1,970 ms
41,264 KB
testcase_31 AC 2,041 ms
40,600 KB
testcase_32 AC 2,029 ms
40,732 KB
testcase_33 AC 1,958 ms
40,100 KB
testcase_34 AC 1,990 ms
40,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

struct Scanner {
    private var elements = [String]()
    private var index = 0

    mutating func peek() -> String {
        while elements.count == index {
            elements = readLine()!.split(separator: " ").map(String.init)
            index = 0
        }
        return elements[index]
    }

    mutating func next() -> String {
        defer { index += 1 }
        return peek()
    }

    mutating func nextInt() -> Int {
        return Int(next())!
    }

    mutating func nextInts(_ n: Int) -> [Int] {
        return (0 ..< n).map { _ in nextInt() }
    }

    mutating func nextDouble() -> Double {
        return Double(next())!
    }
}

extension Int {
    func test(_ k: Int) -> Bool {
        return self >> k & 1 == 1
    }
}

var scanner = Scanner()

let N = scanner.nextInt()
let A = scanner.nextInts(N)
let Q = scanner.nextInt()
let B = scanner.nextInts(Q)

var last = [Int?](repeating: nil, count: 60)
for i in 0 ..< 60 {
    for j in (0 ..< N).reversed() {
        if A[j].test(i) {
            last[i] = j
            break
        }
    }
}

func solve(_ b: Int) -> Int? {
    var constraints = [Int: Bool]()
    for i in last.indices {
        if let last = last[i] {
            if let c = constraints[last] {
                if !b.test(i) != c {
                    return nil
                }
            } else {
                constraints[last] = !b.test(i)
            }
        } else {
            if let c = constraints[0] {
                if b.test(i) != c {
                    return nil
                }
            } else {
                constraints[0] = b.test(i)
            }
        }
    }
    var answer = 0
    var x = false
    for (_, y) in constraints.sorted(by: { $0.key > $1.key }) {
        if x != y {
            answer += 1
            x = y
        }
    }
    return answer
}

for k in 0 ..< Q {
    print(solve(B[k]) ?? -1)
}

0