結果

問題 No.967 引き算をして門松列(その2)
ユーザー ccppjsrbccppjsrb
提出日時 2020-09-07 19:23:43
言語 Go
(1.22.1)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,349 bytes
コンパイル時間 11,585 ms
コンパイル使用メモリ 210,076 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-19 17:06:49
合計ジャッジ時間 12,411 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 7 ms
4,376 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) {
	testcase := func() {
		a := getNextInt64(scanner)
		b := getNextInt64(scanner)
		c := getNextInt64(scanner)
		x := getNextInt64(scanner)
		y := getNextInt64(scanner)
		z := getNextInt64(scanner)
		fmt.Fprintln(writer, calc(a, b, c, x, y, z))
	}
	t := getNextInt(scanner)
	for t > 0 {
		testcase()
		t--
	}
}
func calc(a, b, c, x, y, z int64) int64 {
	if a <= 0 || b <= 0 || c <= 0 {
		return -1
	}
	if a > c {
		return calc(c, b, a, z, y, x)
	}
	if a < b && b > c && a != c {
		return 0
	}
	if a > b && b < c && a != c {
		return 0
	}
	if a == c {
		if x < z {
			if res := calc(a-1, b, c, x, y, z); res != -1 {
				return x + res
			}
			return -1
		}
		if res := calc(a-1, b, c, z, y, x); res != -1 {
			return z + res
		}
		return -1
	}
	var ans int64
	ans = math.MaxInt64
	if res := calc(a, a-1, c, x, y, z); res != -1 {
		ans = min(ans, y*(b-(a-1))+res)
	}
	if res := calc(a, b, b-1, x, y, z); res != -1 {
		ans = min(ans, z*(c-(b-1))+res)
	}
	if ans == math.MaxInt64 {
		return -1
	}
	return ans
}
func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func max(a, b int64) int64 {
	return -min(-a, -b)
}
0