Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.6 kB
1
Indexable
Never
import sys
import time

def list_input():
    start = time.time()
    N = int(input())
    num = []
    for line in sys.stdin:
        num.extend(int(s) for s in line.split())
    end = time.time()
    print('Intag', end-start)
    return num, N


def list_sorter(num, N):
    start1 = time.time()
    M = [None]*N
    W = [None]*N
    for i in range(0, 2*N*(N+1),  N+1):
        if W[num[i]-1] is None: 
            W[num[i]-1] = num[i:i+N+1]
        else:
            M[num[i]-1] = num[i:i+N+1]
    end1 = time.time()
    print('Sortering', end1-start1)
    return W, M

def sortW(W):
    N = len(W)
    V = [None]*N
    temp = [0]*(N*(N+1))
    for i in range(0,N):
        temp[i*(N+1)] = i+1
        for j in range(1,N+1):
            t = W[i][j]
            temp[i*(N+1)+t] = j
        V[i] = temp[i*(N+1):(i+1)*(N+1)]
    return V

def GS(W, M):
    p = []
    t = [None]*len(W)
    start3 = time.time()
    W = sortW(W)
    end3 = time.time()
    print('SortW', end3-start3)

    start2 = time.time()
    for men in M:
        p.append([men[0],1])
    while len(p) > 0:
        m = p.pop(0)
        w = M[m[0]-1][m[1]]
        m[1] = m[1]+1
        if t[w-1] is None:
            t[w-1] = m
        elif(W[w-1][t[w-1][0]]-W[w-1][m[0]] > 0):
            p.append(t[w-1])
            t[w-1] = m
        else:
            p.append(m)
    end2 = time.time()
    print('GS', end2-start2)
    #for i in range(0,len(t)):
        #print(t[i][0])


num, N = list_input()
W, M = list_sorter(num, N)
#print(W)
#print(M)
GS(W, M)