結果

問題 No.777 再帰的ケーキ
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-05 19:34:24
言語 Go
(1.22.1)
結果
AC  
実行時間 712 ms / 2,000 ms
コード長 3,683 bytes
コンパイル時間 14,367 ms
コンパイル使用メモリ 230,964 KB
実行使用メモリ 37,128 KB
最終ジャッジ日時 2024-05-09 16:34:04
合計ジャッジ時間 16,394 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 4 ms
6,944 KB
testcase_22 AC 4 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 5 ms
6,940 KB
testcase_26 AC 5 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 688 ms
37,100 KB
testcase_29 AC 676 ms
32,132 KB
testcase_30 AC 712 ms
32,176 KB
testcase_31 AC 708 ms
37,080 KB
testcase_32 AC 414 ms
37,120 KB
testcase_33 AC 39 ms
7,620 KB
testcase_34 AC 50 ms
9,672 KB
testcase_35 AC 543 ms
21,000 KB
testcase_36 AC 341 ms
37,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"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
	cnt := 1
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		cnt = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	for i := 0; i < cnt; i++ {
		if i > 0 {
			fmt.Fprintln(writer, "-----------------------------------")
		}
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	bb := make([]int64, n)
	cakes := make([]cake, n)
	for i := 0; i < n; i++ {
		cakes[i].a = getNextInt64(scanner)
		cakes[i].b = getNextInt64(scanner)
		cakes[i].c = getNextInt64(scanner)
		bb[i] = cakes[i].b
	}
	rm := rank(bb)
	sort.SliceStable(cakes, func(i, j int) bool {
		if cakes[i].a == cakes[j].a {
			return cakes[i].b < cakes[j].b
		}
		return cakes[i].a > cakes[j].a
	})
	dec := newDecomposition(len(rm))
	for i := 0; i < n; i++ {
		r := rm[cakes[i].b]
		mx := cakes[i].c
		if r+1 < len(rm) {
			mx += dec.max(r+1, len(rm))
		}
		if mx > dec.bucket[0][r] {
			dec.maximize(r, mx)
		}
	}
	fmt.Fprintln(writer, dec.max(0, len(rm)))
}

func rank(aa []int64) map[int64]int {
	m := map[int64]int{}
	for _, a := range aa {
		m[a] = 1
	}
	rr := make([]int64, 0)
	for a := range m {
		rr = append(rr, a)
	}
	sort.SliceStable(rr, func(i, j int) bool {
		return rr[i] < rr[j]
	})
	res := map[int64]int{}
	for i := 0; i < len(rr); i++ {
		res[rr[i]] = i
	}
	return res
}

type cake struct {
	a, b, c int64
}

type decomposition struct {
	chunk  int
	bucket [][]int64
}

func newDecomposition(n int) decomposition {
	bucket := make([][]int64, 1)
	bucket[0] = make([]int64, n)
	chunk := 8
	for i := 0; n > 1; i++ {
		n = (n-1)/chunk + 1
		bucket = append(bucket, make([]int64, n))
	}
	return decomposition{
		chunk:  chunk,
		bucket: bucket,
	}
}
func (dec *decomposition) upleft(l int) int {
	return (l + dec.chunk - 1) / dec.chunk
}
func (dec *decomposition) upright(r int) int {
	return r / dec.chunk
}
func (dec *decomposition) maximize(index int, value int64) {
	dec.bucket[0][index] = value
	h := len(dec.bucket)
	for i := 0; i < h-1; i++ {
		p := index / dec.chunk
		r := (p + 1) * dec.chunk
		if r >= len(dec.bucket[i]) {
			r = len(dec.bucket[i])
		}
		dec.bucket[i+1][p] = value
		for j := p * dec.chunk; j < r; j++ {
			if dec.bucket[i+1][p] < dec.bucket[i][j] {
				dec.bucket[i+1][p] = dec.bucket[i][j]
			}
		}
		index /= dec.chunk
	}
}
func (dec *decomposition) max(l, r int) int64 {
	return dec.maxh(l, r, 0)
}
func (dec *decomposition) maxh(l, r, h int) int64 {
	m := dec.bucket[h][l]
	ll := dec.upleft(l)
	rr := dec.upright(r)
	for i := l; i < r && i < ll*dec.chunk; i++ {
		if m < dec.bucket[h][i] {
			m = dec.bucket[h][i]
		}
	}
	if ll < rr {
		mm := dec.maxh(ll, rr, h+1)
		if m < mm {
			m = mm
		}
	}
	for i := rr * dec.chunk; i < r && ll <= rr; i++ {
		if m < dec.bucket[h][i] {
			m = dec.bucket[h][i]
		}
	}
	return m
}
0