結果

問題 No.2094 Symmetry
ユーザー HimaHima
提出日時 2022-10-09 14:49:14
言語 Go
(1.22.1)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,644 bytes
コンパイル時間 11,772 ms
コンパイル使用メモリ 214,164 KB
実行使用メモリ 14,396 KB
最終ジャッジ日時 2023-09-05 19:01:22
合計ジャッジ時間 17,156 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 105 ms
14,368 KB
testcase_10 AC 105 ms
14,364 KB
testcase_11 AC 105 ms
14,364 KB
testcase_12 AC 105 ms
14,364 KB
testcase_13 AC 80 ms
14,368 KB
testcase_14 AC 105 ms
14,368 KB
testcase_15 AC 105 ms
14,364 KB
testcase_16 AC 105 ms
14,368 KB
testcase_17 AC 81 ms
14,364 KB
testcase_18 AC 80 ms
14,368 KB
testcase_19 AC 105 ms
14,368 KB
testcase_20 AC 105 ms
14,364 KB
testcase_21 AC 104 ms
14,364 KB
testcase_22 AC 105 ms
14,364 KB
testcase_23 AC 80 ms
14,368 KB
testcase_24 AC 80 ms
14,368 KB
testcase_25 AC 104 ms
14,368 KB
testcase_26 AC 104 ms
14,364 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 36 ms
14,392 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 37 ms
14,396 KB
testcase_31 AC 36 ms
14,396 KB
testcase_32 AC 37 ms
14,372 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 31 ms
14,364 KB
testcase_35 AC 27 ms
14,392 KB
testcase_36 AC 39 ms
14,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n, k := nextInt(), nextInt()
	s := make([]string, 2*n)
	for i := 0; i < 2*n; i++ {
		s[i] = nextString()
	}
	c := make([][]int, 2*n)
	for i := 0; i < 2*n; i++ {
		c[i] = nextIntSlice(2 * n)
	}
	ans := solve(n, k, s, c)
	PrintInt(ans)
}

func solve(n, k int, s []string, c [][]int) int {
	//Ci, jが大きい順に黒くする
	nb := 0
	var cs []int
	for i := 0; i < 2*n; i++ {
		for j := 0; j < 2*n; j++ {
			if s[i][j] == '#' {
				nb++
			}
			cs = append(cs, c[i][j])
		}
	}
	sort.Slice(cs, func(i, j int) bool {
		return cs[i] > cs[j]
	})
	ans := 0
	for i := 0; i < nb; i++ {
		ans += cs[i]
	}
	//シンメトリーが作れないのでここで終了
	if nb%2 == 1 {
		return ans
	}
	//シンメトリーのスコアと比較する
	var hcs []int
	for i := 0; i < 2*n; i++ {
		for j := 0; j < n; j++ {
			hcs = append(hcs, c[i][j]+c[i][2*n-1-j])
		}
	}
	ans2 := k
	sort.Slice(hcs, func(i, j int) bool {
		return hcs[i] > hcs[j]
	})
	for i := 0; i < nb/2; i++ {
		ans2 += hcs[i]
	}
	return Max(ans, ans2)
}

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func nextIntSlice(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = nextInt()
	}
	return s
}

func nextString() string {
	sc.Scan()
	return sc.Text()
}

func PrintInt(x int) {
	defer out.Flush()
	fmt.Fprintln(out, x)
}

func Max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
0