結果

問題 No.777 再帰的ケーキ
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-05 19:30:05
言語 Go
(1.22.1)
結果
AC  
実行時間 869 ms / 2,000 ms
コード長 4,512 bytes
コンパイル時間 11,109 ms
コンパイル使用メモリ 213,968 KB
実行使用メモリ 32,688 KB
最終ジャッジ日時 2023-08-22 10:48:28
合計ジャッジ時間 18,796 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 6 ms
4,376 KB
testcase_22 AC 5 ms
4,500 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 6 ms
4,376 KB
testcase_26 AC 6 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 855 ms
32,672 KB
testcase_29 AC 854 ms
32,056 KB
testcase_30 AC 866 ms
32,332 KB
testcase_31 AC 869 ms
32,068 KB
testcase_32 AC 480 ms
32,460 KB
testcase_33 AC 39 ms
7,816 KB
testcase_34 AC 53 ms
9,900 KB
testcase_35 AC 688 ms
20,592 KB
testcase_36 AC 376 ms
32,688 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
}

type cint int64

func (ct *cint) cintize(x interface{}) cint {
	if y, isCint := x.(cint); isCint {
		return y
	}
	if y, isInt64 := x.(int64); isInt64 {
		return cint(y)
	}
	return cint(x.(int))
}
func (ct *cint) minAs(x interface{}) *cint {
	*ct = ct.min(x)
	return ct
}
func (ct *cint) maxAs(x interface{}) *cint {
	*ct = ct.max(x)
	return ct
}
func (ct cint) min(x interface{}) cint {
	y := ct.cintize(x)
	if ct > y {
		return y
	}
	return ct
}
func (ct cint) max(x interface{}) cint {
	y := ct.cintize(x)
	if ct < y {
		return y
	}
	return ct
}
func (ct cint) add(x interface{}) cint {
	return ct + ct.cintize(x)
}
func (ct cint) sub(x interface{}) cint {
	return ct - ct.cintize(x)
}
func (ct cint) mul(x interface{}) cint {
	return ct * ct.cintize(x)
}
func (ct cint) div(x interface{}) cint {
	return ct / ct.cintize(x)
}
0