結果

問題 No.168 ものさし
ユーザー aru aruaru aru
提出日時 2020-08-30 20:59:02
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 2,676 bytes
コンパイル時間 16,124 ms
コンパイル使用メモリ 213,452 KB
実行使用メモリ 36,816 KB
最終ジャッジ日時 2023-08-09 18:31:10
合計ジャッジ時間 20,530 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
10,104 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 18 ms
5,488 KB
testcase_11 AC 262 ms
10,108 KB
testcase_12 AC 871 ms
20,352 KB
testcase_13 AC 1,243 ms
36,812 KB
testcase_14 AC 1,172 ms
36,816 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 12 ms
5,504 KB
testcase_19 AC 270 ms
36,812 KB
testcase_20 AC 290 ms
36,816 KB
testcase_21 AC 282 ms
36,812 KB
testcase_22 AC 280 ms
36,812 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
}

type dist struct {
	l, x, y int
}

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

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

type unionfind []int

func newUionfind(n int) unionfind {
	ret := make(unionfind, n)
	for i := 0; i < n; i++ {
		ret[i] = -1
	}
	return ret
}

func (u unionfind) r(x int) int {
	if u[x] < 0 {
		return x
	}
	root := u.r(u[x])
	return root
}

func (u unionfind) unite(i, j int) {
	x := u.r(i)
	y := u.r(j)
	if x == y {
		return
	}
	u[x] += u[y]
	u[y] = x
}

func (u unionfind) same(i, j int) bool {
	x := u.r(i)
	y := u.r(j)
	if x == y {
		return true
	}
	return false
}

func (u unionfind) size(i int) int {
	x := u.r(i)
	return -u[x]
}

func main() {
	sc.Split(bufio.ScanWords)
	N := getInt()
	x := make([]int, N)
	y := make([]int, N)
	for i := 0; i < N; i++ {
		x[i], y[i] = getInt(), getInt()
	}

	d := make([]dist, 0)
	for i := 0; i < N; i++ {
		for j := i + 1; j < N; j++ {
			dx := abs(x[i] - x[j])
			dy := abs(y[i] - y[j])
			d = append(d, dist{dx*dx + dy*dy, i, j})
		}
	}
	sort.Slice(d, func(i, j int) bool {
		return d[i].l < d[j].l
	})
	// out(d)

	// xx := min(len(d)-1, upperBoundDist(d, 0))
	// out(d[xx])

	l := 0
	r := int(1e9 + 10)

	for l+1 != r {
		m := (l + r) / 2
		uf := newUionfind(N)
		// p := upperBoundDist(d, m)
		// for i := 0; i < p; i++ {
		// 	uf.unite(d[i].x, d[i].y)
		// }
		for _, e := range d {
			if e.l <= m*m {
				// out(e, m)
				uf.unite(e.x, e.y)
			}
		}
		if uf.same(0, N-1) {
			r = m
		} else {
			l = m
		}
	}

	// out(r)
	out(((r + 9) / 10) * 10)
}
0