結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー | ccppjsrb |
提出日時 | 2020-06-26 21:43:14 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,221 bytes |
コンパイル時間 | 13,649 ms |
コンパイル使用メモリ | 221,804 KB |
実行使用メモリ | 12,416 KB |
最終ジャッジ日時 | 2024-07-04 20:25:08 |
合計ジャッジ時間 | 20,247 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | WA | - |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | WA | - |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | WA | - |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | WA | - |
testcase_13 | AC | 21 ms
5,376 KB |
testcase_14 | AC | 22 ms
5,376 KB |
testcase_15 | WA | - |
testcase_16 | AC | 21 ms
5,376 KB |
testcase_17 | WA | - |
testcase_18 | AC | 23 ms
5,376 KB |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 23 ms
5,376 KB |
testcase_23 | AC | 637 ms
12,416 KB |
testcase_24 | AC | 614 ms
12,416 KB |
testcase_25 | AC | 634 ms
12,416 KB |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | AC | 254 ms
12,416 KB |
testcase_29 | AC | 330 ms
12,416 KB |
testcase_30 | AC | 412 ms
12,416 KB |
testcase_31 | AC | 412 ms
12,416 KB |
testcase_32 | AC | 415 ms
12,416 KB |
ソースコード
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, seg.min) r := seg2.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) } 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 }