Submission #36189038


Source Code Expand

n = int(input())
l = [map(int, input().split()) for _ in range(n)]
xl, yl = [list(i) for i in zip(*l)]

# 発電所が(a,b)にあったとすると、求める不便さFは
# F = abs(x[0]-a) + abs(y[0]-b) + ... + abs(x[n-1]-a) + abs(y[n-1]-b)
#   = (abs(x[0]-a) + ... + abs(x[n-1]-a)) + (abs(y[0]-b) + ... + abs(y[n-1]-b))
# つまり不便さFはxとyで独立に考える事ができる
# xによる不便さをFx, yによる不便さをFyとすることにする。

# xについて数直線を考えて、発電所がどこにあると良さげか考える。
# -------------------------->
# x1    x2    a     x3    x4
# aは端っこだと効率が悪そう…
# aが真ん中付近にあると効率が良さそう…
# 考察を進めると、aよりも左にあるx点はすべてaが右に動くと+1増える。
# aよりも右にあるx点はすべてaが右に動くと-1減る。
# つまり、aからみたときに、偶数点なら左と右にx点の数が同じだとよい。ついでに、ちょうど真ん中だとよい。
# 奇数点なら真ん中点そのままだと良さそう。
# つまり、aはxの中央値だと良さそう。
# xyは独立なので、bはyの中央値だと良い。
# よって、Fxはxの中央値からの各点の距離の総和、Fyはyの中央値からの各点の距離の総和になれば良い。

xl.sort()
yl.sort()

if n%2==0:
  x_median = (xl[n//2-1]+xl[n//2])/2
  y_median = (yl[n//2-1]+yl[n//2])/2
else:
  x_median = xl[n//2]
  y_median = yl[n//2]

f = 0
for i in range(n):
    f += abs(x_median-xl[i])+abs(y_median-yl[i])
  
print(int(f))

Submission Info

Submission Time
Task 070 - Plant Planning(★4)
User inaty
Language Python (3.8.2)
Score 4
Code Size 1645 Byte
Status AC
Exec Time 402 ms
Memory 62748 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 4 / 4
Status
AC × 4
AC × 23
Set Name Test Cases
Sample 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 00_sample_04.txt
All 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 00_sample_04.txt, 10_small_random_01.txt, 10_small_random_02.txt, 10_small_random_03.txt, 10_small_random_04.txt, 10_small_random_05.txt, 10_small_random_06.txt, 10_small_random_07.txt, 10_small_random_08.txt, 10_small_random_09.txt, 50_large_random_01.txt, 50_large_random_02.txt, 50_large_random_03.txt, 50_large_random_04.txt, 50_large_random_05.txt, 60_x_axis_01.txt, 60_y_axis_01.txt, 70_corner_points_01.txt, 70_single_point_01.txt, 70_upper_right_01.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 17 ms 8996 KiB
00_sample_02.txt AC 17 ms 8884 KiB
00_sample_03.txt AC 17 ms 9144 KiB
00_sample_04.txt AC 19 ms 9104 KiB
10_small_random_01.txt AC 24 ms 9036 KiB
10_small_random_02.txt AC 18 ms 8972 KiB
10_small_random_03.txt AC 18 ms 9100 KiB
10_small_random_04.txt AC 25 ms 8884 KiB
10_small_random_05.txt AC 21 ms 9108 KiB
10_small_random_06.txt AC 19 ms 9108 KiB
10_small_random_07.txt AC 19 ms 9104 KiB
10_small_random_08.txt AC 18 ms 9024 KiB
10_small_random_09.txt AC 18 ms 9024 KiB
50_large_random_01.txt AC 398 ms 62540 KiB
50_large_random_02.txt AC 396 ms 62580 KiB
50_large_random_03.txt AC 400 ms 62748 KiB
50_large_random_04.txt AC 399 ms 62476 KiB
50_large_random_05.txt AC 402 ms 62452 KiB
60_x_axis_01.txt AC 344 ms 53240 KiB
60_y_axis_01.txt AC 337 ms 53112 KiB
70_corner_points_01.txt AC 378 ms 62636 KiB
70_single_point_01.txt AC 353 ms 62584 KiB
70_upper_right_01.txt AC 376 ms 62704 KiB