結果
| 問題 |
No.1296 OR or NOR
|
| コンテスト | |
| ユーザー |
semisagi
|
| 提出日時 | 2020-11-20 23:14:36 |
| 言語 | Swift (6.0.3) |
| 結果 |
AC
|
| 実行時間 | 2,236 ms / 3,000 ms |
| コード長 | 1,896 bytes |
| コンパイル時間 | 9,175 ms |
| コンパイル使用メモリ | 135,688 KB |
| 実行使用メモリ | 42,844 KB |
| 最終ジャッジ日時 | 2024-07-23 13:49:40 |
| 合計ジャッジ時間 | 49,689 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
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)
}
semisagi