結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー aru aruaru aru
提出日時 2021-02-01 20:59:33
言語 Go
(1.22.1)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 2,210 bytes
コンパイル時間 11,769 ms
コンパイル使用メモリ 213,776 KB
実行使用メモリ 7,944 KB
最終ジャッジ日時 2023-09-12 10:47:21
合計ジャッジ時間 12,815 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 16 ms
7,884 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 7 ms
4,380 KB
testcase_05 AC 23 ms
7,944 KB
testcase_06 AC 24 ms
7,944 KB
testcase_07 AC 22 ms
7,940 KB
testcase_08 AC 23 ms
7,940 KB
testcase_09 AC 21 ms
7,944 KB
testcase_10 AC 21 ms
7,944 KB
testcase_11 AC 21 ms
7,940 KB
testcase_12 AC 21 ms
7,944 KB
testcase_13 AC 20 ms
7,760 KB
testcase_14 AC 21 ms
7,792 KB
testcase_15 AC 22 ms
7,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

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
}

// min for n entry
func nmin(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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
}

func solve() {
	N := getI()
	a := getInts(N)
	a = append(a, a[0:2]...)
	c := make([]int, N+2)
	for i := 2; i < N+2; i++ {
		x, y, z := a[i-2], a[i-1], a[i]
		if x != y && x != z && y != z && (nmin(x, y, z) == y || nmax(x, y, z) == y) {
			c[i] = x
		}
	}

	// out(a, c)

	ans := 0
	for k := 0; k < 3; k++ {
		dp := make([]int, N)
		for i := 2; i < N; i++ {
			// out(i, k)
			if i-3 >= 0 {
				dp[i] = nmax(dp[i-1], dp[i-2], dp[i-3]+c[i+k])
			} else {
				dp[i] = nmax(dp[i-1], dp[i-2], c[i+k])
			}
		}
		// out(dp)
		ans = max(ans, dp[N-1])
	}
	out(ans)
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, math.MaxInt32)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	T := getI()
	for i := 0; i < T; i++ {
		// out("--------------------------------")
		solve()
	}
}
0