結果

問題 No.684 Prefix Parenthesis
ユーザー aru aruaru aru
提出日時 2020-11-25 22:31:09
言語 Go
(1.22.1)
結果
MLE  
実行時間 -
コード長 2,928 bytes
コンパイル時間 11,034 ms
コンパイル使用メモリ 211,876 KB
実行使用メモリ 634,968 KB
最終ジャッジ日時 2023-10-01 01:57:32
合計ジャッジ時間 15,877 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 MLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"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
}

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
}

// NextPermutation generates the next permutation of the
// sortable collection x in lexical order.  It returns false
// if the permutations are exhausted.
//
// Knuth, Donald (2011), "Section 7.2.1.2: Generating All Permutations",
// The Art of Computer Programming, volume 4A.
// ※NextPermutationは辞書順で次を返す
func NextPermutation(x sort.Interface) bool {
	n := x.Len() - 1
	if n < 1 {
		return false
	}
	j := n - 1
	for ; !x.Less(j, j+1); j-- {
		if j == 0 {
			return false
		}
	}
	l := n
	for !x.Less(j, l) {
		l--
	}
	x.Swap(j, l)
	for k, l := j+1, n; k < l; {
		x.Swap(k, l)
		k++
		l--
	}
	return true
}

type dat struct {
	l, r, n int
}

var N int
var d []dat

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	N = getI()
	s := getS()

	d = make([]dat, N)
	l, r := 0, 0
	n := 0
	tot := 0
	for i := 0; i < N; i++ {
		if s[i] == '(' {
			l++
		} else {
			if l > 0 {
				l--
				n++
			} else {
				r++
			}
		}
		d[i] = dat{l, r, n}
		tot += n
	}
	// sort.Slice(d, func(i, j int) bool {
	// 	if d[i].l == d[j].l {
	// 		return d[i].r < d[j].r
	// 	}
	// 	return d[i].l > d[j].l
	// })

	dp := make([]map[int]int, N+1)
	for i := 0; i <= N; i++ {
		dp[i] = make(map[int]int)
	}
	dp[0][0] = 0
	for i := 0; i < N; i++ {
		for e, n := range dp[i] {
			if e < 0 {
				dp[i+1][e+d[i].l] = max(dp[i+1][e+d[i].l], n+min(d[i].l, -e))
				dp[i+1][e-d[i].r] = max(dp[i+1][e-d[i].r], n)
			} else {
				dp[i+1][e+d[i].l] = max(dp[i+1][e+d[i].l], n)
				dp[i+1][e-d[i].r] = max(dp[i+1][e-d[i].r], n+min(e, d[i].r))
			}
		}
	}
	// for i := 0; i <= N; i++ {
	// 	out(dp[i])
	// }

	maxV := 0
	for _, e := range dp[N] {
		maxV = max(maxV, e)
	}
	out((maxV + tot) * 2)
}
0