結果

問題 No.798 コレクション
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-05 10:30:17
言語 Go
(1.22.1)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 2,304 bytes
コンパイル時間 17,202 ms
コンパイル使用メモリ 210,300 KB
実行使用メモリ 36,968 KB
最終ジャッジ日時 2023-08-21 15:50:57
合計ジャッジ時間 18,849 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 11 ms
17,996 KB
testcase_14 AC 18 ms
26,340 KB
testcase_15 AC 15 ms
24,248 KB
testcase_16 AC 8 ms
13,820 KB
testcase_17 AC 13 ms
18,004 KB
testcase_18 AC 25 ms
34,880 KB
testcase_19 AC 18 ms
28,636 KB
testcase_20 AC 8 ms
13,824 KB
testcase_21 AC 7 ms
11,756 KB
testcase_22 AC 15 ms
24,244 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 26 ms
36,968 KB
testcase_25 AC 26 ms
36,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"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
	cnt := 1
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		cnt = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	for i := 0; i < cnt; i++ {
		if i > 0 {
			fmt.Fprintln(writer, "-----------------------------------")
		}
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	pp := make([]pair, n)
	for i := 0; i < n; i++ {
		pp[i][0] = cint(getNextInt(scanner))
		pp[i][1] = cint(getNextInt(scanner))
	}
	sort.SliceStable(pp, func(i, j int) bool {
		return pp[i][1] > pp[j][1]
	})
	dp := makeGrid(n+1, n+1)
	for j := 0; j < n+1; j++ {
		for i := 0; i < n+1; i++ {
			dp[j][i] = math.MaxInt64
		}
	}
	dp[0][0] = 0
	for i := 0; i < n; i++ {
		for j := cint(i + 1); j > 0; j-- {
			if dp[j-1][j-1] == math.MaxInt32 {
				continue
			}
			dp[j][j].minAs(dp[j-1][j-1] + pp[i][0] + pp[i][1]*(j-1))
		}
	}
	fmt.Fprintln(writer, dp[n-n/3][n-n/3])
}

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 pair [2]cint

type cint int64

func (x *cint) minAs(y cint) *cint {
	*x = x.min(y)
	return x
}
func (x *cint) maxAs(y cint) *cint {
	*x = x.max(y)
	return x
}
func (x *cint) min(y cint) cint {
	if *x > y {
		return y
	}
	return *x
}
func (x *cint) max(y cint) cint {
	if *x < y {
		return y
	}
	return *x
}
0