結果

問題 No.165 四角で囲え!
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-13 16:54:01
言語 Go
(1.21.3)
結果
AC  
実行時間 218 ms / 5,000 ms
コード長 2,954 bytes
コンパイル時間 15,925 ms
コンパイル使用メモリ 213,400 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2023-09-07 21:10:10
合計ジャッジ時間 18,881 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
5,540 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 129 ms
5,540 KB
testcase_06 AC 31 ms
5,508 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 53 ms
5,524 KB
testcase_12 AC 23 ms
5,508 KB
testcase_13 AC 24 ms
5,512 KB
testcase_14 AC 18 ms
5,508 KB
testcase_15 AC 17 ms
5,512 KB
testcase_16 AC 182 ms
7,848 KB
testcase_17 AC 134 ms
7,848 KB
testcase_18 AC 218 ms
7,844 KB
testcase_19 AC 157 ms
7,848 KB
testcase_20 AC 137 ms
7,848 KB
testcase_21 AC 136 ms
7,844 KB
testcase_22 AC 136 ms
7,844 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)
	b := getNextInt(scanner)
	xx := make([]int, n)
	yy := make([]int, n)
	pp := make([]int, n)
	for i := 0; i < n; i++ {
		xx[i] = getNextInt(scanner)
		yy[i] = getNextInt(scanner)
		pp[i] = getNextInt(scanner)
	}
	xm := rankmap(xx)
	ym := rankmap(yy)
	xl := len(xm)
	yl := len(ym)
	g := makeGrid(yl+1, xl+1)
	c := makeGrid(yl+1, xl+1)
	for i := 0; i < n; i++ {
		g[ym[yy[i]]][xm[xx[i]]] = pp[i]
		c[ym[yy[i]]][xm[xx[i]]] = 1
	}
	for y := 0; y < yl+1; y++ {
		for x := 1; x < xl+1; x++ {
			g[y][x] += g[y][x-1]
			c[y][x] += c[y][x-1]
		}
	}
	for x := 0; x < xl+1; x++ {
		for y := 1; y < yl+1; y++ {
			g[y][x] += g[y-1][x]
			c[y][x] += c[y-1][x]
		}
	}
	var ans cint
	for ty := 0; ty < yl+1; ty++ {
		for sy := 0; sy < ty; sy++ {
			l := -1
			for r := 1; r < xl+1; r++ {
				for r > l && g[ty][r]-g[ty][l+1]-g[sy][r]+g[sy][l+1] > b {
					l++
				}
				ans.maxAs(cint(c[ty][r] - c[ty][l+1] - c[sy][r] + c[sy][l+1]))
			}
		}
	}
	fmt.Fprintln(writer, ans)

}
func makeGrid(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}
func rankmap(xx []int) map[int]int {
	m := map[int]int{}
	for _, x := range xx {
		m[x] = 1
	}
	yy := make([]int, 0)
	for x := range m {
		yy = append(yy, x)
	}
	sort.Ints(yy)
	ret := map[int]int{}
	for i := 0; i < len(yy); i++ {
		ret[yy[i]] = i + 1
	}
	return ret
}

type cint int64

func (ct *cint) minAs(x cint) *cint {
	*ct = ct.min(x)
	return ct
}
func (ct *cint) maxAs(x cint) *cint {
	*ct = ct.max(x)
	return ct
}
func (ct cint) min(x cint) cint {
	if ct > x {
		return x
	}
	return ct
}
func (ct cint) max(x cint) cint {
	if ct < x {
		return x
	}
	return ct
}
0