結果

問題 No.1077 Noelちゃんと星々4
ユーザー c-yanc-yan
提出日時 2020-06-15 00:28:51
言語 Go
(1.22.1)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,439 bytes
コンパイル時間 12,558 ms
コンパイル使用メモリ 200,784 KB
実行使用メモリ 85,368 KB
最終ジャッジ日時 2023-09-10 12:49:58
合計ジャッジ時間 14,101 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
5,536 KB
testcase_02 AC 32 ms
60,312 KB
testcase_03 AC 41 ms
77,012 KB
testcase_04 AC 14 ms
26,788 KB
testcase_05 AC 11 ms
20,544 KB
testcase_06 AC 17 ms
33,100 KB
testcase_07 AC 19 ms
37,296 KB
testcase_08 AC 15 ms
28,856 KB
testcase_09 AC 13 ms
24,720 KB
testcase_10 AC 40 ms
77,012 KB
testcase_11 AC 27 ms
51,888 KB
testcase_12 AC 23 ms
43,532 KB
testcase_13 AC 11 ms
20,532 KB
testcase_14 AC 39 ms
74,928 KB
testcase_15 AC 21 ms
41,460 KB
testcase_16 AC 16 ms
28,868 KB
testcase_17 AC 25 ms
47,712 KB
testcase_18 AC 41 ms
79,088 KB
testcase_19 AC 10 ms
18,468 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 45 ms
85,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

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

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

func main() {
	defer flush()

	N := readInt()
	Y := make([]int, N)
	for i := 0; i < N; i++ {
		Y[i] = readInt()
	}

	dp := make([][]int, N)
	for i := 0; i < N; i++ {
		dp[i] = make([]int, 10000+1)
	}

	for j := 0; j < 10000+1; j++ {
		dp[0][j] = abs(Y[0] - j)
	}

	for i := 1; i < N; i++ {
		t := math.MaxInt64
		for j := 0; j < 10000+1; j++ {
			t = min(t, dp[i-1][j])
			dp[i][j] = t + abs(Y[i]-j)
		}
	}

	result := math.MaxInt64
	for j := 0; j < 10000+1; j++ {
		result = min(result, dp[N-1][j])
	}
	println(result)
}

const (
	ioBufferSize = 1 * 1024 * 1024 // 1 MB
)

var stdinScanner = func() *bufio.Scanner {
	result := bufio.NewScanner(os.Stdin)
	result.Buffer(make([]byte, ioBufferSize), ioBufferSize)
	result.Split(bufio.ScanWords)
	return result
}()

func readString() string {
	stdinScanner.Scan()
	return stdinScanner.Text()
}

func readInt() int {
	result, err := strconv.Atoi(readString())
	if err != nil {
		panic(err)
	}
	return result
}

var stdoutWriter = bufio.NewWriter(os.Stdout)

func flush() {
	stdoutWriter.Flush()
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdoutWriter, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdoutWriter, args...)
}
0