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 = input()
x = map(float, raw_input().split())
y = map(float, raw_input().split())
temp = p1 = p2 = p3 = pinf = 0

for i,j in zip(x, y):
    temp = math.fabs(i - j)
    p1 += temp
    p2 += temp ** 2
    p3 += temp ** 3
    if temp > pinf:
        pinf = temp

print u"%f\n%f\n%f\n%f" % (p1, p2**(1.0/2), p3**(1.0/3), pinf)

Submission status: Accepted

Solution code:
import math
n=int(input())
xs=list(map(float,input().split()))
ys=list(map(float,input().split()))
ds=[ abs(x - y) for (x,y) in zip(xs,ys) ]
print('{0:.6f}'.format(sum(ds)))
print('{0:.6f}'.format((sum(map(lambda x: x*x,ds)))**0.5))
print('{0:.6f}'.format((sum(map(lambda x: x*x*x,ds)))**(1./3.)))
print('{0:.6f}'.format(max(ds)))

Submission status: Accepted

Solution code:
# 22-Math_Functions-Distance_II.py

# ??????????????????????????¢
# ???????????????????????????????????????????????????????????????????????¢??§??¬???????????????
# ????????????????????°???????????????????§?????????¨????????§????????????????????????
# ????????§??????????????? n ?¬????????????????
#  x={x1,x2,...,xn} ??¨
#  y={y1,y2,...,yn} 
# ????????¢????¨????????????????????????????

# ????????????????????????????????¢?????¬???????¨?????????¨?????¨??????????¬??????????????????????????????¢?????\????????????????????? 
# Dxy=(???{i=1n}|xi???yi|^p)1/p

# p=1 ?????¨??? 
# Dxy=|x1???y1|+|x2???y2|+...+|xn???yn|

# ??¨?????????????????????????????????????????¢??¨?????°???????????? 

# p=2 ?????¨??? 
# Dxy=(|x1???y1|)2+(|x2???y2|)2+...+(|xn???yn|)2?????????????????????????????????????????????????????????????????????????????????????????????????????????
# ??¨???????????????????????¬?????????????????????????????????????????¢?????????????????? 

# p=??? ?????¨??? 
# Dxy=maxni=1(|xi???yi|)
# ??¨???????????????????????§????????§????????¢??¨?????°????????????

# ????????? n ?¬???????????????????????????????????????§???
# p ??????????????? 1???2???3?????? ?????????????????????????????¢????±???????????????°????????????????????????????????????

# Input
# ??????????????´??° n ????????????????????????
# ???????????????????????? x ???????´? {x1,x2,...xn}{x1,x2,...xn}???
# ???????????????????????? y ???????´? {y1,y2,...yn}{y1,y2,...yn} 
# ????????????????????§???????????????????????\?????????????????´??°?????§??????

# Output
# p ??????????????? 1???2???3?????? ??????????????????????????????????????¢??????????????????????????????
# ????????????0.00001 ??\????????????????????£????????????????????¨????????????

# Constraints
# 1???n???100
# 0???xi,yi???1000
# Sample Input
# 3
# 1 2 3
# 2 0 4
# Sample Output
# 4.000000
# 2.449490
# 2.154435
# 2.000000

n = int(input())
x = list( map(int, input().split()) )
y = list( map(int, input().split()) )
pp=[1.0, 2.0, 3.0, 500.0]


for p in pp:
	if p==500.0:
		print( "{0:.8f}".format( max( [abs(x[i]-y[i]) for i in range(n)] )  ))
	else:
		print( "{0:.8f}".format( sum( [abs(x[i]-y[i])**p for i in range(n)] )**(1./p)  ))

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
# p=1,2,3
for p in range(1,4):
    print("{0:.6f}".format(sum([abs(a-b)**p for a,b in zip(X,Y)])**(1/p)))
# p=infinity
print("{0:.6f}".format(max([abs(a-b) for a,b in zip(X,Y)])))

Submission status: Accepted

Solution code:
import sys
import math

def str_input():
    S = raw_input()
    if S[len(S)-1] == "\r":
        return S[:len(S)-1]
    return S

def float_to_str(num):
    return str("{:.10f}".format(num))

def list_input(tp):
    return map(tp, str_input().split())

# # # # # # # # # # # # # # # # # # # # # # # # #

n = input()
a = list_input(float)
b = list_input(float)

for d in xrange(1, 4):
    dist = 0
    for i in xrange(n):
        dist += pow(abs(a[i] - b[i]), d)
    dist = pow(dist, 1./d)
    print float_to_str(dist)

dist = 0
for i in xrange(n):
    dist = max(dist, abs(a[i] - b[i]))
print dist

Submission status: Accepted

Solution code:
import math

Manhattan = 0
Euclid = 0
Three = 0
Chebychev = []
num = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

# ???????????????????????¢
for i in range(num):
	Manhattan += math.fabs(x[i] - y[i]) 
	Euclid += math.fabs((x[i] - y[i])**2)
	Three += math.fabs((x[i] - y[i])**3)
	Chebychev.append(math.fabs((x[i] - y[i])))

print(Manhattan)
print(math.sqrt(Euclid))
print(math.pow(Three,1/3))
print(max(Chebychev))

Submission status: Accepted

Solution code:
from math import sqrt

#def area########################################

def Manhattan(x,y):
	D=0.0
	for i in range(n):
		D+=abs(x[i]-y[i])
	return D

def Euclid(x,y):
	D=0.0
	for i in range(n):
		D+=(x[i]-y[i])**2
	D=sqrt(D)
	return D

def Noname(x,y):
	D=0.0
	for i in range(n):
		D+=abs((x[i]-y[i])**3)
	D**=1.0/3
	return D

def Infinity(x,y):
	L=list()
	for i in range(n):
		L.append(abs(x[i]-y[i]))			
	return max(L)

#################################################


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

print Manhattan(x,y)
print Euclid(x,y)
print Noname(x,y)
print Infinity(x,y)

Submission status: Accepted

Solution code:
from math import sqrt

#def area########################################

def Manhattan(x,y):
	D=0.0
	for i in range(n):
		D+=abs(x[i]-y[i])
	return D

def Euclid(x,y):
	D=0.0
	for i in range(n):
		D+=(x[i]-y[i])**2
	D=sqrt(D)
	return D

def Nazo(x,y):
	D=0.0
	for i in range(n):
		D+=abs((x[i]-y[i])**3)
	D**=1.0/3
	return D

def Infinity(x,y):
	L=list()
	for i in range(n):
		L.append(abs(x[i]-y[i]))			
	return max(L)

#################################################


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

print Manhattan(x,y)
print Euclid(x,y)
print Nazo(x,y)
print Infinity(x,y)

Submission status: Accepted

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

D = [x for i in range(4)]

for i in range(len(D)):
    if i == len(D) -1:
        D[i] = 0
        for j in range(n):
            di = math.fabs(x[j]-y[j])
            if D[i] < di:
                D[i] = di

    else:
        p = 1
        p += i
        di = 0
        d = 0
        for k in range(n):
            d += math.fabs(x[k]-y[k])**p
        di = d**(1/p)
        D[i] = di

for i in range(len(D)):
    print(D[i])

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()]

for p in range(1,4):
    s =sum([abs(x[i]-y[i])**p for i in range(n)])
    s = s**(1/p)
    print("%.6f" % s)

s = max([abs(x[i]-y[i]) for i in range(n)])
print("%.6f" % s)

Submission status: Accepted

Solution code:
import math
deg = int(input())
li_x = list(map(int,input().split()))
li_y = list(map(int, input().split()))
p1_dis = 0.0
p2_dis = 0.0
p2_temp = 0.0
p3_dis = 0.0
p3_temp = 0.0
p4_dis = 0.0
p4_temp = [ ]

for i in range(deg):
	p1_dis += math.fabs(li_x[i] - li_y[i])
	p2_temp += (li_x[i] - li_y[i]) **2.0
	p3_temp += math.fabs(li_x[i] - li_y[i]) **3.0
	p4_temp.append( math.fabs(li_x[i] - li_y[i]) )

p2_dis = math.sqrt( p2_temp )
p3_dis = math.pow(p3_temp, 1.0/3.0)
p4_dis = max(p4_temp)

print('{0:.6f}'.format(p1_dis))
print('{0:.6f}'.format(p2_dis))
print('{0:.6f}'.format(p3_dis))
print('{0:.6f}'.format(p4_dis))

Submission status: Accepted

Solution code:
n=int(input())
vector1=[int(x) for x in input().split()]
vector2=[int(x) for x in input().split()]
for i in range(1,4):
    print("{0:.8f}".format(sum([abs(a-b)**i for a,b in zip(vector1,vector2)])**(1/i)))
print("{0:.8f}".format(max([abs(a-b) for a,b in zip(vector1,vector2)])))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d = [abs(x[i] - y[i]) for i in range(n)]
for i in range(1,4):
    print('{0:f}'.format(sum(j ** i for j in d) ** (1 / i)))
print('{0:f}'.format(max(d)))

Submission status: Accepted

Solution code:
import math

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

for p in range(1, 4):
    if p == 1:
        print('{0:f}'.format(sum([abs(a - b) for a, b in zip(x, y)])))
    else:
        print('{0:f}'.format(math.pow(sum([math.pow(abs(a - b), p) for a, b in zip(x, y)]), 1 / p)))

print('{0:f}'.format(max(abs(a - b) for a, b in zip(x, y))))

Submission status: Accepted

Solution code:

import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
t1 = t2 = t3 = ti = 0.0
for a, b in zip(x, y):
	d1 = abs(a - b)
	ti = max(ti, d1)
	t1 += d1
	t2 += d1**2
	t3 += d1**3
print(t1)
print(math.sqrt(t2))
print(t3 ** (1.0/3))
print(ti)

Submission status: Accepted

Solution code:
import math
n = int(input())
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
p1, p2, p3, p4 = 0, 0, 0, 0
hyp = 0
for i in range(n):
    p1 += math.fabs(a1[i] -a2[i])
    p2 += (a1[i]-a2[i])**2
    p3 += math.fabs((a1[i]-a2[i])**3)
    hyp = math.fabs(a1[i] -a2[i]) 
    if hyp > p4:
        p4 = hyp

p2 = math.sqrt(p2)
if p3 != 0:
    p3 = math.exp(math.log(p3)/3)
print(p1)
print(p2)
print(p3)
print(p4)

Submission status: Accepted

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

sum1 = 0.00
for i in range(n):
	sum1 += abs(x[i]-y[i])
print '%.6f'%sum1

sum2 = 0.00
for i in range(n):
	sum2 += pow(abs(x[i]-y[i]), 2)
print '%.6f'%pow(sum2, 0.5)

sum3 = 0.00
for i in range(n):
	sum3 += pow(abs(x[i]-y[i]), 3)
print '%.6f'%pow(sum3, 1.00/3)

sum4 = 0.00
for i in range(n):
	sum4 = max(sum4, abs(x[i]-y[i]))
print '%.6f'%(sum4)

Submission status: Accepted

Solution code:
import math

n = int(raw_input())
X = map(float, raw_input().split(" "))
Y = map(float, raw_input().split(" "))

print(sum([math.fabs(X[i] - Y[i]) for i in range(n)]))
print(math.sqrt(sum([math.fabs(X[i] - Y[i])**2 for i in range(n)])))
print(math.pow(sum([math.fabs(X[i] - Y[i])**3 for i in range(n)]), (1.0/3.0)))
print(max([math.fabs(X[i] - Y[i]) for i in range(n)]))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import math
n = int(input())
x, y = [list(map(int, input().split())) for _ in range(2)]
for p in (1,2,3,0):
    if p == 0:
        print(max(abs(x-y) for x, y in zip(x,y)))
    else:
        print(math.pow(sum(abs(x-y) ** p for x, y in zip(x,y)), 1.0 / p))

Submission status: Accepted

Solution code:
n = input()
x = list(map(int,raw_input().split()))
y = list(map(int,raw_input().split()))
gap = []
gap1 = 0.0
gap2 = 0.0
gap3 = 0.0
a = 0.0

for i in range(n):
    gap.append(abs(x[i]-y[i]))

for i in range(n):
    gap1 += gap[i]
print gap1

for i in range(n):
    gap2 += gap[i] ** 2  

print pow(gap2,0.5)

for i in range(n):
    gap3 += gap[i] ** 3
print pow(gap3,0.3333333333)

print max(gap)

Submission status: Accepted

Solution code:
# coding:utf-8
import math

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

manh_dist=0.0
for i in range(0,N):
    manh_dist=manh_dist+math.fabs(x[i]-y[i])

print('%.6f'%manh_dist)

eucl_dist=0.0
for i in range(0,N):
    eucl_dist=eucl_dist+(x[i]-y[i])**2
eucl_dist=math.sqrt(eucl_dist)

print('%.6f'%eucl_dist)

mink_dist=0.0
for i in range(0,N):
    mink_dist=mink_dist+math.fabs(x[i]-y[i])**3
mink_dist=math.pow(mink_dist,1.0/3)

print('%.6f'%mink_dist)

cheb_dist=0.0
for i in range(0,N):
    if cheb_dist<math.fabs(x[i]-y[i]):
        cheb_dist=math.fabs(x[i]-y[i])

print('%.6f'%cheb_dist)

Submission status: Accepted

Solution code:
import math

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

for p in range(4):
    p = float(p)
    D = 0
    for j in range(n):
        if p == 3.0:
            D = max(D, abs(x[j] - y[j]))
        else:
            D += math.pow(abs(x[j] - y[j]), p+1)
    if p == 3.0:
        print D
    else:
        print math.pow(D, 1/(p+1))

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):
 sum = 0
 for i in range(n):
  sum += math.fabs(x[i]-y[i])**p
 print(sum**(1/p))

a = []
for i in range(n):
 a.append(math.fabs(x[i]-y[i]))
a.sort()
print(a[n-1])

Submission status: Accepted

Solution code:
def calc_minkowski_distance(x1, x2, p):
    """calc L_p norm(finite dimension)

    Args:
        x1: vector1
        x2: vector2
        p: p in L_p

    Returns:
        L_p distance between x1 and x2 (float)
    """
    if p == "inf":
        return max([abs(x[0] - x[1]) for x in zip(x1, x2)])
    else:
        return sum([abs(x[0] - x[1]) ** (float(p)) for x in zip(x1, x2)]) ** float(1.0 / p)


target = [1, 2, 3, "inf"]
dimension = int(raw_input())
x1 = [float(x) for x in raw_input().split()]
x2 = [float(x) for x in raw_input().split()]
for p in target:
    print(calc_minkowski_distance(x1, x2, p))

Submission status: Accepted

Solution code:
import math


def calc_minkowski_distance(x1, x2, p):
    """calc L_p norm(finite dimension)

    Args:
        x1: vector1
        x2: vector2
        p: p in L_p

    Returns:
        L_p distance between x1 and x2 (float)
    """
    if p == "inf":
        return max([math.fabs(x[0] - x[1]) for x in zip(x1, x2)])
    else:
        return math.pow(sum([math.pow(math.fabs(x[0] - x[1]), p) for x in zip(x1, x2)]), 1.0 / p)


target = [1, 2, 3, "inf"]
dimension = int(raw_input())
x1 = [float(x) for x in raw_input().split()]
x2 = [float(x) for x in raw_input().split()]
for p in target:
    print(calc_minkowski_distance(x1, x2, p))

Submission status: Accepted

Solution code:
import math


def calc_minkowski_distance(data_x, data_y, types):
    # ???????´???????????¨???????map???????????¬??????????????????????????¨????????¨????????§?¶???????????????????????????§????????????????????????
    distance = list(map(lambda x, y:abs(x - y), data_x, data_y))

    results = []
    for t in types:
        if t == 'Inf':
            results.append(max(distance))
        else:
            temp = map(lambda x:math.pow(x, t), distance)
            total = sum(temp)
            results.append(math.pow(total, 1.0/t))
    return results


if __name__ == '__main__':
    # ??????????????\???
    # data_x = [1, 2, 3]
    # data_y = [2, 0, 4]
    num = int(input())
    data_x = [int(x) for x in input().split(' ')]
    data_y = [int(x) for x in input().split(' ')]

    # ???????????????
    types = [1, 2, 3, 'Inf']  # math.inf???python3.5??\?????????
    results = calc_minkowski_distance(data_x, data_y, types)

    # ???????????¨???
    for r in results:
        print('{0:.8f}'.format(r))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
p1 = sum([math.fabs(x[i]-y[i]) for i in range(n)])
p2 = math.sqrt(sum([math.fabs(x[i]-y[i])**2 for i in range(n)]))
p3 = math.pow(sum([math.fabs(x[i]-y[i])**3 for i in range(n)]), 1/3)
pp = max([math.fabs(x[i]-y[i]) for i in range(n)])
print("{0:.6f}".format(p1))
print("{0:.6f}".format(p2))
print("{0:.6f}".format(p3))
print("{0:.6f}".format(pp))

Submission status: Accepted

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

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

from math import pow
d2=pow(d2,(1.0/2.0))
d3=pow(d3,(1.0/3.0))
di=max([abs(x[i]-y[i]) for i in range(n)])

print(d1)
print(d2)
print(d3)
print(di)

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())

x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]

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

for p in range(1,4):
    d = sum([i ** p for i in dl]) ** (1/p)
    print("{:.8f}".format(d))

print("{:.8f}".format(max(dl)))

Submission status: Accepted

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

c = list(map(lambda x: abs(x[0]-x[1]), list(zip(a, b))))
print("{:.5f}".format(sum(c))) # 1

d = list(map(lambda x: x**2, c))
print("{:.5f}".format(math.sqrt(sum(d)))) # 2

e = list(map(lambda x: x**3, c))
print("{:.5f}".format(math.pow(sum(e), 1.0/3.0))) # 3

print("{:.5f}".format(max(c))) # inf

Submission status: Accepted

Solution code:
import math
n = int(input())
xs = [int(_) for _ in input().split()]
ys = [int(_) for _ in input().split()]

print(sum([abs(x-y) for x, y in zip(xs, ys)]))
print(math.sqrt(sum([pow(abs(x-y),2) for x, y in zip(xs, ys)])))
print(math.pow(sum([pow(abs(x-y),3) for x, y in zip(xs, ys)]), 1/3))
print(max([abs(x-y) for x, y in zip(xs, ys)]))

Submission status: Accepted

Solution code:
n=eval(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
def minkowski(p,x_1,x_2,n):
    if p==0:
        return float(max([abs(x_1[i]-x_2[i]) for i in range(n)]))
    else:
        return sum([(abs(x_1[i]-x_2[i]))**p for i in range(n)])**(1/p)
for i in range(4):
    print(minkowski((i+1)%4,x,y,n))

Submission status: Accepted

Solution code:
n = int(input())
a, b = [[int(el) for el in input().split(' ')] for _ in range(2)]
print(sum(abs(a[i] - b[i]) for i in range(n)))
print(sum((a[i] - b[i]) ** 2 for i in range(n)) ** 0.5)
print(sum(abs(a[i] - b[i]) ** 3 for i in range(n)) ** (1/3))
print(max(abs(a[i] - b[i]) for i in range(n)))

Submission status: Accepted

Solution code:
input()
X_vec = map(float, input().split())
Y_vec = map(float, input().split())
pair = list(zip(X_vec, Y_vec))
D1 = sum(abs(x-y) for x,y in pair)
D2 = (sum((x-y)**2 for x,y in pair)) ** .5
D3 = (sum(abs(x-y)**3 for x,y in pair)) ** (1/3)
Dinf = max(abs(x-y) for x,y in pair)
print("{0:.6f}\n{1:.6f}\n{2:.6f}\n{3:.6f}\n".format(D1,D2,D3,Dinf))

Submission status: Accepted

Solution code:
n = int(input())
xn = list(map(int, input().split()))
yn = list(map(int, input().split()))

Dp1, Dp2, Dp3, Dpinf = [0.0]*4

for (x, y) in zip(xn, yn):
    a = abs(x-y)
    Dp1 += a
    Dp2 += a**2
    Dp3 += a**3
    Dpinf = a if a > Dpinf else Dpinf

Dp2 = Dp2**(1/2)
Dp3 = Dp3**(1/3)

print(Dp1, Dp2, Dp3, Dpinf, sep='\n')

Submission status: Accepted

Solution code:
n = int(input())
x = map(float, input().strip().split())
y = map(float, input().strip().split())
d = tuple(map(lambda x,y:abs(x-y), x, y))
print(sum(d))
print(sum(map(pow, d, [2]*n))**0.5)
print(sum(map(pow, d, [3]*n))**(1/3))
print(max(d))

Submission status: Accepted

Solution code:
n = int(input())
x = map(float, input().strip().split())
y = map(float, input().strip().split())
d = tuple(map(lambda i,j: abs(i-j), x, y))
print(sum(d))
print(sum(map(pow, d, [2]*n))**0.5)
print(sum(map(pow, d, [3]*n))**(1/3))
print(max(d))

Submission status: Accepted

Solution code:
import math
n = int(raw_input())
x = map(lambda l: int(l)*1.0, (raw_input()).split(" "))
y = map(lambda l: int(l)*1.0, (raw_input()).split(" "))

for p in range(1,4):
    d = [math.pow(math.fabs(x[i] - y[i]), p) for i in range(n)]
    print math.pow(sum(d), 1.0/p)

d = [math.fabs(x[i]-y[i]) for i in range(n)]
print max(d)

Submission status: Accepted

Solution code:
def minkowski(n, x, y, p):
    D = 0
    for i in range(n):
        D += abs(x[i] - y[i])**p

    return(D**(1/p))

def chebyshev(n, x, y):
    D = []
    for i in range(n):
        D.append(abs(x[i] - y[i]))

    return(max(D))

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

print(minkowski(n, x, y, 1))
print(minkowski(n, x, y, 2))
print(minkowski(n, x, y, 3))

print(chebyshev(n, x, y))

Submission status: Accepted

Solution code:
import math
def nor(x,y,p):
    sum = 0
    for i in range(len(x)):
        sum += (abs(x[i]-y[i]))**p
    return sum**(1/p)
def nor_inf(x,y):
    tmp = []
    for i in range(len(x)):
        tmp.append(abs(x[i]-y[i]))
    return max(tmp)
                 
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

print(nor(x,y,1))
print(nor(x,y,2))
print(nor(x,y,3))
print(nor_inf(x,y))

Submission status: Accepted

Solution code:
from math import sqrt

n = int(input())
vector1 = map(int,input().split())
vector2 = map(int,input().split())
diffs = [abs(x-y) for x,y in zip(vector1,vector2)]
print (sum(diffs))
print (sqrt(sum(map(lambda n:pow(n,2),diffs))))
print ((sum(map(lambda n:pow(n,3),diffs)))**(1/3))
print (max(diffs))

Submission status: Accepted

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

D = [0,0,0,0]
dis = []
for i in range(n):
    D[0] += abs(x[i]-y[i])
    D[1] += (x[i]-y[i])**2
    D[2] += abs((x[i]-y[i]))**3
    dis.append(abs(x[i]-y[i]))

D[1] = math.sqrt(D[1])
D[2] = math.pow(D[2],1/3)
D[3] = max(dis)

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

Submission status: Accepted

Solution code:
import math

N = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
z = [abs(X-Y) for (X,Y) in zip(x, y)]

print(sum(z))

z2 = [Z ** 2 for Z in z]
z3 = [Z ** 3 for Z in z]

print(math.sqrt(sum(z2)))
print(math.pow(sum(z3),1/3))
print(max(z))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
for p in range(1,4):
    s = [math.fabs(x[i]-y[i])**p for i in range(n)]
    z = math.pow(sum(s), 1/p)
    print("{:.6f}".format(z))
t = [math.fabs(x[i]-y[i]) for i in range(n)]
print("{:.6f}".format(max(t)))

Submission status: Accepted

Solution code:
from math import *


def distance (p,n,x,y):
    dis = 0.0
    if p == "inf":
        for i in range(0,n) :
            if dis < abs(x[i]-y[i]):
                dis = abs(x[i]-y[i])
    else :
        for i in range(0,n) :
            dis += abs(x[i]-y[i])**p
        dis = dis ** (1.0/p)
    return dis


if  __name__ == '__main__' :
     n = int(input())
     x = map(int,raw_input().split())
     y = map(int,raw_input().split())
     print distance(1,n,x,y)
     print distance(2,n,x,y)
     print distance(3,n,x,y)
     v = distance('inf',n,x,y)
     print "%.6f" % v

Submission status: Accepted

Solution code:
import math

dim = int(input().strip())
vec_x = list(map(int,input().strip().split(" ")))
vec_y = list(map(int,input().strip().split(" ")))

dist_1 = 0
dist_2 = 0
tmp_dist_2 = 0
dist_3 = 0
tmp_dist_3 = 0
dist_infinite = 0
abs_xy = [0 for i in range(dim)]
for i in range(dim):
    abs_xy[i]= abs(vec_x[i]-vec_y[i])
    
    dist_1 = dist_1 + abs_xy[i]
    tmp_dist_2 = tmp_dist_2 + abs_xy[i] ** 2
    tmp_dist_3 = tmp_dist_3 + abs_xy[i] ** 3
    
dist_2 = math.sqrt(tmp_dist_2)
dist_3 = tmp_dist_3 ** (1/3)
dist_infinite = max(abs_xy)

print("{:.5f}\n{:.5f}\n{:.5f}\n{:.5f}".format(dist_1,dist_2,dist_3,dist_infinite))

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 [1.0,2.0,3.0]:
    s = 0
    for i in range(n):
        s += abs(x[i]-y[i])**p
    print(pow(s,1.0/p))
print(max([abs(x[i]-y[i]) for i in range(n)]))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = [int(i) for i in input().split()]
y = [int(j) for j in input().split()]
p_1 = p_2 = p_3 = p_m = 0

for i in range(n):
	diff = math.fabs(x[i]-y[i])
	p_1 += diff
	p_2 += diff*diff
	p_3 += diff*diff*diff
	if p_m < diff:
		p_m = diff
	
p_2 = math.sqrt(p_2) 
p_3 = math.pow(p_3, 1/3)

print('%.5f\n%.5f\n%.5f\n%.5f'% (p_1, p_2, p_3, p_m))

Submission status: Accepted

Solution code:
import math

n = int(input())
vector_x = [float(x) for x in input().split(" ")]
vector_y = [float(y) for y in input().split(" ")]

p1 = sum([abs(x - y) for x, y in zip(vector_x, vector_y)])
p2 = sum([abs(x - y) ** 2 for x, y in zip(vector_x, vector_y)]) ** (1/2)
p3 = sum([abs(x - y) ** 3 for x, y in zip(vector_x, vector_y)]) ** (1/3)
t = max([abs(x - y) for x, y in zip(vector_x, vector_y)])

print("{:.6f}".format(p1))
print("{:.6f}".format(p2))
print("{:.6f}".format(p3))
print("{:.6f}".format(t))

Submission status: Accepted

Solution code:
import math
n = int(input())
array = [[int(i) for i in input().split()] for i in range(2)] 
p1 = 0
p2 = 0
p3 = 0
pm = []
for i in range(n):
    p1 +=math.fabs(array[0][i]-array[1][i])
    p2 +=math.fabs(array[0][i]-array[1][i])**2
    p3 +=math.fabs(array[0][i]-array[1][i])**3
    pm.append(math.fabs(array[0][i]-array[1][i]))
print(p1)
print(math.sqrt(p2))
print(p3**(1/3))
print(max(pm))

Submission status: Accepted

Solution code:
n, x, y = int(input()), list(map(int, input().split())), list(map(int, input().split()))
d = tuple(map(lambda x, y:abs(x - y), x, y))
print(sum(d))
print(sum(map(pow, d, [2]*n))**0.5)
print(sum(map(pow, d, [3]*n))**(1/3))
print(max(d))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
p_1 = sum([abs(x[i]-y[i]) for i in range(n)])
p_2 = (sum([(x[i]-y[i])**2 for i in range(n)]))**0.5
p_3 = (sum([(abs(x[i]-y[i]))**3 for i in range(n)]))**(1/3)
p_inf = max([abs(x[i]-y[i]) for i in range(n)])

print("{:.5f}".format(p_1))
print("{:.5f}".format(p_2))
print("{:.5f}".format(p_3))
print("{:.5f}".format(p_inf))

Submission status: Accepted

Solution code:
elements = input()
vector_1 = list(map(float,input().split()))
vector_2 = list(map(float,input().split()))

for i in range(1,4):
    minkowski = sum([abs(a-b)**i for (a,b) in zip(vector_1,vector_2)])**(1/i)
    print("{0:.7f}".format(minkowski))

minkowski_inf=max([abs(a-b) for (a,b) in zip(vector_1,vector_2)])
print("{0:.7f}".format(minkowski_inf))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math

def distance(v0, v1, p):
    n = len(v0)
    s = 0
    for i in range(n):
        diff = abs(v0[i] - v1[i])
        s += diff ** p
    return s ** (1/p)

def chebyshev(v0, v1):
    n = len(v0)
    ret = 0
    for i in range(n):
        diff = abs(v0[i] - v1[i])
        if diff > ret:
            ret = diff
    return ret

n = int(input())
v0 = list(map(float, input().split()))
v1 = list(map(float, input().split()))

print(distance(v0, v1, 1))
print(distance(v0, v1, 2))
print(distance(v0, v1, 3))
print(chebyshev(v0, v1))

Submission status: Accepted

Solution code:
import math

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

d = [0 for _ in range(n)]
for i in range(n):
    d[i] = pow(math.fabs(x[i]-y[i]), 1)
D_1 = pow(sum(d), 1/1)

for i in range(n):
    d[i] = pow(math.fabs(x[i]-y[i]), 2)
D_2 = pow(sum(d), 1/2)

for i in range(n):
    d[i] = pow(math.fabs(x[i]-y[i]), 3)
D_3 = pow(sum(d), 1/3)

for i in range(n):
    d[i] = math.fabs(x[i]-y[i])
D_inf = max(d)

print(D_1)
print(D_2)
print(D_3)
print(D_inf)

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !

def calculate_minkowski_distances():
    _ = input()
    data = []
    for i in range(2):
        data.append([float(value) for value in input().rstrip().split()])

    for exponent in (1, 2, 3, float("inf")):
        print(minkowski_distance(data[0],data[1], exponent))
#        print("{0:.8f}".format(minkowski_distance(*data, exponent)))

def minkowski_distance(vector_x, vector_y, exponent):
#    abs_xy = [ abs(pair[0] - pair[1]) for pair in zip(vector_x, vector_y) ]
    abs_xy = [abs(vector_x[i] - vector_y[i]) for i in range(len(vector_x))]
    
    m_dis = 0
    if exponent == float("inf") :
        m_dis = max(abs_xy)
    elif exponent == 0 :
        m_dis = nan
    else :
        m_dis = sum([ elem**exponent for elem in abs_xy ])**(1/exponent)
    
    return m_dis

if __name__ == "__main__" :
    calculate_minkowski_distances()

Submission status: Accepted

Solution code:


def dis_func(x, y):
    dis = [round(0, 6) for i in range(4)]
    for x, y in zip(x, y):
        tmp = abs(x - y)
        dis[0] += tmp
        dis[1] += (tmp ** 2)
        dis[2] += (tmp ** 3)
        dis[3] = max(dis[3], tmp)

    dis[1] = dis[1] ** (1/2)
    dis[2] = dis[2] ** (1/3)
    print('\n'.join(list(map(str, dis))))


if __name__ == '__main__':
    n = int(input())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))

    dis_func(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(" ")]

for p in range(1, 4):
    print("{:.6f}".format(sum( [abs(a - b) ** p for a, b in zip(X,Y) ] ) ** (1 / p) ) )

print("{:.6f}".format(max( [ abs(a - b) for a, b in zip(X, Y) ] ) ) )

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import math

chebyshev = []

n = int(input())

x = input().split()
y = input().split()

for i in range(n):
    x[i] = int(x[i])
for i in range(n):
    y[i] = int(y[i])

for p in range(3):
    total = 0
    for i in range(n):
        total += (math.fabs(x[i]-y[i]))**(p+1)
    distance = total**(1/(p+1))
    print("%.5f" % distance)

for i in range(n):
    chebyshev.append(math.fabs(x[i]-y[i]))
print("%.5f" % max(chebyshev))

Submission status: Accepted

Solution code:
#ITP1_10-D Distance2
n = int(input())
x = [float(x) for x in input().split(" ")]
y = [float(x) for x in input().split(" ")]

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

d2=0.0
for i in range(n):
    d2 += (x[i]-y[i])**2
print(d2**(1/2))

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

d_inf=0.0
for i in range(n):
    if abs(x[i]-y[i])>d_inf:
        d_inf = abs(x[i]-y[i])
print(d_inf)

Submission status: Accepted

Solution code:
import math

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

Dp1 = 0
Dp2 = 0
Dp3 = 0
Dp4 = []
for i in range(n):
 xyi = abs(x[i] - y[i])
 Dp1 += xyi
 Dp2 += xyi ** 2
 Dp3 += xyi ** 3
 Dp4.append(xyi)
print("{0:05f}".format(Dp1))
print("{0:05f}".format(math.sqrt(Dp2)))
print("{0:05f}".format(math.pow(Dp3, (1 / 3))))
print("{0:05f}".format(max(Dp4)))