結果
問題 | No.1029 JJOOII 3 |
ユーザー | aru aru |
提出日時 | 2020-10-30 21:57:26 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,242 bytes |
コンパイル時間 | 12,991 ms |
コンパイル使用メモリ | 225,460 KB |
実行使用メモリ | 211,328 KB |
最終ジャッジ日時 | 2024-07-22 00:31:37 |
合計ジャッジ時間 | 22,511 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 7 ms
12,032 KB |
testcase_01 | AC | 4 ms
6,912 KB |
testcase_02 | AC | 10 ms
17,280 KB |
testcase_03 | AC | 42 ms
56,320 KB |
testcase_04 | AC | 40 ms
56,320 KB |
testcase_05 | AC | 251 ms
208,768 KB |
testcase_06 | AC | 282 ms
211,328 KB |
testcase_07 | AC | 231 ms
206,208 KB |
testcase_08 | AC | 247 ms
206,208 KB |
testcase_09 | AC | 228 ms
206,208 KB |
testcase_10 | AC | 233 ms
201,088 KB |
testcase_11 | AC | 249 ms
206,208 KB |
testcase_12 | AC | 227 ms
203,648 KB |
testcase_13 | AC | 227 ms
206,208 KB |
testcase_14 | AC | 244 ms
211,328 KB |
testcase_15 | AC | 233 ms
208,768 KB |
testcase_16 | AC | 220 ms
200,960 KB |
testcase_17 | AC | 220 ms
201,088 KB |
testcase_18 | AC | 218 ms
200,960 KB |
testcase_19 | WA | - |
testcase_20 | AC | 146 ms
141,568 KB |
testcase_21 | AC | 143 ms
136,448 KB |
testcase_22 | AC | 150 ms
136,448 KB |
testcase_23 | AC | 138 ms
139,008 KB |
testcase_24 | AC | 143 ms
136,448 KB |
testcase_25 | AC | 152 ms
141,568 KB |
testcase_26 | AC | 237 ms
190,720 KB |
testcase_27 | AC | 273 ms
203,648 KB |
testcase_28 | AC | 243 ms
201,088 KB |
testcase_29 | AC | 227 ms
198,400 KB |
testcase_30 | AC | 261 ms
206,208 KB |
testcase_31 | AC | 241 ms
198,528 KB |
testcase_32 | AC | 478 ms
211,328 KB |
testcase_33 | AC | 484 ms
211,328 KB |
testcase_34 | AC | 486 ms
211,328 KB |
testcase_35 | AC | 480 ms
211,328 KB |
testcase_36 | AC | 482 ms
211,328 KB |
testcase_37 | WA | - |
testcase_38 | AC | 7 ms
12,032 KB |
testcase_39 | AC | 8 ms
14,720 KB |
testcase_40 | WA | - |
ソースコード
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, 110000) } for j := 0; j < 110000; 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 < 110000; 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, 110000) } for j := 0; j < 110000; 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 < 110000; 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, 110000) } for j := 0; j < 110000; 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 < 110000; 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]) } } } for i := 100100; i >= 0; i-- { if dp0[N][i] > dp0[N][i+1] { dp0[N][i] = dp0[N][i+1] } if dp1[N][i] > dp1[N][i+1] { dp1[N][i] = dp1[N][i+1] } if dp2[N][i] > dp2[N][i+1] { dp2[N][i] = dp2[N][i+1] } } // out(dp0[N][:K+1]) // out(dp1[N][:K+1]) // out(dp2[N][:K+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) }