結果

問題 No.1029 JJOOII 3
ユーザー aru aruaru aru
提出日時 2020-10-30 21:26:30
言語 Go
(1.22.1)
結果
RE  
実行時間 -
コード長 3,945 bytes
コンパイル時間 14,075 ms
コンパイル使用メモリ 212,556 KB
実行使用メモリ 24,764 KB
最終ジャッジ日時 2023-09-29 04:45:56
合計ジャッジ時間 15,821 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
5,480 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 73 ms
16,356 KB
testcase_22 AC 84 ms
16,364 KB
testcase_23 AC 69 ms
18,484 KB
testcase_24 AC 76 ms
16,428 KB
testcase_25 AC 83 ms
18,484 KB
testcase_26 AC 117 ms
22,656 KB
testcase_27 AC 142 ms
24,740 KB
testcase_28 AC 119 ms
24,740 KB
testcase_29 AC 105 ms
24,736 KB
testcase_30 WA -
testcase_31 AC 119 ms
24,736 KB
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 2 ms
4,376 KB
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

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

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func getI() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getI()
	}
	return ret
}

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

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

const inf = int(1e18)

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	N, K := getI(), getI()
	s := make([]string, N)
	c := make([]int, N+1)
	for i := 0; i < N; i++ {
		s[i] = getS()
		c[i] = getI()
	}

	// DP0
	dp0 := make([][]int, N+1)
	for i := 0; i <= N; i++ {
		dp0[i] = make([]int, 11000)
	}
	for j := 0; j < 11000; j++ {
		dp0[0][j] = inf
	}
	dp0[0][0] = 0
	for i := 1; i <= N; i++ {
		n := strings.Count(s[i-1], "J")
		for j := 0; j < 11000; j++ {
			dp0[i][j] = dp0[i-1][j]
			if j-n >= 0 {
				dp0[i][j] = min(dp0[i-1][j], dp0[i][j-n]+c[i-1])
			}
		}
	}

	// DP1
	dp1 := make([][]int, N+1)
	for i := 0; i <= N; i++ {
		dp1[i] = make([]int, 11000)
	}
	for j := 0; j < 11000; j++ {
		dp1[0][j] = inf
	}
	dp1[0][0] = 0
	for i := 1; i <= N; i++ {
		n := strings.Count(s[i-1], "O")
		for j := 0; j < 11000; j++ {
			dp1[i][j] = dp1[i-1][j]
			if j-n >= 0 {
				dp1[i][j] = min(dp1[i-1][j], dp1[i][j-n]+c[i-1])
			}
		}
	}

	// DP2
	dp2 := make([][]int, N+1)
	for i := 0; i <= N; i++ {
		dp2[i] = make([]int, 11000)
	}
	for j := 0; j < 11000; j++ {
		dp2[0][j] = inf
	}
	dp2[0][0] = 0
	for i := 1; i <= N; i++ {
		n := strings.Count(s[i-1], "I")
		for j := 0; j < 11000; j++ {
			dp2[i][j] = dp2[i-1][j]
			if j-n >= 0 {
				dp2[i][j] = min(dp2[i-1][j], dp2[i][j-n]+c[i-1])
			}
		}
	}

	J := make([]int, N)
	O := make([]int, N)
	I := make([]int, N)
	for i := 0; i < N; i++ {
		J[i] = strings.Count(s[i], "J")
		O[i] = strings.Count(s[i], "O")
		I[i] = strings.Count(s[i], "I")
	}
	// ここに記述;N文字目でJ,O,Iがいくつ出現しているかの累積を計算しておく
	s = append(s, "")
	c = append(c, 0)
	// out(dp0[N][:10])
	// out(dp1[N][:10])
	// out(dp2[N][:10])

	ans := inf
	for i := 0; i <= N; i++ {
		for j := 0; j <= N; j++ {
			j0 := 0
			o0 := 0
			if i != N {
				o0 = O[i]
			}
			for k := 0; k <= len(s[i]); k++ {
				o1 := 0
				i1 := 0
				if j != N {
					i1 = I[j]
				}
				for l := 0; l <= len(s[j]); l++ {
					numJ := max(0, K-j0)
					numO := max(0, K-o0-o1)
					numI := max(0, K-i1)
					cost := c[i] + c[j] + dp0[N][numJ] + dp1[N][numO] + dp2[N][numI]
					ans = min(ans, cost)
					// out(s[i][0:k], s[i][k:len(s[i])], j0, o0,
					// 	":",
					// 	s[j][0:l], s[j][l:len(s[j])], o1, i1,
					// 	"JOI", numJ, numO, numI, "DP",
					// 	dp0[N][numJ], dp1[N][numO], dp2[N][numI],
					// 	"C", c[i], c[j],
					// 	cost)
					if l != len(s[j]) {
						if s[j][l] == 'O' {
							o1++
						}
						if s[j][l] == 'I' {
							i1--
						}
					}
				}
				if k != len(s[i]) {
					if s[i][k] == 'J' {
						j0++
					}
					if s[i][k] == 'O' {
						o0--
					}
				}
			}
		}
	}

	if ans == inf {
		out(-1)
		return
	}
	out(ans)
}
0