結果

問題 No.165 四角で囲え!
ユーザー aru aruaru aru
提出日時 2020-08-29 13:43:16
言語 Go
(1.22.1)
結果
AC  
実行時間 370 ms / 5,000 ms
コード長 3,321 bytes
コンパイル時間 14,611 ms
コンパイル使用メモリ 230,368 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-26 16:46:28
合計ジャッジ時間 14,537 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 171 ms
5,376 KB
testcase_06 AC 47 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 73 ms
5,376 KB
testcase_12 AC 28 ms
5,376 KB
testcase_13 AC 35 ms
5,376 KB
testcase_14 AC 22 ms
5,376 KB
testcase_15 AC 20 ms
5,376 KB
testcase_16 AC 230 ms
5,376 KB
testcase_17 AC 175 ms
5,376 KB
testcase_18 AC 370 ms
5,376 KB
testcase_19 AC 203 ms
5,376 KB
testcase_20 AC 179 ms
5,376 KB
testcase_21 AC 177 ms
5,376 KB
testcase_22 AC 178 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getInt()
	}
	return ret
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	sc.Split(bufio.ScanWords)
	N, B := getInt(), getInt()
	x := make([]int, N)
	y := make([]int, N)
	p := make([]int, N)
	mx := make(map[int]int)
	my := make(map[int]int)
	for i := 0; i < N; i++ {
		x[i], y[i], p[i] = getInt(), getInt(), getInt()
		mx[x[i]]++
		my[y[i]]++
	}
	// get xy
	px := make([]int, 0, len(mx))
	py := make([]int, 0, len(my))
	for xx := range mx {
		px = append(px, xx)
	}
	for yy := range my {
		py = append(py, yy)
	}
	sort.Ints(px)
	sort.Ints(py)
	mx = make(map[int]int)
	my = make(map[int]int)
	for i, xx := range px {
		mx[xx] = i + 1
	}
	for i, yy := range py {
		my[yy] = i + 1
	}

	// make cost table
	ny := len(my)
	nx := len(mx)
	c := make([][]int, ny+1)
	num := make([][]int, ny+1)
	for yy := 0; yy <= ny; yy++ {
		c[yy] = make([]int, nx+1)
		num[yy] = make([]int, nx+1)
	}
	for i := 0; i < N; i++ {
		xpos := mx[x[i]]
		ypos := my[y[i]]
		c[ypos][xpos] = p[i]
		num[ypos][xpos] = 1
	}
	// for yy := 0; yy <= ny; yy++ {
	// 	out(c[yy])
	// }
	// out("-------------")
	// for yy := 0; yy <= ny; yy++ {
	// 	out(num[yy])
	// }
	// out("-------------")

	for yy := 0; yy <= ny; yy++ {
		for xx := 1; xx <= nx; xx++ {
			c[yy][xx] += c[yy][xx-1]
			num[yy][xx] += num[yy][xx-1]
		}
	}
	for xx := 0; xx <= nx; xx++ {
		for yy := 1; yy <= ny; yy++ {
			c[yy][xx] += c[yy-1][xx]
			num[yy][xx] += num[yy-1][xx]
		}
	}
	// for yy := 0; yy <= ny; yy++ {
	// 	out(c[yy])
	// }
	// out("-------------")
	// for yy := 0; yy <= ny; yy++ {
	// 	out(num[yy])
	// }
	// 積分画像作成完了C
	cnt := 0
	for sy := 1; sy <= ny; sy++ {
		for ey := sy; ey <= ny; ey++ {
			sx, ex := 1, 0
			// out("Y", sy, ey, nx, ny)
			for sx <= nx && ex <= nx {
				cost := 0
				d := 0
				for cost <= B {
					ex++
					if ex > nx {
						break
					}
					cost = c[ey][ex] - c[sy-1][ex] - c[ey][sx-1] + c[sy-1][sx-1]
					d = num[ey][ex] - num[sy-1][ex] - num[ey][sx-1] + num[sy-1][sx-1]
					// out("EX", sx, ex, nx, cost, d)
					if cost <= B {
						cnt = max(cnt, d)
					}
				}
				for cost > B {
					sx++
					if ex > nx {
						break
					}
					// out("SX", sx, ex, nx)
					cost = c[ey][ex] - c[sy-1][ex] - c[ey][sx-1] + c[sy-1][sx-1]
					d = num[ey][ex] - num[sy-1][ex] - num[ey][sx-1] + num[sy-1][sx-1]
					if cost <= B {
						cnt = max(cnt, d)
					}
				}
				// out(sx, ex)
			}
		}
	}

	out(cnt)
}
0