結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-26 21:52:47
言語 Go
(1.21.3)
結果
AC  
実行時間 710 ms / 2,000 ms
コード長 3,225 bytes
コンパイル時間 12,009 ms
コンパイル使用メモリ 203,116 KB
実行使用メモリ 14,244 KB
最終ジャッジ日時 2023-09-18 04:15:02
合計ジャッジ時間 19,486 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 26 ms
5,496 KB
testcase_14 AC 25 ms
5,492 KB
testcase_15 AC 25 ms
5,492 KB
testcase_16 AC 25 ms
5,492 KB
testcase_17 AC 25 ms
5,492 KB
testcase_18 AC 25 ms
5,492 KB
testcase_19 AC 25 ms
5,496 KB
testcase_20 AC 25 ms
5,492 KB
testcase_21 AC 25 ms
5,492 KB
testcase_22 AC 25 ms
5,492 KB
testcase_23 AC 701 ms
14,240 KB
testcase_24 AC 710 ms
14,244 KB
testcase_25 AC 705 ms
14,240 KB
testcase_26 AC 706 ms
14,244 KB
testcase_27 AC 708 ms
14,240 KB
testcase_28 AC 273 ms
14,240 KB
testcase_29 AC 362 ms
14,244 KB
testcase_30 AC 460 ms
14,240 KB
testcase_31 AC 457 ms
14,244 KB
testcase_32 AC 463 ms
14,244 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
	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)
	seg := newSegment(n)
	seg2 := newSegment(n)
	for i := 0; i < n; i++ {
		seg.update(i, math.MaxInt32, seg.min)
		seg2.update(i, math.MaxInt32, seg.min)
	}
	pp := make([]pair, n)
	for i := 0; i < n; i++ {
		a := getNextInt(scanner)
		pp[i][0] = i
		pp[i][1] = a
	}
	var ans int
	ans = math.MaxInt32
	sort.SliceStable(pp, func(i, j int) bool {
		return pp[i][1] < pp[j][1]
	})
	for i := 0; i < n; i++ {
		if pp[i][0] > 0 && pp[i][0] < n {
			l := seg.query(0, pp[i][0], math.MaxInt32, seg.min)
			r := seg.query(pp[i][0]+1, n, math.MaxInt32, seg.min)
			if l < math.MaxInt32 && r < math.MaxInt32 && ans > l+r+pp[i][1] {
				ans = l + r + pp[i][1]
			}
		}
		seg.update(pp[i][0], pp[i][1], seg.min)
	}
	sort.SliceStable(pp, func(i, j int) bool {
		return pp[i][1] > pp[j][1]
	})
	for i := 0; i < n; i++ {
		if pp[i][0] > 0 && pp[i][0] < n {
			l := seg2.query(0, pp[i][0], math.MaxInt32, seg2.min)
			r := seg2.query(pp[i][0]+1, n, math.MaxInt32, seg2.min)
			if l < math.MaxInt32 && r < math.MaxInt32 && ans > l+r+pp[i][1] {
				ans = l + r + pp[i][1]
			}
		}
		seg2.update(pp[i][0], pp[i][1], seg2.min)
	}
	if ans == math.MaxInt32 {
		ans = -1
	}
	fmt.Fprintln(writer, ans)
}

type pair [2]int
type segment [][]int

func newSegment(n int) segment {
	seg := make(segment, 0)
	for n > 1 {
		seg = append(seg, make([]int, n))
		n = (n + 1) >> 1
	}
	seg = append(seg, make([]int, 1))
	return seg
}
func (seg segment) update(i int, v int, f func(x, y int) int) {
	seg[0][i] = v
	for h := 1; h < len(seg); h++ {
		i >>= 1
		if i<<1+1 < len(seg[h-1]) {
			seg[h][i] = f(seg[h-1][i<<1], seg[h-1][i<<1+1])
		} else {
			seg[h][i] = seg[h-1][i<<1]
		}
	}
}
func (seg segment) query(l, r int, s int, f func(x, y int) int) int {
	for h := 0; h < len(seg) && l < r; h++ {
		if l&1 == 1 {
			s = f(s, seg[h][l])
			l++
		}
		l >>= 1
		if r&1 == 1 {
			s = f(s, seg[h][r-1])
		}
		r >>= 1
	}
	return s
}
func (seg segment) min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
0