結果

問題 No.165 四角で囲え!
ユーザー aru aruaru aru
提出日時 2020-08-29 13:17:09
言語 Go
(1.22.1)
結果
TLE  
実行時間 -
コード長 2,975 bytes
コンパイル時間 15,431 ms
コンパイル使用メモリ 231,248 KB
実行使用メモリ 12,304 KB
最終ジャッジ日時 2024-04-26 16:23:52
合計ジャッジ時間 28,157 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

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
	sum := 0
	cnt := 0
	for sy := 1; sy <= ny; sy++ {
		for ey := sy; ey <= ny; ey++ {
			for sx := 1; sx <= nx; sx++ {
				for ex := sx; ex <= nx; ex++ {
					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(sx, sy, ex, ey, "cost", cost, "num", d)
					if B >= cost && cost >= sum {
						cnt = max(cnt, d)
						sum = cost
					}
				}
			}
		}
	}

	out(cnt)
}
0