結果

問題 No.1037 exhausted
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-10 17:56:21
言語 Go
(1.22.1)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 3,295 bytes
コンパイル時間 11,556 ms
コンパイル使用メモリ 214,092 KB
実行使用メモリ 36,972 KB
最終ジャッジ日時 2023-09-05 14:42:45
合計ジャッジ時間 12,831 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 38 ms
36,968 KB
testcase_13 AC 38 ms
36,972 KB
testcase_14 AC 38 ms
34,908 KB
testcase_15 AC 38 ms
36,968 KB
testcase_16 AC 38 ms
34,908 KB
testcase_17 AC 39 ms
36,968 KB
testcase_18 AC 39 ms
36,968 KB
testcase_19 AC 39 ms
36,972 KB
testcase_20 AC 32 ms
34,908 KB
testcase_21 AC 30 ms
36,972 KB
testcase_22 AC 26 ms
36,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"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)
	v := getNextInt(scanner)
	l := getNextInt(scanner)
	xx := make([]int, n)
	vv := make([]int, n)
	ww := make([]int, n)
	for i := 0; i < n; i++ {
		xx[i] = getNextInt(scanner)
		vv[i] = getNextInt(scanner)
		ww[i] = getNextInt(scanner)
	}
	xx = append(xx, l)
	for i := n; i > 0; i-- {
		xx[i] -= xx[i-1]
	}
	dp := makeGrid(n+2, v+1)
	for i := 0; i < n+2; i++ {
		for j := 0; j < v+1; j++ {
			dp[i][j] = cint(math.MaxInt64)
		}
	}
	dp[0][v] = 0
	for i := 0; i < n+1; i++ {
		for j := xx[i]; j < v+1; j++ {
			dp[i+1][j-xx[i]].minAs(dp[i][j])
		}
		for j := 1; i < n && j <= vv[i] && v-j >= 0; j++ {
			if dp[i+1][v-j].sub(math.MaxInt64) == 0 {
				continue
			}
			dp[i+1][v].minAs(dp[i+1][v-j].add(ww[i]))
		}

		for j := v; i < n && j >= vv[i]; j-- {
			if dp[i+1][j-vv[i]].sub(math.MaxInt64) == 0 {
				continue
			}
			dp[i+1][j].minAs(dp[i+1][j-vv[i]].add(ww[i]))
		}
	}
	var ans cint
	ans = dp[n+1][0]
	for i := 0; i < v; i++ {
		ans.minAs(dp[n+1][i+1])
	}
	if ans.sub(math.MaxInt64) == 0 {
		ans = -1
	}
	fmt.Fprintln(writer, ans)
}

func makeGrid(h, w int) [][]cint {
	index := make([][]cint, h, h)
	data := make([]cint, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

type cint int64

func (ct *cint) cintize(x interface{}) cint {
	if y, isCint := x.(cint); isCint {
		return y
	}
	if y, isInt64 := x.(int64); isInt64 {
		return cint(y)
	}
	return cint(x.(int))
}
func (ct *cint) minAs(x interface{}) *cint {
	*ct = ct.min(x)
	return ct
}
func (ct *cint) maxAs(x interface{}) *cint {
	*ct = ct.max(x)
	return ct
}
func (ct cint) min(x interface{}) cint {
	y := ct.cintize(x)
	if ct > y {
		return y
	}
	return ct
}
func (ct cint) max(x interface{}) cint {
	y := ct.cintize(x)
	if ct < y {
		return y
	}
	return ct
}
func (ct cint) add(x interface{}) cint {
	return ct + ct.cintize(x)
}
func (ct cint) sub(x interface{}) cint {
	return ct - ct.cintize(x)
}
func (ct cint) mul(x interface{}) cint {
	return ct * ct.cintize(x)
}
func (ct cint) div(x interface{}) cint {
	return ct / ct.cintize(x)
}
0