結果

問題 No.1029 JJOOII 3
ユーザー aru aruaru aru
提出日時 2020-10-30 21:43:35
言語 Go
(1.22.1)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 2,552 bytes
コンパイル時間 11,374 ms
コンパイル使用メモリ 215,916 KB
実行使用メモリ 5,492 KB
最終ジャッジ日時 2023-09-29 05:36:53
合計ジャッジ時間 14,005 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 8 ms
5,480 KB
testcase_05 AC 39 ms
5,484 KB
testcase_06 AC 26 ms
5,472 KB
testcase_07 AC 26 ms
5,480 KB
testcase_08 AC 30 ms
5,484 KB
testcase_09 AC 31 ms
5,476 KB
testcase_10 AC 37 ms
5,480 KB
testcase_11 AC 36 ms
5,488 KB
testcase_12 AC 25 ms
5,468 KB
testcase_13 AC 37 ms
5,484 KB
testcase_14 AC 39 ms
5,488 KB
testcase_15 AC 11 ms
5,472 KB
testcase_16 AC 3 ms
5,484 KB
testcase_17 AC 29 ms
5,488 KB
testcase_18 AC 45 ms
5,484 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 3 ms
4,384 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 46 ms
5,488 KB
testcase_33 AC 36 ms
5,488 KB
testcase_34 AC 44 ms
5,488 KB
testcase_35 AC 41 ms
5,488 KB
testcase_36 AC 39 ms
5,492 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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)
	for i := 0; i < N; i++ {
		s[i] = getS()
		c[i] = getI()
	}

	cntj := make([]int, N)
	cnto := make([]int, N)
	cnti := make([]int, N)
	for i := 0; i < N; i++ {
		cntj[i] = strings.Count(s[i], "J")
		cnto[i] = strings.Count(s[i], "O")
		cnti[i] = strings.Count(s[i], "I")
	}

	cost := make([]int, K*3+1)
	for i := 0; i < len(cost); i++ {
		cost[i] += inf
	}
	cost[0] = 0
	for i := 0; i < K*3; i++ {
		{
			if cost[i] < cost[i+1] {
				for j := 0; j < N; j++ {
					if i+100 < K {
						cost[i+cntj[j]] = min(cost[i+cntj[j]], cost[i]+c[j])
					} else if i >= K && i+100 < K*2 {
						cost[i+cnto[j]] = min(cost[i+cnto[j]], cost[i]+c[j])
					} else if i >= K*2 && i+100 < K*3 {
						cost[i+cnti[j]] = min(cost[i+cnti[j]], cost[i]+c[j])
					} else {
						now := i
						for _, c := range s[j] {
							if now < K && c == 'J' {
								now++
							}
							if now >= K && now < K*2 && c == 'O' {
								now++
							}
							if now >= K*2 && now < K*3 && c == 'I' {
								now++
							}
						}
						cost[now] = min(cost[now], cost[i]+c[j])
					}
				}
			}
		}
	}
	if cost[len(cost)-1] == inf {
		out(-1)
		return
	}
	out(cost[len(cost)-1])
}
0