結果

問題 No.43 野球の試合
ユーザー ccppjsrbccppjsrb
提出日時 2020-07-09 10:55:43
言語 Go
(1.22.1)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 2,381 bytes
コンパイル時間 16,401 ms
コンパイル使用メモリ 225,168 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 02:44:49
合計ジャッジ時間 15,966 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 28 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	ss := make([]string, n)
	for i := 0; i < n; i++ {
		ss[i] = getNextString(scanner)
	}
	b := 0
	fixed := make([]int, n)
	gg := make([]game, 0)
	for i := 0; i < n; i++ {
		for j := 0; j < i; j++ {
			switch ss[i][j] {
			case 'o':
				fixed[i]++
			case 'x':
				fixed[j]++
			case '-':
				b++
				gg = append(gg, game{y: i, x: j})
			}
		}
	}
	var ans int
	ans = n

	for i := 0; i < 1<<uint(b); i++ {
		win := make([]int, n)
		for j := 0; j < b; j++ {
			if i>>uint(j)&1 == 0 {
				win[gg[j].y]++
			} else {
				win[gg[j].x]++
			}
		}
		for j := 0; j < n; j++ {
			win[j] += fixed[j]
		}
		c := newCompress(win)
		if ans > c.get(win[0]) {
			ans = c.get(win[0])
		}
	}
	fmt.Fprintln(writer, ans+1)
}

type compress map[int]int

func newCompress(aa []int) compress {
	n := len(aa)
	bb := make([]int, n)
	copy(bb, aa)
	sort.SliceStable(bb, func(i, j int) bool {
		return bb[i] > bb[j]
	})
	c := compress{}
	l := 0
	i := 0
	for r := 0; r < n; r++ {
		for r > l && bb[r] != bb[l] {
			l++
		}
		if r == l {
			c[bb[l]] = i
			i++
		}
	}
	return c
}
func (c compress) get(x int) int {
	return c[x]
}

type game struct {
	y, x int
}
0