Problem p03006 (python)

Fixed submission:
n = int(input())
if n == 1:
    print(1)
else:
    buf = []
    for i in range(n):
        x,y = map(int,input().split())
        buf.append([x,y])
    
    buf.sort()
    
    ans = 1e9
    for i in range(n):
        for j in range(i+1,n):
            p,q = buf[j][0]-buf[i][0],buf[j][1]-buf[i][1]
    
            ind = 0
            used = set()
            tcost = 0
            befx = 0
            befy = 0
            flag = False
            while True:
                befx,befy = buf[ind][0],buf[ind][1]
                tcost += 1
                for l in range(n):
                    if l not in used:
                        tx,ty = buf[l][0],buf[l][1]
                        if tx-befx == p and ty-befy == q:
                            befx = tx
                            befy = ty
                            used.add(l)
                while True:
                    ind += 1
                    if ind == n:
                        flag = True
                        break
                    if ind in used:
                        continue
                    else:
                        break
                if flag:
                    break
            if ans > tcost:
                # print(used)
                # print(p,q,tcost)
                ans = tcost
    
    print(ans)

Buggy submission (verdict: Runtime Error):


def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g



n = int(input())
b1 = []

for i in range(n):
    b1.append(list(map(int,input().split())))

b2 = b1.copy()
b3 = b1.copy()
b4 = b1.copy()
b5 = b1.copy()
b6 = b1.copy()
b7 = b1.copy()
b8 = b1.copy()



b1.sort(key=lambda x:(x[0],x[1]))
b2.sort(key=lambda x:(x[1],x[0]))
b3.sort(key=lambda x:(x[0],-x[1]))
b4.sort(key=lambda x:(x[1],-x[0]))
b5.sort(key=lambda x:(-x[0],x[1]))
b6.sort(key=lambda x:(-x[1],x[0]))
b7.sort(key=lambda x:(-x[0],x[1]))
b8.sort(key=lambda x:(-x[1],x[0]))
# print(b1)
# print(b2)

costs = []

l1_x = {}
l1_y = {}

bef_x = b1[0][0]
bef_y = b1[0][1]
for i in range(1,n):
    diff_x = b1[i][0]-bef_x
    diff_y = b1[i][1]-bef_y
    bef_x = b1[i][0]
    bef_y = b1[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x]+=1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y]+=1

x_step = max(l1_x,key = l1_x.get)
y_step = max(l1_y,key = l1_y.get)


cost1 = 1
bef_x = b1[0][0]
bef_y = b1[0][1]
for i in range(1,n):
    diff_x = b1[i][0]-bef_x
    diff_y = b1[i][1]-bef_y
    bef_x = b1[i][0]
    bef_y = b1[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost1 += 1

costs.append(cost1)
#*****************************************************
l1_x = {}
l1_y = {}

bef_x = b2[0][0]
bef_y = b2[0][1]
for i in range(1, n):
    diff_x = b2[i][0] - bef_x
    diff_y = b2[i][1] - bef_y
    bef_x = b2[i][0]
    bef_y = b2[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x] += 1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y] += 1

x_step = max(l1_x, key = l1_x.get)
y_step = max(l1_y, key = l1_y.get)

cost2 = 1
bef_x = b2[0][0]
bef_y = b2[0][1]
for i in range(1, n):
    diff_x = b2[i][0] - bef_x
    diff_y = b2[i][1] - bef_y
    bef_x = b2[i][0]
    bef_y = b2[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost2 += 1

costs.append(cost2)




l1_x = {}
l1_y = {}

bef_x = b3[0][0]
bef_y = b3[0][1]
for i in range(1,n):
    diff_x = b3[i][0]-bef_x
    diff_y = b3[i][1]-bef_y
    bef_x = b3[i][0]
    bef_y = b3[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x]+=1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y]+=1

x_step = max(l1_x,key = l1_x.get)
y_step = max(l1_y,key = l1_y.get)


cost1 = 1
bef_x = b3[0][0]
bef_y = b3[0][1]
for i in range(1,n):
    diff_x = b3[i][0]-bef_x
    diff_y = b3[i][1]-bef_y
    bef_x = b3[i][0]
    bef_y = b3[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost1 += 1

costs.append(cost1)
#*****************************************************
l1_x = {}
l1_y = {}

bef_x = b4[0][0]
bef_y = b4[0][1]
for i in range(1, n):
    diff_x = b4[i][0] - bef_x
    diff_y = b4[i][1] - bef_y
    bef_x = b4[i][0]
    bef_y = b4[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x] += 1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y] += 1

x_step = max(l1_x, key = l1_x.get)
y_step = max(l1_y, key = l1_y.get)

cost2 = 1
bef_x = b4[0][0]
bef_y = b4[0][1]
for i in range(1, n):
    diff_x = b4[i][0] - bef_x
    diff_y = b4[i][1] - bef_y
    bef_x = b4[i][0]
    bef_y = b4[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost2 += 1

costs.append(cost2)




l1_x = {}
l1_y = {}

bef_x = b5[0][0]
bef_y = b5[0][1]
for i in range(1,n):
    diff_x = b5[i][0]-bef_x
    diff_y = b5[i][1]-bef_y
    bef_x = b5[i][0]
    bef_y = b5[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x]+=1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y]+=1

x_step = max(l1_x,key = l1_x.get)
y_step = max(l1_y,key = l1_y.get)


cost1 = 1
bef_x = b5[0][0]
bef_y = b5[0][1]
for i in range(1,n):
    diff_x = b5[i][0]-bef_x
    diff_y = b5[i][1]-bef_y
    bef_x = b5[i][0]
    bef_y = b5[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost1 += 1

costs.append(cost1)
#*****************************************************
l1_x = {}
l1_y = {}

bef_x = b6[0][0]
bef_y = b6[0][1]
for i in range(1, n):
    diff_x = b6[i][0] - bef_x
    diff_y = b6[i][1] - bef_y
    bef_x = b6[i][0]
    bef_y = b6[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x] += 1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y] += 1

x_step = max(l1_x, key = l1_x.get)
y_step = max(l1_y, key = l1_y.get)

cost2 = 1
bef_x = b6[0][0]
bef_y = b6[0][1]
for i in range(1, n):
    diff_x = b6[i][0] - bef_x
    diff_y = b6[i][1] - bef_y
    bef_x = b6[i][0]
    bef_y = b6[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost2 += 1

costs.append(cost2)




l1_x = {}
l1_y = {}

bef_x = b7[0][0]
bef_y = b7[0][1]
for i in range(1,n):
    diff_x = b7[i][0]-bef_x
    diff_y = b7[i][1]-bef_y
    bef_x = b7[i][0]
    bef_y = b7[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x]+=1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y]+=1

x_step = max(l1_x,key = l1_x.get)
y_step = max(l1_y,key = l1_y.get)


cost1 = 1
bef_x = b7[0][0]
bef_y = b7[0][1]
for i in range(1,n):
    diff_x = b7[i][0]-bef_x
    diff_y = b7[i][1]-bef_y
    bef_x = b7[i][0]
    bef_y = b7[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost1 += 1

costs.append(cost1)
#*****************************************************
l1_x = {}
l1_y = {}

bef_x = b8[0][0]
bef_y = b8[0][1]
for i in range(1, n):
    diff_x = b8[i][0] - bef_x
    diff_y = b8[i][1] - bef_y
    bef_x = b8[i][0]
    bef_y = b8[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x] += 1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y] += 1

x_step = max(l1_x, key = l1_x.get)
y_step = max(l1_y, key = l1_y.get)

cost2 = 1
bef_x = b8[0][0]
bef_y = b8[0][1]
for i in range(1, n):
    diff_x = b8[i][0] - bef_x
    diff_y = b8[i][1] - bef_y
    bef_x = b8[i][0]
    bef_y = b8[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost2 += 1

costs.append(cost2)

print(min(costs))

Buggy submission (verdict: Runtime Error):



def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g



n = int(input())
b1 = []

for i in range(n):
    b1.append(list(map(int,input().split())))

b2 = b1.copy()

b1.sort(key=lambda x:(x[0],x[1]))
b2.sort(key=lambda x:(x[1],x[0]))
# print(b1)
# print(b2)

l1_x = {}
l1_y = {}

bef_x = b1[0][0]
bef_y = b1[0][1]
for i in range(1,n):
    diff_x = b1[i][0]-bef_x
    diff_y = b1[i][1]-bef_y
    bef_x = b1[i][0]
    bef_y = b1[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x]+=1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y]+=1

x_step = max(l1_x,key = l1_x.get)
y_step = max(l1_y,key = l1_y.get)


cost1 = 1
bef_x = b1[0][0]
bef_y = b1[0][1]
for i in range(1,n):
    diff_x = b1[i][0]-bef_x
    diff_y = b1[i][1]-bef_y
    bef_x = b1[i][0]
    bef_y = b1[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost1 += 1


#*****************************************************
l1_x = {}
l1_y = {}

bef_x = b2[0][0]
bef_y = b2[0][1]
for i in range(1, n):
    diff_x = b2[i][0] - bef_x
    diff_y = b2[i][1] - bef_y
    bef_x = b2[i][0]
    bef_y = b2[i][1]

    if not diff_x in l1_x:
        l1_x[diff_x] = 1
    else:
        l1_x[diff_x] += 1

    if not diff_y in l1_y:
        l1_y[diff_y] = 1
    else:
        l1_y[diff_y] += 1

x_step = max(l1_x, key = l1_x.get)
y_step = max(l1_y, key = l1_y.get)

cost2 = 1
bef_x = b2[0][0]
bef_y = b2[0][1]
for i in range(1, n):
    diff_x = b2[i][0] - bef_x
    diff_y = b2[i][1] - bef_y
    bef_x = b2[i][0]
    bef_y = b2[i][1]

    if diff_x != x_step or diff_y != y_step:
        cost2 += 1

print(min(cost1,cost2))