結果

問題 No.817 Coin donation
ユーザー ccppjsrbccppjsrb
提出日時 2020-08-20 11:20:47
言語 Go
(1.22.1)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 2,318 bytes
コンパイル時間 13,523 ms
コンパイル使用メモリ 239,856 KB
実行使用メモリ 24,344 KB
最終ジャッジ日時 2024-04-21 06:22:55
合計ジャッジ時間 13,097 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 19 ms
6,940 KB
testcase_07 AC 28 ms
7,748 KB
testcase_08 AC 113 ms
17,108 KB
testcase_09 AC 31 ms
7,364 KB
testcase_10 AC 168 ms
24,344 KB
testcase_11 AC 168 ms
24,340 KB
testcase_12 AC 174 ms
24,336 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,944 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
	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)
	k := getNextInt(scanner)
	aa := make([]int, n)
	bb := make([]int, n)
	cc := make([]int, 0)
	for i := 0; i < n; i++ {
		a := getNextInt(scanner)
		b := getNextInt(scanner) + 1
		aa[i] = a
		bb[i] = b
		cc = append(cc, a)
		cc = append(cc, b)
	}
	cm := newCompress(cc)
	l := len(cm)
	rr := make([]int, l)
	for key, val := range cm {
		rr[val] = key
	}
	imos := make([]int, l)
	for i := 0; i < n; i++ {
		imos[cm.get(aa[i])]++
		imos[cm.get(bb[i])]--
	}
	for i := 1; i < l; i++ {
		imos[i] += imos[i-1]
	}
	for i := 0; i < l-1; i++ {
		cnt := imos[i] * (rr[i+1] - rr[i])
		if k <= cnt {
			fmt.Fprintln(writer, rr[i]-1+((k+imos[i]-1)/imos[i]))
			return
		}
		k -= cnt
	}
}

type compress map[int]int

func newCompress(aa []int) compress {
	n := len(aa)
	bb := make([]int, n)
	copy(bb, aa)
	sort.SliceStable(bb, func(i, j int) bool {
		return bb[i] < bb[j]
	})
	c := compress{}
	l := 0
	i := 0
	for r := 0; r < n; r++ {
		for r > l && bb[r] != bb[l] {
			l++
		}
		if r == l {
			c[bb[l]] = i
			i++
		}
	}
	return c
}
func (c compress) get(x int) int {
	return c[x]
}
0