結果

問題 No.130 XOR Minimax
ユーザー code-devocode-devo
提出日時 2016-02-06 08:36:19
言語 Go
(1.22.1)
結果
AC  
実行時間 85 ms / 5,000 ms
コード長 975 bytes
コンパイル時間 11,340 ms
コンパイル使用メモリ 212,036 KB
実行使用メモリ 10,220 KB
最終ジャッジ日時 2023-10-10 00:12:42
合計ジャッジ時間 13,505 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
8,376 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,372 KB
testcase_04 AC 29 ms
10,172 KB
testcase_05 AC 34 ms
10,176 KB
testcase_06 AC 39 ms
10,172 KB
testcase_07 AC 43 ms
10,220 KB
testcase_08 AC 84 ms
8,420 KB
testcase_09 AC 10 ms
8,260 KB
testcase_10 AC 14 ms
8,392 KB
testcase_11 AC 42 ms
8,312 KB
testcase_12 AC 6 ms
7,956 KB
testcase_13 AC 36 ms
8,116 KB
testcase_14 AC 85 ms
8,316 KB
testcase_15 AC 3 ms
4,372 KB
testcase_16 AC 60 ms
8,332 KB
testcase_17 AC 60 ms
8,400 KB
testcase_18 AC 61 ms
8,336 KB
testcase_19 AC 74 ms
8,284 KB
testcase_20 AC 37 ms
8,388 KB
testcase_21 AC 80 ms
8,308 KB
testcase_22 AC 19 ms
8,088 KB
testcase_23 AC 6 ms
5,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// maskの計算をシフト演算にしたら速くなった。

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func calc(a []int, b int) int {
	if b < 0 {
		return a[0]
	}

	mask := 1 << (uint(b))
	p0 := make([]int, 0)
	p1 := make([]int, 0)
	for i := 0; i < len(a); i++ {
		num := a[i];
		if num & mask == 0 {
			p0 = append(p0, num)
		} else {
			p1 = append(p1, num)
		}
	}

	if len(p0) == 0 {
		for i := 0; i < len(p1); i++ {
			p1[i] ^= mask;
		}
		return calc(p1, b - 1)
	} else if len(p1) == 0 {
		return calc(p0, b - 1)
	} else {
		for i := 0; i < len(p0); i++ {
			p0[i] ^= mask;
		}
		v1 := calc(p0, b - 1)
		v2 := calc(p1, b - 1)
		if v1 < v2 {
			return v1
		} else {
			return v2
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan(); n, _ := strconv.Atoi(sc.Text())

	a := make([]int, n)
	for i := 0; i < n; i++ {
		sc.Scan(); num, _ := strconv.Atoi(sc.Text())
		a[i] = num
	}

	fmt.Println(calc(a, 30))
}
0