Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Distance II

  Your task is to calculate the distance between two $n$ dimensional vectors $x = \{x_1, x_2, ..., x_n\}$ and $y = \{y_1, y_2, ..., y_n\}$.

  The Minkowski's distance defined below is a metric which is a generalization of both the Manhattan distance and the Euclidean distance.

\[
D_{xy} = (\sum_{i=1}^n |x_i - y_i|^p)^{\frac{1}{p}}
\]

It can be the Manhattan distance 

\[
D_{xy} = |x_1 - y_1| + |x_2 - y_2| + ... + |x_n - y_n|
\]

where $p = 1 $.

It can be the Euclidean distance

\[
D_{xy} = \sqrt{(|x_1 - y_1|)^{2} + (|x_2 - y_2|)^{2} + ... + (|x_n - y_n|)^{2}}
\]

where $p = 2 $.

Also, it can be the Chebyshev distance

\[
D_{xy} = max_{i=1}^n (|x_i - y_i|)
\]

where $p = \infty$

Write a program which reads two $n$ dimensional vectors $x$ and $y$, and calculates Minkowski's distance where $p = 1, 2, 3, \infty$ respectively.

Input

  In the first line, an integer $n$ is given. In the second and third line, $x = \{x_1, x_2, ... x_n\}$ and $y = \{y_1, y_2, ... y_n\}$ are given respectively. The elements in $x$ and $y$ are given in integers.

Output

  Print the distance where $p = 1, 2, 3$ and $\infty$ in a line respectively.
The output should not contain an absolute error greater than 10-5.

Constraints

$1 \leq n \leq 100$

$0 \leq x_i, y_i \leq 1000$

Sample Input

3
1 2 3
2 0 4

Sample Output

4.000000
2.449490
2.154435
2.000000



Sample input:
3
1 2 3
2 0 4


Sample output:
4.000000
2.449490
2.154435
2.000000

Submission status: Accepted

Solution code:
import math

n = int(input())
x = input().split()
x_list = [int(xi) for xi in x]
y = input().split()
y_list = [int(yi) for yi in y]
dm, de, d3, dc = 0, 0, 0, 0
d = []

i = 0
while n > i:
    d.append(abs(x_list[i] - y_list[i]))
    dm += d[i]
    i += 1

i = 0
while n > i:
    de += (x_list[i] - y_list[i]) ** 2
    i += 1
de = math.sqrt(de)

i = 0
while n > i:
    d3 += abs(x_list[i] - y_list[i]) ** 3
    i += 1
d3 = d3 ** (1/3)

print(dm)
print(de)
print(d3)
print(max(d))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d1 = 0
d2sqr = 0
d3cube = 0
d_inf = 0

for i in range(n):
    d1 += abs(a[i] - b[i])
    d2sqr += (a[i] - b[i]) ** 2
    d3cube += abs(a[i] - b[i]) ** 3
    d_inf = max(d_inf, abs(a[i] - b[i]))

d2 = d2sqr ** (1/2)
d3 = d3cube ** (1/3)

print(d1)
print(d2)
print(d3)
print(d_inf)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

p1,p2,p3,p4 = 0,0,0,0
for i,j in zip(x,y):
    p1 += abs(i-j)
    p2 += abs(i-j)**2
    p3 += abs(i-j)**3
    p4 = abs(i-j) if p4 < abs(i-j) else p4

print(p1)
print(p2**0.5)
print(p3**(1/3))
print(float(p4))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
sum1 = 0
sum2 = 0
sum3 = 0
sum8 = 0
for i in range(n):
    d = abs(x[i]-y[i])
    sum1 += d
    sum2 += d**2
    sum3 += d**3
    if sum8 < d:
        sum8 = d
print(sum1)
print(sum2**0.5)
print(sum3**(1/3))
print(sum8)

Submission status: Accepted

Solution code:
import math
# import numpy as np


def D1(x,y):
    d1 = 0
    for i in range(len(x)):
        d1 +=  abs(x[i] - y[i])
    return d1

def D2(x,y):
    d2 = 0
    for i in range(len(x)):
        d2 +=  abs(x[i] - y[i]) ** 2
    return d2 ** (1/2)

def D3(x,y):
    d3 = 0
    for i in range(len(x)):
        d3 +=  abs(x[i] - y[i]) ** 3
    return d3 ** (1/3)

def Dmax(x,y):
    dmax = 0
    for i in range(len(x)):
        if dmax <= abs(x[i] - y[i]):
            dmax = abs(x[i] - y[i])
    return dmax

n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

print(D1(x,y))
print(D2(x,y))
print(D3(x,y))
print(Dmax(x,y))

Submission status: Accepted

Solution code:
import math

n = int(input())
X = [int(x) for x in input().split()]
Y = [int(y) for y in input().split()]

d1 = 0
d2 = 0
d3 = 0
Di = []

for i in range(n) :
    d1 += abs(X[i] - Y[i])
print(f"{d1:.6f}")

for i in range(n) :
    d2 += abs(X[i] - Y[i]) ** 2
print(f"{math.sqrt(d2):.6f}")

for i in range(n) :
    d3 += abs(X[i] - Y[i]) ** 3
print(f"{math.pow(d3, 1/3):.6f}")
    
for i in range(n) :
    Di.append(abs(X[i] - Y[i]))
print(f"{max(Di):.6f}")

Submission status: Accepted

Solution code:
def mink(X,Y,p):
    a =0
    for x,y in zip(X,Y):
        a+=abs(x-y)**p
    print(a**(1/p))

n = int(input())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))

for p in range(1,4):
    mink(X,Y,p)
    
print(max(abs(x-y) for x,y in zip(X,Y)))

Submission status: Accepted

Solution code:
import math

n = int(input())

x = list(map(float, input().split()))
y = list(map(float, input().split()))

#p=1
p_1 = 0
for i in range(n):
    p_1 += math.fabs(x[i] - y[i])

#p=2
p_2 = 0
for i in range(n):
    p_2 += math.fabs(x[i] - y[i]) ** 2
p_2 = math.sqrt(p_2)

#p=3
p_3 = 0
for i in range(n):
    p_3 += math.fabs(x[i] - y[i]) ** 3
p_3 = pow(p_3, (1/3))

#p is inf
prep = []
for i in range(n):
    prep.append(math.fabs(x[i] - y[i]))
p_inf = max(prep)

print('{:.6f}'.format(p_1))
print('{:.6f}'.format(p_2))
print('{:.6f}'.format(p_3))
print('{:.6f}'.format(p_inf))

Submission status: Accepted

Solution code:

import math

n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

p1 = 0
for i in range(n):
    p1 += abs(x[i] - y[i])

p2 = 0
for i in range(n):
    p2 += (x[i] - y[i]) ** 2

p2 = math.sqrt(p2)

p3 = 0
for i in range(n):
    p3 += abs((x[i] - y[i])) ** 3

p3 = math.pow(p3, 1.0/3.0)

px = 0
l = list()
for i in range(n):
    l.append(abs(x[i] - y[i]))
    px = max(l)

print(f'{p1:.6f}')
print(f'{p2:.6f}')
print(f'{p3:.6f}')
print(f'{px:.6f}')

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))


for p in range(1, 4):
    D = 0
    for i in range(n):
        D += abs((x[i] - y[i]))**p
    ans = math.pow(D, 1/p)
    print(f"{ans:.6f}")

l = []
for i in range(n):
    l.append(abs((x[i] - y[i])))
print(max(l))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = input().split()
y = input().split()

d1 = 0
d2 = 0
d3 = 0
d = []
for i in range(n):
    d1 += abs(float(x[i]) - float(y[i])) **1
    d2 += abs(float(x[i]) - float(y[i])) **2
    d3 += abs(float(x[i]) - float(y[i])) **3
    d.append(abs(float(x[i]) - float(y[i])))

print('{}'.format(d1))
print('{}'.format(d2**(1/2)))
print('{}'.format(d3**(1/3)))
print('{}'.format(max(d)))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

# p = 1
ans = 0
for i in range(n):
    ans += abs(x[i]-y[i])
print (ans)

# p = 2
ans = 0
for i in range(n):
    ans += (abs(x[i]-y[i]))**2
print (ans**(1/2))

# p = 3
ans = 0
for i in range(n):
    ans += (abs(x[i]-y[i]))**3
print (ans**(1/3))

# p = inf
ans = []
for i in range(n):
    ans.append(abs(x[i]-y[i]))
print (max(ans))

Submission status: Accepted

Solution code:
def minkowski(xlst,ylst,p):
  d = list()
  for i in range(len(xlst)):
    d.append((abs(xlst[i] - ylst[i]))**p)
  D = sum(d) ** (1/p)
  return D

def mugen(xlst,ylst):
  d = list()
  for i in range(len(xlst)):
    d.append((abs(xlst[i] - ylst[i])))
  m = max(d)
  D = m
  return D

n = int(input())
xl = list(map(int,input().split()))
yl = list(map(int,input().split()))

D1 = minkowski(xl, yl, 1)
D2 = minkowski(xl, yl, 2)
D3 = minkowski(xl, yl, 3)
Dm = mugen(xl, yl)

print(D1)
print(D2)
print(D3)
print(Dm)

Submission status: Accepted

Solution code:
import math

n=int(input())
x=list(map(float,input().split(" ")))
y=list(map(float,input().split(" ")))

#p=1
print(sum(list(map(lambda x,y:abs(x-y),x,y))))
#p=2
print(math.sqrt(sum(list(map(lambda x,y:(x-y)**2,x,y)))))
#p=3
print(math.pow(sum(list(map(lambda x,y:abs(x-y)**3,x,y))),1/3))
#p=∞
print(max(list(map(lambda x,y:abs(x-y),x,y))))

Submission status: Accepted

Solution code:
n = int(input())
x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]

man_x_y = sum(abs(x[i]-y[i]) for i in range(n)) 
euk2_x_y = (sum((x[i]-y[i])**2 for i in range(n)))**(1/2)
euk3_x_y = (sum((abs(x[i]-y[i]))**3 for i in range(n)))**(1/3)

che_x_y = max(abs(x[i]-y[i]) for i in range(n)) 
print(man_x_y)
print(euk2_x_y)
print(euk3_x_y)
print(che_x_y)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))

def calc(X, Y, p):
    count = 0
    for x, y in zip(X, Y):
        a = abs(x - y)
        count += a ** p
    return count ** (1 / p)

print(calc(X, Y, 1))
print(calc(X, Y, 2))
print(calc(X, Y, 3))
print(max(abs(x - y) for x, y in zip(X, Y)))

Submission status: Accepted

Solution code:
import functools


if __name__ == "__main__":
    n = int(input())

    xs = list(map(int, input().split()))
    ys = list(map(int, input().split()))

    diffs = list(map(lambda x, y: abs(x - y), xs, ys))

    manhattan_distance = functools.reduce(lambda acc, diff: acc + diff, diffs, 0.0)
    euclidean_distance = (functools.reduce(lambda acc, diff: acc + (diff ** 2), diffs, 0.0))**(1 / 2)
    minkowski_distance_p3 = (functools.reduce(lambda acc, diff: acc + (diff ** 3), diffs, 0.0))**(1 / 3)
    chebyshev_distance = max(diffs)

    print(manhattan_distance)
    print(euclidean_distance)
    print(minkowski_distance_p3)
    print(chebyshev_distance)

Submission status: Accepted

Solution code:
n = int(input())
x_list = list(map(int, input().split()))
y_list = list(map(int, input().split()))
d = [abs(x - y) for x, y in zip(x_list, y_list)]

for i in range(1, 4):
    print(sum([j ** i for j in d]) ** (1 / i))
    
print(max(d))

Submission status: Accepted

Solution code:
N = int(input())
X = [float(x) for x in input().split()]
Y = [float(y) for y in input().split()]

for p in range(1, 4):
    print(sum(map(lambda x, y: abs(x - y) ** p, X, Y)) ** (1 / p))
print(max(map(lambda x, y: abs(x - y), X, Y)))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = [0]*n
y = [0]*n
x = list(map(float,input().split()))
y = list(map(float,input().split()))
dist1 = 0
dist2 = 0
dist3 = 0
diste = 0
for i in range(n):
    dist1 += abs(x[i]-y[i])
    dist2 += abs(x[i]-y[i])**2
    dist3 += abs(x[i]-y[i])**3
    diste = max(diste,abs(x[i]-y[i]))
print(str(dist1))
print(str(dist2**(1/2)))
print(str(dist3**(1/3)))
print(str(diste))

Submission status: Accepted

Solution code:
n =int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))

def distance(X,Y,p):
    l = []
    sum = 0
    
    for xi, yi in zip(X,Y):
        d = abs(xi-yi)
        l.append(d)
        if p != 'inf':
            sum+=d**p
    
    if p == 'inf':
        D = max(l)
    else:
        D = round(sum**(1/p),5)
        
    print(D)
    
P = [1,2,3,'inf']
for p in P:
    distance(X,Y,p)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dif = [abs(a[i]-b[i]) for i in range(n)]
dif2 = [x**2 for x in dif]
dif3 = [x**3 for x in dif]

print(sum(dif))
print((sum(dif2))**(1/2))
print((sum(dif3))**(1/3))
print(max(dif))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = [float(f) for f in input().split()]
y = [float(f) for f in input().split()]
L1 = 0
for i in range(n):
    L1 += abs(x[i] - y[i])
L2 = 0
for i in range(n):
    L2 += abs(x[i] - y[i])**2
L2 = pow(L2, 1.0/2.0)
L3 = 0
for i in range(n):
    L3 += abs(x[i] - y[i])**3
L3 = pow(L3, 1.0/3.0)
Linf = 0
for i in range(n):
    Linf = max(Linf, abs(x[i] - y[i]))
print(L1)
print(L2)
print(L3)
print(Linf)

Submission status: Accepted

Solution code:
import math

n = int(input())
xlist = list(map(int, input().split()))
ylist = list(map(int, input().split()))
d1 = sum([abs(x - y) for x, y in zip(xlist, ylist)])
d2 = sum([abs(x - y)**2 for x, y in zip(xlist, ylist)])**0.50
d3 = sum([abs(x - y)**3 for x, y in zip(xlist, ylist)])**(1/3)
dinf = max([abs(x - y) for x, y in zip(xlist, ylist)])
print(d1)
print(d2)
print(d3)
print(dinf)

Submission status: Accepted

Solution code:
N = int(input())

X = [int(n) for n in input().split()]
Y = [int(n) for n in input().split()]

d1,d2,d3,dinf= 0,0,0,0


for i in range(N):
    # p=1
    d1 += abs(X[i]-Y[i])
    # p=2
    d2 += (X[i]-Y[i])**2
    # p=3
    d3 += (abs(X[i]-Y[i]))**3
    # p=inf
    dinf = max(abs(X[i]-Y[i]),dinf)

print(d1)
print(d2**(1/2))
print(d3**(1/3))
print(dinf)

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d_one = 0
d_two = 0
d_three = 0
d_inf = [0]*n


for i in range(n):
    d_one += abs(x[i]-y[i])
    d_two += abs(x[i] - y[i]) ** 2
    d_three += abs(x[i] - y[i]) ** 3
    d_inf[i] = abs(x[i] - y[i]) 
    
print(d_one)
print(math.sqrt(d_two))
print(math.pow(d_three, 1.0 / 3.0))
print(max(d_inf))

Submission status: Accepted

Solution code:
n = int(input())
x_v = list(map(int, input().split(" ")))
y_v = list(map(int, input().split(" ")))

def mincos(x_v, y_v, p):
    wa = 0
    for i in range(n):
        sa = x_v[i] - y_v[i]
        if x_v[i] - y_v[i] < 0:
            sa = y_v[i] - x_v[i]
        wa += sa**p
    return wa**(1/p)
list=[]
print(float(mincos(x_v, y_v, 1)))
print(float(mincos(x_v, y_v, 2)))
print(float(mincos(x_v, y_v, 3)))
for i in range(n):
    sa = x_v[i] - y_v[i]
    if x_v[i] - y_v[i] < 0:
        sa = y_v[i] - x_v[i]
    list.append(sa)
print(max(list))

Submission status: Accepted

Solution code:
n = int(input())
xi = [int(x) for x in input().split()]
yi = [int(y) for y in input().split()]
ans1 = ans2 = ans3 = ansm = 0
for i in range(n):
    sa = abs(xi[i]-yi[i])
    ans1 += sa
    ans2 += sa**2
    ans3 += sa**3
    if ansm < sa:
        ansm = sa
print(ans1,ans2**(1/2),ans3**(1/3),ansm, end='\n')

Submission status: Accepted

Solution code:
n = input()
x = list(map(float, input().split()))
y = list(map(float, input().split()))


def distance(x, y, p):
    dist = sum([abs(x_ - y_)**p for x_, y_ in zip(x, y)]) ** (1/p)
    return dist


manhattan = distance(x, y, 1)
euclid = distance(x, y, 2)
minkowski = distance(x, y, 3)
chebyshev = max([abs(x_ - y_) for x_, y_ in zip(x, y)])

print(manhattan)
print(euclid)
print(minkowski)
print(chebyshev)

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

r_1 = 0
r_2 = 0
r_3 = 0
r_inf = 0

for i in range(n):
    r_1 += abs(x[i]-y[i]) 
print(r_1)

for i in range(n):
    r_2 += (x[i]-y[i])**2
r_2 = math.sqrt(r_2)
print(r_2)

for i in range(n):
    r_3 += abs(x[i]-y[i])**3
r_3 = math.pow(r_3, (1.0/3.0))
print(r_3)

for i in range(n):
    r_inf = max(abs(x[i]-y[i]), r_inf)
print(r_inf)

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

print(sum(map(lambda v: math.fabs(v[0] - v[1]), zip(x, y))))
print(sum(map(lambda v: math.fabs((v[0] - v[1]) ** 2), zip(x, y))) ** (1/2))
print(sum(map(lambda v: math.fabs((v[0] - v[1]) ** 3), zip(x, y))) ** (1/3))
print(max(map(lambda v: math.fabs(v[0] - v[1]), zip(x, y))))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
def d(a,b):
    if a>b:
        return a-b
    else:
        return b-a
d1=0
d2=0
d3=0
dinf=0
for i in range(n):
    d1+=d(x[i],y[i])
    d2+=(d(x[i],y[i]))**2
    d3+=(d(x[i],y[i]))**3
d2=d2**(1/2)
d3=d3**(1/3)
dinf=d(x[0],y[0])
for i in range(n):
    if dinf<d(x[i],y[i]):
        dinf=d(x[i],y[i])
print(round(d1,6))
print(round(d2,6))
print(round(d3,6))
print(round(dinf,6))

Submission status: Accepted

Solution code:
n = int(input())
*x, = map(int, input().split())
*y, = map(int, input().split())

abs_xy = [abs(i-j) for i,j in zip(x,y)]

print(sum(abs_xy))
print((sum(d**2 for d in abs_xy)) ** 0.5)
print((sum(d**3 for d in abs_xy)) ** (1/3))
print(max(abs_xy))

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(float, input().split(' ')))
y = list(map(float, input().split(' ')))

for p in range(1, 4):
    ans = 0
    for i in range(n):
        ans += math.pow(abs(x[i] - y[i]), p)
    ans = math.pow(ans, 1 / p)
    print(ans)

ans = 0
for i in range(n):
    ans = max(ans, abs(x[i] - y[i]))
print(ans)

Submission status: Accepted

Solution code:
def distance(x, y, p):
    length = len(x)
    if p == -1:
        return max(abs(x[i] - y[i]) for i in range(length))
    return sum([abs(i - j)**p for i, j in zip(x, y)])**(1/p)

n = int(input())
x = [*map(int, input().split())]
y = [*map(int, input().split())]
P = (1, 2, 3, -1)
for p in P:
    print('%f' % distance(x, y, p))

Submission status: Accepted

Solution code:
import math
n=int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
Dp = [0,0,0,0]

def Dp(x,y,n,p):
    i = 0
    ans = 0
    for i in range(n):
        ans += (math.fabs(x[i]-y[i]))**p
        i+=1
    return ans**(1/p)

def Dp_inf(x,y,n):
    i = 0
    ans = 0
    for i in range(n):
        if ans < math.fabs(x[i]-y[i]):
            ans = math.fabs(x[i]-y[i])
    return ans

for j in range(3):
    print(Dp(x,y,n,j+1))

print(Dp_inf(x,y,n))

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
distance = [0,0,0,0]

for i in range(n):
  distance[0] += math.fabs(x[i] - y[i])
  distance[1] += (x[i] - y[i])**2
  distance[2] += math.fabs((x[i] - y[i])**3)
  distance[3] = max(distance[3],math.fabs(x[i] - y[i]))

distance[1] = math.sqrt(distance[1])
distance[2] = distance[2]**(1/3)

for i in range(4):
  print(distance[i])

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

#p=1
res = 0
for i in range(n):
    res += abs(x[i]-y[i])
print(res)

#p=2
res = 0
for i in range(n):
    res += (x[i]-y[i])**2
print(res**0.5)

#p=3
res = 0
for i in range(n):
    res += (abs(x[i]-y[i]))**3
print(res**(1/3))

#p=inf
ma = 0
for i in range(n):
    tmp = abs(x[i]-y[i])
    if tmp > ma: ma = tmp
print(ma)

Submission status: Accepted

Solution code:
# Problem - ミンコフスキー距離

def minkowski(x_list, y_list, p):
    tmp_ans = 0
    for i in range(len(x_list)):
        tmp = abs(x_list[i] - y_list[i])**p
        tmp_ans += tmp
    tmp_ans = tmp_ans**(1/p)
    return tmp_ans

def minkowski_max(x_list, y_list):
    tmp_ans = 0
    for i in range(len(x_list)):
        tmp = abs(x_list[i] - y_list[i])
        tmp_ans = max(tmp_ans, tmp)
    return tmp_ans

# input
n = int(input())
x_list = list(map(int, input().split()))
y_list = list(map(int, input().split()))

# output
print(minkowski(x_list, y_list, 1))
print(minkowski(x_list, y_list, 2))
print(minkowski(x_list, y_list, 3))
print(minkowski_max(x_list, y_list))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d = [abs(a-b) for (a,b) in zip(x,y)]
print(sum(d))
s = 0
for i in range(n):
    s += (x[i]-y[i])**2
print(math.sqrt(s))
s = 0
for i in range(n):
    s += abs(x[i]-y[i])**3
print(s**(1/3))
print(max(d))

Submission status: Accepted

Solution code:
n=int(input())
x=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]
l=[]
for a,b in zip(x,y):
    l.append(abs(a-b))
print("{:.6f}".format(sum(l)))
print("{:.6f}".format(sum([s**2 for s in l])**.5))
print("{:.6f}".format(sum([s**3 for s in l])**(1/3)))
print("{:.6f}".format(max(l)))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]

def dist(x, y, p):
    tmp = 0
    lst = []
    for x, y in zip(x, y):
        tmp += (abs(x-y))**p
        lst.append((abs(x-y)))
    if p==0:
        return max(lst)
    return tmp**(1/p)


print(dist(x, y, 1))
print(dist(x, y, 2))
print(dist(x, y, 3))
print(dist(x, y, 0))

Submission status: Accepted

Solution code:

def dist(list_x,list_y,p):
    sum = 0
    for i in range(len(list_x)):
        sum += abs(list_x[i]-list_y[i])**p
    return print(sum**(1/p))


def infi(list_x,list_y):
    sum=[]
    for i in range(len(list_x)):
        sum.append(abs(list_x[i]-list_y[i]))
    return print(max(sum))

#%%
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

dist(x,y,p=1)
dist(x,y,p=2)
dist(x,y,p=3)
infi(x,y)

Submission status: Accepted

Solution code:
n = int(input())
x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]

d = 0
for i in range(n):
    d += abs(x[i] - y[i])
print(d)

d = 0
for i in range(n):
    d += (x[i] - y[i])**2
print(d**0.5)

d = 0
for i in range(n):
    d += (abs(x[i] - y[i]))**3
print(d**(1/3))

print(max([abs(x[i] - y[i]) for i in range(n)]))

Submission status: Accepted

Solution code:
def dis(A,B,d):
    res = 0.0
    for i in range(len(A)):
        res += abs(A[i]-B[i])**d
    return res**(1.0/d)

def main():
    n = int(input())
    A = list(map(float,input().split()))
    B = list(map(float,input().split()))
    print(dis(A,B,1.0))
    print(dis(A,B,2.0))
    print(dis(A,B,3.0))
    res = 0.0
    for i in range(n):res = max(res,abs(A[i]-B[i]))
    print(res)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

def distance(p,x,y):
    ans = 0
    for i in range(len(x)):
        ans += (abs(x[i]-y[i])**p)
    return pow(ans,1/p)

def chebyshev(x,y):
    return max(abs(x[i]-y[i]) for i in range(len(x)))

N = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

print(distance(1,x,y))
print(distance(2,x,y))
print(distance(3,x,y))
print(chebyshev(x,y))

Submission status: Accepted

Solution code:
import math
f_inf = float("inf")
num = int(input())
s = input().rstrip().split(' ')
t = input().rstrip().split(' ')
d1 = 0
d2 = 0
d3 = 0
dinf = 0
for i in range(num):
  d1 = d1 + abs(int(s[i]) - int(t[i]))
  d2 = d2 + (int(s[i]) - int(t[i])) ** 2
  d3 = d3 + (abs(int(s[i]) - int(t[i]))) ** 3
  if abs(int(s[i]) - int(t[i])) > dinf:
    dinf = abs(int(s[i]) - int(t[i]))

d2 = math.sqrt(d2)
d3 = d3 ** (1/3)
print(d1)
print(d2)
print(d3)
print(dinf)

Submission status: Accepted

Solution code:
n = int(input())
xi = list(map(int,input().split()))
yi = list(map(int,input().split()))
dxy = [abs(a-b) for (a,b) in zip(xi,yi)]
for p in range(1,4):
    print (sum(map(lambda t: t**p,dxy))**(1/p))
print (max(dxy))

Submission status: Accepted

Solution code:
def distance(x, y, p):
    return sum([abs(i - j) ** p for i, j in zip(x, y)]) ** (1 / p)

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

[print('{:f}'.format(distance(x, y, _))) for _ in range(1, 4)]
print('{:f}'.format(max([abs(i - j) for i, j in zip(x, y)])))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

print(sum([abs(z) for z in [y - x for x, y in zip(x, y)]]))
print(sum([(y - x) ** 2 for x, y in zip(x, y)]) ** 0.5)
print(sum([abs(z) for z in [(y - x) ** 3 for x, y in zip(x, y)]]) ** (1/3))
print(max([abs(z) for z in [y - x for x, y in zip(x, y)]]))

Submission status: Accepted

Solution code:
# 入力、変数定義
n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
D1, D2, D3, Dinf = 0, 0, 0, 0

# D1の計算
for i in range(n):
    D1 += abs(x[i]-y[i])

#  D2の計算
for i in range(n):
    D2 += (x[i]-y[i])**2
D2 **= 1/2

#  D3の計算
for i in range(n):
    D3 += abs(x[i]-y[i])**3
D3 **= 1/3

#  Dinfの計算
for i in range(n):
    d = abs(x[i]-y[i])
    if d > Dinf:
        Dinf = d


print(D1, D2, D3, Dinf, sep='\n')

Submission status: Accepted

Solution code:

n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

for i in range(1,4):
    ans = 0
    for j in range(n):
        ans += abs(x[j] - y[j]) ** i
    else:
        ans **= 1/i
        print(ans)
        
max_d = 0
for i in range(n):
    max_d = max(max_d, abs(x[i]-y[i]))
ans = max_d

print(ans)

Submission status: Accepted

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
import statistics
import decimal
import heapq
import collections
import itertools
from operator import mul

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===
def getInputListF():
    outputData = []
    inputData = input().split()
    outputData = [float(n) for n in inputData]

    return outputData

def calcDis(a, b, num):
    ans = 0
    for i in range(len(a)):
        ans += abs(a[i]-b[i])**num
    ans = ans**(1/num)
    return ans

def calcChebi(a, b):
    ans = 0
    for i in range(len(a)):
        ans = max(ans, abs(a[i]-b[i]))
    return ans

n = int(input())
d1 = getInputListF()
d2 = getInputListF()

print(calcDis(d1, d2, 1))
print(calcDis(d1, d2, 2))
print(calcDis(d1, d2, 3))
print(calcChebi(d1, d2))

Submission status: Accepted

Solution code:
import math
input()
a=[int(item) for item in input().split()]
b=[int(item) for item in input().split()]
#マンハッタン距離
manhattan=sum([abs(x-y) for x,y in zip(a,b)])
#ユークリッド距離
eucrid=sum([(x-y)**2 for x,y in zip(a,b)])**(1/2)
#3のときの距離
sono3=sum([abs(x-y)**3 for x,y in zip(a,b)])**(1/3)
#チェビシェフ距離
chebyshev=max([abs(x-y) for x,y in zip(a,b)])
print("{}\n{}\n{}\n{}".format(manhattan,eucrid,sono3,chebyshev))

Submission status: Accepted

Solution code:
n = int(input())
an = [int(x) for x in input().split()]
bn = [int(x) for x in input().split()]

print(sum([abs(an[i] - bn[i]) for i in range(n)]))
print(sum([abs(an[i] - bn[i])**2 for i in range(n)])**(1/2))
print(sum([abs(an[i] - bn[i])**3 for i in range(n)])**(1/3))
print(max([float(abs(an[i] - bn[i])) for i in range(n)]))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
def D(p):
    s = 0
    for i in range(n):
        s += (abs(x[i]-y[i]))**p
    return s**(1/p)
L = []
for i in range(n):
    L.append(abs(x[i]-y[i]))
print(D(1))
print(D(2))
print(D(3))
print(max(L))

Submission status: Accepted

Solution code:
def minkowski_dist(x, y, n, p):
    d = 0
    for i in range(n):
        d += abs(x[i] - y[i])**p
    d = d**(1/p)
    return d

n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

for p in range(1,4):
    print(minkowski_dist(x,y,n,p))

d = abs(x[0]-y[0])
for i in range(n):
    el = abs(x[i]-y[i])
    d = max(d,el)
print(d)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    x_y = [abs(i - j) for i, j in zip(x, y)]
    
    for p in range(1, 4):
        tmp = 0
        for i in range(N):
            tmp += pow(x_y[i], p)
        print(pow(tmp, 1/p))
    print(max(x_y))




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
n=int(input())
x=list(map(float, input().split()))
y=list(map(float, input().split()))

c=[abs(x[i]-y[i]) for i in range(n)]

for i in range(1,4):
    ans=0
    for d in c:
        ans += d**i
    print(f"{ans**(1/i):.6f}")
print(f"{max(c):.6f}")

Submission status: Accepted

Solution code:

import math
N = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

p_1 = sum((abs(x[i]-y[i])) for i in range(N))

p_2 = math.sqrt(sum(((x[i]-y[i])**2 for i in range(N))))

p_3 = (sum(abs(x[i]-y[i])**3 for i in range(N)))**(1/3)

p_m = max(abs(x[i]-y[i]) for i in range(N))

print(p_1)
print(p_2)
print(p_3)
print(p_m)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
for p in range(1,4):
    tmp = 0
    for x, y in zip(X, Y):
        tmp += abs(x-y)**p
    print(tmp**(1/p))
M = 0
for x, y in zip(X, Y):
     M = max(M, abs(x-y))
print(M)

Submission status: Accepted

Solution code:

def dist(X, Y, p):
    d = sum([abs(x - y)**p for x, y in zip(X, Y)])
    return d**(1/p)


N = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))

print(dist(X, Y, 1))
print(dist(X, Y, 2))
print(dist(X, Y, 3))
print(max([abs(x - y) for x, y in zip(X, Y)]))

Submission status: Accepted

Solution code:
import math

n = int(input())
x_list = list(map(int,input().split()))
y_list = list(map(int,input().split()))

ans = []
Chebyshev = 0
for i in range(1,4):
  Minkowski = 0
  for x, y in zip(x_list, y_list):
    Minkowski += abs(x - y) ** (i)
    Chebyshev = max(Chebyshev, abs(x - y))
  ans.append(Minkowski ** (1/i))
ans.append(Chebyshev)
for out in ans:
  print(float(out))

Submission status: Accepted

Solution code:
import math
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
p1=0
p2=0
p3=0
pinf=[]
for i in range(n):
	t=abs(x[i]-y[i])
	p1+=t
for i in range(n):
	t=(abs(x[i]-y[i]))**2
	p2+=t
p2=math.sqrt(p2)
for i in range(n):
	t=(abs(x[i]-y[i]))**3
	p3+=t
p3=p3**(1/3)
for i in range(n):
	pinf.append(abs(x[i]-y[i]))
p4=max(pinf)
print(p1)
print(p2)
print(p3)
print(p4)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
lop = []
p1 = 0
p2 = 0
p3 = 0
pm = 0
for i in range(n):
    p1 += float((abs(x[i]-y[i])))
    
for i in range(n):
    p2 += abs((x[i]-y[i]))**2
    
for i in range(n):
    p3 += (abs((x[i]-y[i])))**3
    
for i in range(n):
    lop.append(float((abs(x[i]-y[i]))))
    
print(p1)
print(p2**(1/2))
print(p3**(1/3))
print(max(lop))

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

D1 = 0
D2t = 0
D3t = 0
Di = []

for i in range(n):
    D1 += abs(x[i] - y[i])
    D2t += abs(x[i] - y[i]) ** 2
    D3t += abs(x[i] - y[i]) ** 3
    Di.append(abs(x[i] - y[i]))

print("{:f}".format(D1))
print("{:f}".format(math.sqrt(D2t)))
print("{:f}".format(D3t ** (1/3)))
print("{:f}".format(max(Di)))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

result1 = 0
for i in range(n):
    tmp = x[i] - y[i]
    if tmp > 0:
        result1 += tmp
    else:
        result1 -= tmp
        
result2 = 0
for i in range(n):
    tmp = x[i] - y[i]
    tmp = tmp**2
    result2 += tmp
result2 = math.sqrt(result2)

result3 = 0
for i in range(n):
    tmp = x[i] - y[i]
    tmp = tmp**3
    if tmp > 0:
        result3 += tmp
    else:
        result3 -= tmp
result3 = math.pow(result3, 1.0/3.0)

result4 = 0        
for i in range(n):
    tmp = x[i] - y[i]
    tmp = tmp**2
    tmp = math.sqrt(tmp)
    if tmp > result4:
        result4 = tmp
        
print(result1)
print(result2)
print(result3)
print(result4)