結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー 小野寺健小野寺健
提出日時 2021-12-20 08:54:31
言語 Go
(1.22.1)
結果
AC  
実行時間 329 ms / 2,000 ms
コード長 1,922 bytes
コンパイル時間 14,889 ms
コンパイル使用メモリ 209,004 KB
実行使用メモリ 18,504 KB
最終ジャッジ日時 2023-10-13 19:26:34
合計ジャッジ時間 20,795 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 13 ms
4,352 KB
testcase_14 AC 13 ms
4,352 KB
testcase_15 AC 14 ms
4,352 KB
testcase_16 AC 14 ms
4,352 KB
testcase_17 AC 13 ms
4,348 KB
testcase_18 AC 13 ms
4,348 KB
testcase_19 AC 13 ms
4,352 KB
testcase_20 AC 13 ms
4,376 KB
testcase_21 AC 13 ms
4,352 KB
testcase_22 AC 13 ms
4,348 KB
testcase_23 AC 325 ms
18,496 KB
testcase_24 AC 329 ms
18,492 KB
testcase_25 AC 324 ms
18,504 KB
testcase_26 AC 320 ms
18,492 KB
testcase_27 AC 322 ms
18,496 KB
testcase_28 AC 209 ms
18,492 KB
testcase_29 AC 211 ms
18,496 KB
testcase_30 AC 261 ms
18,496 KB
testcase_31 AC 259 ms
18,492 KB
testcase_32 AC 259 ms
18,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

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

type SegmentTree struct {
	n int
	tree []int
}

func newSegmentTree(n int) *SegmentTree{
	sp := new(SegmentTree)
	sp.n = 1
	for sp.n < n {
		sp.n *= 2
	}
	sp.tree = make([]int, 2*sp.n)
	for i := 0; i < 2*sp.n; i++ {
		sp.tree[i] = math.MaxInt64
	}
	return sp
}

func (this *SegmentTree) set(idx, x int) {
	idx += this.n
	this.tree[idx] = x
	for idx > 0 {
		idx >>= 1
		this.tree[idx] = Min(this.tree[2 * idx], this.tree[2 * idx + 1])
	}
}

func (this *SegmentTree) query(lt, rt int) int {
	lt += this.n
	rt += this.n
	vl, vr := math.MaxInt64, math.MaxInt64
	for rt - lt > 0 {
		if lt & 1 != 0 {
			vl = Min(vl, this.tree[lt])
			lt++
		}
		if rt & 1 != 0 {
			rt--
			vr = Min(this.tree[rt], vr)
		}
		lt >>= 1
		rt >>= 1
	}
	return Min(vl, vr)
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var N int
	fmt.Fscan(r, &N)
	A := make([][2]int, N)
	for i := 0; i < N; i++ {
		var x int
		fmt.Fscan(r, &x)
		A[i] = [2]int{x, i}
	}
	sort.Slice(A, 
		func(i, j int) bool {
			if A[i][0] < A[j][0] {
				return true
			} else if A[i][0] > A[j][0] {
				return false
			} else {
				return A[i][1] < A[j][1]
			}
		})
	res := math.MaxInt64
	st1 := newSegmentTree(N)
	for i := 0; i < N; i++ {
		a, idx := A[i][0], A[i][1]
		st1.set(idx, a)
		lt := st1.query(0, idx)
		rt := st1.query(idx + 1, N)
		if lt == math.MaxInt64 || rt == math.MaxInt64 {
			continue
		}
		res = Min(res, lt + rt + a)
	}
	st2 := newSegmentTree(N)
	for i := N-1; i >= 0; i-- {
		a, idx := A[i][0], A[i][1]
		st2.set(idx, a)
		lt := st2.query(0, idx)
		rt := st2.query(idx + 1, N)
		if lt == math.MaxInt64 || rt == math.MaxInt64 {
			continue
		}
		res = Min(res, lt + rt + a)		
	}
	if res == math.MaxInt64 {
		fmt.Fprintln(w, -1)
	} else {
		fmt.Fprintln(w, res)
	}
}
0