Problem:


Score: 300 points

Problem Statement

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

City i is established in year Y_i and belongs to Prefecture P_i.

You can assume that there are no multiple cities that are established in the same year.

It is decided to allocate a 12-digit ID number to each city.

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.

Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq P_i \leq N

1 \leq Y_i \leq 10^9

Y_i are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
P_1 Y_1
:
P_M Y_M

Output

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).

Sample Input 1

2 3
1 32
2 63
1 12

Sample Output 1

000001000002
000002000001
000001000001

As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.

As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.

As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.

Sample Input 2

2 3
2 55
2 77
2 99

Sample Output 2

000002000001
000002000002
000002000003



Sample input:
2 3
1 32
2 63
1 12


Sample output:
000001000002
000002000001
000001000001

Submission status: Time Limit Exceeded

Solution code:
import sys
def solve(n,m,a):
    bb = {i+1:[] for i in range(n)}
    for [s1,s2] in a:
        bb[s1].append(s2)
    b = [i for i in bb.values()]
    for i in range(len(b)):
        b[i].sort()
    c,d = {s2:s1 for [s1,s2] in a},[s2 for [s1,s2] in a]
    ans = []
    for i in range(m):
        ken = str(c[d[i]]).zfill(6)
        ban = str(b[c[d[i]]-1].index(d[i])+1).zfill(6)
        ans.append(ken+ban)
    return '\n'.join(ans)
def readQuestion():
    ws = sys.stdin.readline().strip().split()
    N = int(ws[0])
    M = int(ws[1])
    ls_PY = [list(map(int, sys.stdin.readline().strip().split())) for j in range(M)]
    return (N, M, ls_PY)

def main():
    N, M, ls_PY = readQuestion()
    answer = solve(N, M, ls_PY)
    print(answer)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def solve(n,m,a):
    bb = {i+1:[] for i in range(n)}
    for [s1,s2] in a:
        bb[s1].append(s2)
    b = [i for i in bb.values()]
    for i in range(len(b)):
        b[i].sort()
    c = {s2:s1 for [s1,s2] in a}
    ans = ""
    zero = ["00000","0000","000","00","0",""]
    for i in range(m):
        ken = str(c[a[i][1]])
        ban = str(b[c[a[i][1]]-1].index(a[i][1])+1)
        ans += zero[len(ken)-1] + ken + zero[len(ban)-1] + ban + "\n"
    
    return ans 
def readQuestion():
    ws = sys.stdin.readline().strip().split()
    N = int(ws[0])
    M = int(ws[1])
    ls_PY = [list(map(int, sys.stdin.readline().strip().split())) for j in range(M)]
    return (N, M, ls_PY)

def main():
    N, M, ls_PY = readQuestion()
    answer = solve(N, M, ls_PY)
    print(answer)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def solve(n,m,a):
    bb = {i+1:[] for i in range(n)}
    for [s1,s2] in a:
        bb[s1].append(s2)
    b = [i for i in bb.values()]
    for i in range(len(b)):
        b[i].sort()
    c = {s2:s1 for [s1,s2] in a}
    d = [s2 for [s1,s2] in a]
    #print(" a=",a,"\n","b=",b,"\n","c=",c,"\n","d=",d)
    ans = ""
    zero = ["00000","0000","000","00","0",""]
    for i in range(m):
        ken = str(c[d[i]])
        ban = str(b[c[d[i]]-1].index(d[i])+1)
        ans += zero[len(ken)-1] + ken + zero[len(ban)-1] + ban + "\n"
    
    return ans 
def readQuestion():
    ws = sys.stdin.readline().strip().split()
    N = int(ws[0])
    M = int(ws[1])
    ls_PY = [list(map(int, sys.stdin.readline().strip().split())) for j in range(M)]
    return (N, M, ls_PY)

def main():
    N, M, ls_PY = readQuestion()
    answer = solve(N, M, ls_PY)
    print(answer)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def solve(n,m,a):
    bb = {i+1:[] for i in range(n)}
    for [s1,s2] in a:
        bb[s1].append(s2)
    b = [i for i in bb.values()]
    for i in range(len(b)):
        b[i].sort()
    c = {s2:s1 for [s1,s2] in a}
    d = [s2 for [s1,s2] in a]
    #print(" a=",a,"\n","b=",b,"\n","c=",c,"\n","d=",d)
    ans = ""
    zero = ["00000","0000","000","00","0",""]
    for i in range(m):
        ken = str(c[d[i]])
        ban = str(b[c[d[i]]-1].index(d[i])+1)
        ans += zero[len(ken)-1] + ken
        ans += zero[len(ban)-1] + ban + "\n"
    
    return ans 
def readQuestion():
    ws = sys.stdin.readline().strip().split()
    N = int(ws[0])
    M = int(ws[1])
    ls_PY = [list(map(int, sys.stdin.readline().strip().split())) for j in range(M)]
    return (N, M, ls_PY)

def main():
    N, M, ls_PY = readQuestion()
    answer = solve(N, M, ls_PY)
    print(answer)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def solve(n,m,a):
    bb = {i+1:[] for i in range(n)}
    for [s1,s2] in a:
        bb[s1].append(s2)
    b = [i for i in bb.values()]
    for i in range(len(b)):
        b[i].sort()
    c = {s2:s1 for [s1,s2] in a}
    d = [s2 for [s1,s2] in a]
    #print(" a=",a,"\n","b=",b,"\n","c=",c,"\n","d=",d)
    ans = ""
    for i in range(m):
        ken = str(c[d[i]])
        ban = str(b[c[d[i]]-1].index(d[i])+1)

        zero = ""
        for i in range(6-len(ken)):
            zero += "0"
        ans += zero
        ans += ken
        zero = ""
        for i in range(6-len(ban)):
            zero += "0"
        ans += zero
        ans += ban
        ans += "\n"
    
    return ans 
def readQuestion():
    ws = sys.stdin.readline().strip().split()
    N = int(ws[0])
    M = int(ws[1])
    ls_PY = [list(map(int, sys.stdin.readline().strip().split())) for j in range(M)]
    return (N, M, ls_PY)

def main():
    N, M, ls_PY = readQuestion()
    answer = solve(N, M, ls_PY)
    print(answer)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# import sys
# import collections
# import numpy as np

# N:県, M:市
N, M = list(map(int, input().split()))
# od = collections.OrderedDict()

L = []
A = {}
B = {}

for i in range(M):
  x = tuple(map(int, input().split()))
  L.append(x)
  k = x[0]
  if k in A.keys():
    A[k].append(x[1])
  else:
    A[k] = [x[1]]  

#print(A)
# A: {県番号: {市1の年:市1のindex, 市2の年:市2のindex}}
 
#c = collections.OrderedDict([('Kuroda', 18), ('Ichiroh', 51), ('Matsui', 55)])
#print(c['Kuroda'])
 
# 県番号でソート
for k in A.keys():
  # keyでソート
  B[k] = (sorted(A[k]))

#print(B)

for x in L:
  # print(B[x[0]])
  # print(x[1])
  num = (B[x[0]].index(x[1])) + 1
  print("{:0>6}{:0>6}".format(x[0], num))

Submission status: Time Limit Exceeded

Solution code:
def fun(x):
    if 0<=x<=9:
        return "00000"+"{}".format(x)
    elif x<=99:
        return "0000"+"{}".format(x)
    elif x<=999:
        return "000"+"{}".format(x)
    elif x<=9999:
        return "00"+"{}".format(x)
    elif x<=99999:
        return "0"+"{}".format(x)
    else:
        return "100000"
a,b=map(int,input().split(" "))
c=[tuple(map(int,input().split(" "))) for i in range(b)]
d=sorted(c)
e={}
f=0
for i in range(1,a+1):
    for j in range(b):
        if i==d[j][0]:
            f+=1
            e["{}".format(d[j])]="{}".format(fun(i)+fun(f))
    else:
        f=0
for i in c:
    if "{}".format(i) in e:
        print(e["{}".format(i)])

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N, M = map(int, input().split())
    d = defaultdict(list)

    py = []
    for i in range(M):
        p, y = map(int, input().split())
        py.append((p, y))
        d[p].append(y)

    for k in d.keys():
        d[k].sort()

    for p, y in py:
        x = d[p].index(y) + 1
        print(str(p).zfill(6) + str(x).zfill(6))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n,m=[int(i) for i in input().split()]
P=[[int(i) for i in input().split()] for j in range(m)]

def prefecturesort(i):
    return i[0]

def yearsort(i):
    return i[1]

W=[]
Z=[]

for i in range(m):
    P[i].append(i)
    W.append(P[i][0])

P.sort(key=prefecturesort)
P_c=[]
sum=0
for i in range(n):
    c=W.count(i+1)
    if c==0:
        P_c.append([])
    else:
        P_c.append(P[sum:c+sum])
    sum+=c
    P_c[i].sort(key=yearsort)
for x in P_c:
    for j in range(len(x)):
        X=str("{0:06d}".format(x[j][0]))
        Y=str("{0:06d}".format(j+1))
        Z.append([X+Y,x[j][2]])
Z.sort(key=yearsort)
for i in range(m):
    print(Z[i][0])

Submission status: Time Limit Exceeded

Solution code:
n,m=[int(i) for i in input().split()]
P=[[int(i) for i in input().split()] for j in range(m)]

def prefecturesort(i):
    return i[0]

def yearsort(i):
    return i[1]

W=[]
Z=[]

for i in range(m):
    P[i].append(i)
    W.append(P[i][0])

P.sort(key=prefecturesort)
P_c=[]
sum=0
for i in range(n):
    c=W.count(i+1)
    if c==0:
        P_c.append([])
    else:
        P_c.append(P[sum:c+sum])
    sum+=c
    P_c[i].sort(key=yearsort)
for x in P_c:
    for j in range(len(x)):
        X="{0:06d}".format(x[j][0])
        Y="{0:06d}".format(j+1)
        X=str(X)
        Y=str(Y)
        Z.append([X+Y,x[j][2]])
Z.sort(key=yearsort)
for i in range(m):
    print(Z[i][0])

Submission status: Time Limit Exceeded

Solution code:
n,m=[int(i) for i in input().split()]
P=[[int(i) for i in input().split()] for j in range(m)]

def prefecturesort(i):
    return i[0]

def yearsort(i):
    return i[1]

W=[]
Z=[]

for i in range(m):
    P[i].append(i)
    W.append(P[i][0])

P.sort(key=prefecturesort)
P_c=[]
sum=0
for i in range(n):
    c=W.count(i+1)
    if c==0:
        P_c.append([])
    else:
        P_c.append(P[sum:c+sum])
    sum+=c
for i in range(n):
    P_c[i].sort(key=yearsort)
for x in P_c:
    for j in range(len(x)):
        X="{0:06d}".format(x[j][0])
        Y="{0:06d}".format(j+1)
        X=str(X)
        Y=str(Y)
        Z.append([X+Y,x[j][2]])
Z.sort(key=yearsort)
for i in range(m):
    print(Z[i][0])

Submission status: Time Limit Exceeded

Solution code:
n,m=[int(i) for i in input().split()]
P=[[int(i) for i in input().split()] for j in range(m)]

def prefecturesort(i):
    return i[0]

def yearsort(i):
    return i[1]

W=[]
Z=[]

for i in range(m):
    P[i].append(i)

P.sort(key=prefecturesort)
P_c=[]
sum=0
for i in range(m):
    W.append(P[i][0])
for i in range(n):#県ごとに分ける→P_c
    c=W.count(i+1)
    if c==0:
        P_c.append([])
    else:
        P_c.append(P[sum:c+sum])
    sum+=c
for i in range(n):#P_cを県の中で出来た年の早い順にソート
    P_c[i].sort(key=yearsort)
for x in P_c:
    for j in range(len(x)):
        X="{0:06d}".format(x[j][0])
        Y="{0:06d}".format(j+1)
        X=str(X)
        Y=str(Y)
        Z.append([X+Y,x[j][2]])
Z.sort(key=yearsort)
for i in range(m):
    print(Z[i][0])

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
p = []
y = []
data = dict()
ans = dict()
for i in range(1,n+1):
    data[i] = []
for i in range(m):
    ptemp,ytemp = map(int,input().split())
    data[ptemp].append(ytemp)
    p.append(ptemp)
    y.append(ytemp)
    ans[ytemp] = []

for i in data:
    data[i].sort()

for i in data:
    for k in range(len(data[i])):
        moji = []
        for j in range(6-len(str(i))):
            moji.append("0")
        moji.append(str(i))
        for j in range(6-len(str(k+1))):
            moji.append("0")
        moji.append(str(k+1))
        ans[data[i][k]].append(moji)
for i in range(m):
    for k in range(len(ans[y[i]][0])):
        if k != len(ans[y[i]][0])-1:
            print(ans[y[i]][0][k],end="")
        else:
            print(ans[y[i]][0][k])

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
p = []
y = []
data = dict()
ans = dict()
for i in range(1,n+1):
    data[i] = []
for i in range(m):
    ptemp,ytemp = map(int,input().split())
    data[ptemp].append(ytemp)
    p.append(ptemp)
    y.append(ytemp)
    ans[ytemp] = []

for i in data:
    data[i].sort()

for i in data:
    for k in range(len(data[i])):
        moji = []
        for j in range(6-len(str(i))):
            moji.append("0")
        for j in range(len(list(str(i)))):
            moji.append(list(str(i))[j])
        for j in range(6-len(str(k+1))):
            moji.append("0")
        for j in range(len(list(str(k+1)))):
            moji.append(list(str(k+1))[j])
        ans[data[i][k]].append(moji)

for i in range(m):
    for k in range(12):
        if k != 11:
            print(ans[y[i]][0][k],end="")
        else:
            print(ans[y[i]][0][k])

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
p = []
y = []
data = dict()
ans = dict()
for i in range(1,n+1):
    data[i] = []
for i in range(m):
    ptemp,ytemp = map(int,input().split())
    data[ptemp].append(ytemp)
    p.append(ptemp)
    y.append(ytemp)
    ans[ytemp] = []

for i in data:
    data[i].sort()

for i in data:
    for k in range(len(data[i])):
        moji = []
        for j in range(6-len(str(i))):
            moji.append("0")
        for j in range(len(str(i))):
            moji.append(list(str(i))[j])
        for j in range(6-len(str(k+1))):
            moji.append("0")
        for j in range(len(str(k+1))):
            moji.append(list(str(k+1))[j])
        ans[data[i][k]].append(moji)

for i in range(m):
    for k in range(12):
        if k != 11:
            print(ans[y[i]][0][k],end="")
        else:
            print(ans[y[i]][0][k])

Submission status: Time Limit Exceeded

Solution code:
import math

def Show(n):
	s = str(n)
	m = len(s)
	for i in range(6 - m):
		print("0",end = "")
	print("{0}".format(n),end = "")

class city:
	year = 0
	p = 0
	x = 0
	number = 0
	def __init__(self,y,pi,n):
		self.year = y
		self.p = pi
		self.number = n

s = input().split()
n = int(s[0])
m = int(s[1])
cities = []
for i in range(m):
	s = input().split()
	p = int(s[0])
	y = int(s[1])
	cities.append(city(y,p,i))
x = [[] for i in range(n)]
for i in range(m):
	x[cities[i].p - 1].append(cities[i])
for i in range(n):
	x[i].sort(key = lambda x : x.year)
ans = []
for i in range(m):
	ans.append([0,0])
for i in range(n):
	for j in range(len(x[i])):
		x[i][j].x = j + 1
for i in range(n):
	for j in range(len(x[i])):
		ans[x[i][j].number][0] = x[i][j].p
		ans[x[i][j].number][1] = x[i][j].x
for i in range(m):
	Show(ans[i][0])
	Show(ans[i][1])
	print()

Submission status: Time Limit Exceeded

Solution code:
import math

def Show(n):
	s = str(n)
	m = len(s)
	for i in range(6 - m):
		print("0",end = "")
	print("{0}".format(n),end = "")

s = input().split()
n = int(s[0])
m = int(s[1])
p = []
y = []
for i in range(m):
	s = input().split()
	p.append(int(s[0]))
	y.append(int(s[1]))
x = [[] for i in range(n)]
for i in range(m):
	x[p[i] - 1].append(y[i])
for i in range(n):
	x[i].sort()
for i in range(m):
	Show(p[i])
	Show(x[p[i] - 1].index(y[i]) + 1)
	print()

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
pys = [list(map(int,input().split())) for i in range(m)]
result = []

for i,py in enumerate(pys):
    py.append(i)

for i in range(n+1):
    l = []
    for py in pys:
        if py[0] == i:
            l.append(py)
    l = sorted(l, key=lambda x:x[1])
    for j,ll in enumerate(l):
            ll.append(j)
    result += l

result = sorted(result, key=lambda x:x[2])

for i in result:
    s = "{:0>6}{:0>6}".format(i[0],i[3]+1)
    print(s)

Submission status: Time Limit Exceeded

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

N,M=[int(n) for n in input().split()]
lis=[[int(n) for n in input().split()] for i in range(M)]
l_2=sorted(lis, key=lambda x: x[1])
dic={}
for i,j in l_2:
    dic[i]=dic.get(i,0)+1
    lis[lis.index([i,j])]='{:0=6}{:0=6}'.format(i,dic[i])
for i in lis:print(i)

Submission status: Time Limit Exceeded

Solution code:
# import sys
# import collections
# import numpy as np

# N:県, M:市
N, M = list(map(int, input().split()))
# od = collections.OrderedDict()
L = [tuple(map(int, input().split())) for _ in range(M)]
A = {}
B = {}

for i in range(M):
  x = L[i]
  k = x[0]
  if k in A.keys():
    # print(A[k])
    # print(x)
    A[k].append(x[1])
  else:
    A[k] = [x[1]]

#print(A)
# A: {県番号: {市1の年:市1のindex, 市2の年:市2のindex}}

#c = collections.OrderedDict([('Kuroda', 18), ('Ichiroh', 51), ('Matsui', 55)])
#print(c['Kuroda'])

# 県番号でソート
for k in A.keys():
  # keyでソート
  B[k] = (sorted(A[k]))

#print(B)

for x in L:
  # print(B[x[0]])
  # print(x[1])
  num = (B[x[0]].index(x[1])) + 1
  print("{:0>6}{:0>6}".format(x[0], num))

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
import sys
imput = sys.stdin.readline

n,m = map(int,input().split())
dict1 = [[]]
dict2 = [[]]
for i in range(m):
    p,y = map(int,input().split())
    dict1.append([p,y])
del dict1[0]

dict2 = sorted(dict1,key=itemgetter(0,1))

count = 0
count2 = 0
for k,v in dict1:
    count2 += 1
    count = 0
    for k2,v2 in dict2:
    
        if k == k2:
            count += 1
            if v == v2:
                if count2 == len(dict1):
                    print(str(k).zfill(6)+str(count).zfill(6),end='')
                else:
                    print(str(k).zfill(6)+str(count).zfill(6))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
dict1 = [[]]
dict2 = [[]]
for i in range(m):
    p,y = map(int,input().split())
    dict1.append([p,y])
del dict1[0]

dict2 = sorted(dict1,key=lambda x:(x[0],x[1]))

count = 0
count2 = 0
for k,v in dict1:
    count2 += 1
    count = 0
    for k2,v2 in dict2:
    
        if k == k2:
            count += 1
            if v == v2:
                if count2 == len(dict1):
                    print(str(k).zfill(6)+str(count).zfill(6),end='')
                else:
                    print(str(k).zfill(6)+str(count).zfill(6))

Submission status: Time Limit Exceeded

Solution code:
from operator import attrgetter

class City:
    def __init__(self, idx, pref, year):
        self.idx = idx # city index
        self.pref = pref # pref index
        self.year = year    # year
        self.number = -1

def main():
    (N_pref, N_city) = map(int, input().split())
    # initialize cityList
    cityList = []
    for i in range(N_city):
        (pref, year) = map(int, input().split())
        city = City(i, pref, year)
        cityList.append(city)

    # make preflist
    preflist = []
    for i in range(N_pref+1):
        preflist.append([])
    # initialize preflist
    for city in cityList:
        preflist[city.pref].append(city)
    # sort preflist
    for i in range(len(preflist)):
        pref = preflist[i]
        preflist[i] = sorted(pref, key=attrgetter('year'))

    # calc city_number
    for i in range(N_city):
        city = cityList[i]
        city_idx = city.idx
        pref_number = 0
        pref_idx = city.pref
        pref = preflist[pref_idx]
        for j in range(len(pref)):
            if pref[j].idx == city_idx:
                pref_number = j + 1
                break
        number = "%06d%06d"%(pref_idx,pref_number)
        cityList[i].number = number
        print(number)



    # show pref
    #for i in range(len(preflist)):
    #    print("pref %d"%i)
    #    pref = preflist[i]
    #    for j in range(len(pref)):
    #        city = pref[j]
    #        print(city.idx)
    #    print()
main()

Submission status: Time Limit Exceeded

Solution code:
n,p = map(int, input().split())
lst = [list(map(int, input().split())) for i in range(p)]
dic = {i : sorted([j[1] for j in lst if j[0] == i]) for i in range(n+1)}
for k in range(p):
  x = str(lst[k][0]).zfill(6)
  y = str(dic[lst[k][0]].index(lst[k][1])+1).zfill(6)
  print(x + y)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
log = [ [ int(i) for i in input().split() ] for j in range(M) ]

pref = {}

for i in log:
    p = i[0]
    if p in pref:
        pref[p].append(i[1])
    else:
        pref[p] = [i[1]]

for i in pref:
    pref[i] = sorted(pref[i])

for i in log:
    num = pref[i[0]].index(i[1])
    print(str(i[0]).zfill(6) + str(num+1).zfill(6))

Submission status: Time Limit Exceeded

Solution code:
N, M = list(map(int, input().split()))
ken = [[] for _ in range(N)]

mlist = []
for i in range(M):
    a, b = list(map(int, input().split()))
    ken[a - 1].append(b)
    mlist.append((a, b,))

for v in ken:
    v.sort()

for a, b in mlist:
    print("{:06d}{:06d}".format(a, ken[a - 1].index(b) + 1))

Submission status: Time Limit Exceeded

Solution code:
n, m = list(map(int, input().split()))
tmps = [[] for _ in range(n)]
pys = []

for _ in range(m):
    p, y = list(map(int, input().split()))
    tmps[p-1].append(y)
    pys.append([p, y])

for tmp in tmps:
    tmp.sort()

for py in pys:
    p, y = py
    yindex = tmps[p-1].index(y)+1
    print('{:06}'.format(p) + '{:06}'.format(yindex))

Submission status: Time Limit Exceeded

Solution code:
n, m = list(map(int, input().split()))
tmps = [[] for i in range(n)]
pys = []

for i in range(0, m):
    p, y = list(map(int, input().split()))
    tmps[p-1].append(y)
    pys.append([p, y])

for tmp in tmps:
    tmp.sort()

for py in pys:
    p, y = py
    yindex = tmps[p-1].index(y)+1
    print('{:06}'.format(p) + '{:06}'.format(yindex))

Submission status: Time Limit Exceeded

Solution code:
n, m = list(map(int, input().split()))
tmps = [[] for i in range(n)]
pys = []

for i in range(0, m):
    p, y = list(map(int, input().split()))
    tmps[p-1].append(y)
    pys.append([p, y])

for i in range(n):
    tmps[i] = sorted(tmps[i])

for py in pys:
    p, y = py
    yindex = tmps[p-1].index(y)+1
    print('{:06}'.format(p) + '{:06}'.format(yindex))

Submission status: Time Limit Exceeded

Solution code:
n, m = list(map(int, input().split()))
tmps = [[] for i in range(n)]
pys = []

for i in range(0, m):
    p, y = list(map(int, input().split()))
    tmps[p-1].append(y)
    pys.append([p, y])

for i in range(n):
    tmps[i].sort()

for py in pys:
    p, y = py
    yindex = tmps[p-1].index(y)+1
    print('{:06}'.format(p) + '{:06}'.format(yindex))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())

l1 = [[] for i in range(n)]
l2 = []

for i in range(m):
    p,y = map(int,input().split())
    l1[p-1].append(y)
    l2.append((p,y))

for p in range(n):
    l1[p].sort()

for i in range(m):
    p = l2[i][0]
    k = l1[p-1].index(l2[i][1])+1
    p = str(p)
    k = str(k)
    ans = "0"*(6-len(p))+p+"0"*(6-len(k))+k
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
i=1
id_list=[]
y_list=[]
id_list2=[]
before=""
after=""
j=0
while j<n:
    id_list.append([0])
    j=j+1 
while i<=m:
    p,y=map(int,input().split())
    y_list.append((p,y))
    id_list[p-1].append(y)
    i+=1
for i in range(n):
    id_list[i].sort()
for item in y_list:
    before=str(item[0])
    while len(before)<6:
        before="0"+before
    after=str(id_list[item[0]-1].index(item[1]))
    
    while len(after)<6:
        after="0"+after
    id_list2.append(before+after)
for idx in id_list2:
    print(idx)

Submission status: Time Limit Exceeded

Solution code:
import copy

N,M = map(int,input().split())
L = [list(map(int,input().split())) for i in range(M)]
S = copy.deepcopy(L)
S.sort()
A = [[] for i in range(N)]
k = 0
j = 1
while (k < M):
  if (S[k][0] == j):
    A[j-1].append(S[k][1])
    k += 1
  else:
    j = S[k][0]
    A[j-1].append(S[k][1])
    k += 1
    
p = 0    
for i in range(M):
  if (L[i][1] in A[L[i][0]-1]):
    p = A[L[i][0]-1].index(L[i][1])+1
  print(str('0'*(6-len(str(L[i][0]))))+str(L[i][0])+str('0'*(6-len(str(p))))+str(p))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
d = dict()
p = [0]*m
y = [0]*m
for i in range(m):
  p[i], y[i] = map(int,input().split())
  if p[i] in d:
    d[p[i]] = d[p[i]] + [y[i]]
  else:
    d[p[i]] = [y[i]]
for x in d:
  d[x] = sorted(d[x])
  d[x] = dict([[d[x][j],j] for j in range(len(d[x]))])
for i in range(m):
  print("{0:06d}".format(p[i])+"{0:06d}".format(d[p[i]][y[i]]+1))

Submission status: Time Limit Exceeded

Solution code:
n,p = map(int, input().split())
lst = [list(map(int, input().split())) for i in range(p)]
pset = (i[0] for i in lst)
dic = {i : sorted([j[1] for j in lst if j[0] == i]) for i in pset}
for k in range(p):
  x = str(lst[k][0]).zfill(6)
  y = str(dic[lst[k][0]].index(lst[k][1])+1).zfill(6)
  print(x + y)

Submission status: Time Limit Exceeded

Solution code:
n, m = list(map(int,input().split()))
prefecture = {}
out = [[] for i in range(m)]
 
for i in range(m): # p=県 y=年
  p, y = list(map(int,input().split()))
  prefecture[str(i)] = [y, p]
sorted_prefecture2 = sorted(prefecture.items(), key=lambda x: x[1][0])
for i in range(n):
    if len(['x' for y in range(m) if sorted_prefecture2[y][1][1]==i+1])>=1:
        count = 0
        for y in range(m):
            if sorted_prefecture2[y][1][1]==i+1:
                count += 1
                out[int(sorted_prefecture2[y][0])] = ('{}{}'.format((6-len(str(sorted_prefecture2[y][1][1])))*'0'+str(sorted_prefecture2[y][1][1]),(6-len(str(count)))*'0'+str(count)))
for i in range(m):
    print(out[i])

Submission status: Time Limit Exceeded

Solution code:
N, M = [int(s) for s in input().rstrip().split()]
pref = [[] for _ in range(N)]
P = []
for m in range(M):
    p, year = [int(s) for s in input().rstrip().split()]
    pref[p-1].append([year, m])
    pref[p-1].sort()
    P.append(p)

id = [None] * M
for n, p in enumerate(pref):
    if not p:
        continue    
    p.sort()
    for x, (year, m) in enumerate(p):
        id[m] = '{0:06d}{1:06d}'.format(n+1, x+1)

print('\n'.join(id))

Submission status: Time Limit Exceeded

Solution code:
N = input().split()
P = []
for i in range(int(N[1])):
    P.append(list(map(int, input().split())))
#print(P)
K = sorted(P)
#print(K)
for i, n in enumerate(P):

#	print("K,n",K,n[1])
#	print(K.index(n))
	tmp = 0;
	for i in range(0,K.index(n)+1):
		#print("K[i]",K[i])
		if K[i][0] == n[0]:
			tmp+=1
	print("{:0=6}".format(int(n[0]))+"{:0=6}".format(tmp))
	#print(n[0])

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
A = [list(map(int,input().split())) for z in range(M)]
a = sorted(A)

MAP = [[0] for b in range(N + 1)]

for i in range(M):
    x, y = A[i]
    MAP[x].append(y)
    MAP[x].sort()

for j in range(M):
    ans = str(A[j][0]).zfill(6)
    ans += str(MAP[A[j][0]].index(A[j][1])).zfill(6)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def year(x):
    return Y[x]

N, M = [int(s) for s in input().rstrip().split()]
pref = [[] for _ in range(N)]
P = []
for m in range(M):
    p, year = [int(s) for s in input().rstrip().split()]
    pref[p-1].append([year, m])
    P.append(p)

id = []    
for n in range(N):
    if pref[n]:
        pref[n].sort()

for i, p in enumerate(P):
    for j, city in enumerate(pref[p-1]):
        if city[1] == i:
            x = j+1
    id.append('{0:06d}{1:06d}'.format(p, x))

for m in range(M):
    print(id[m])

Submission status: Time Limit Exceeded

Solution code:
n,p = map(int, input().split())
lst = []
mx = 0
for i in range(p):
  a = list(map(int, input().split()))
  lst.append(a)
  if a[1] > mx:
    mx = a[1]
dic = {}
for i in range(1,mx+1):
  lsti = []
  for j in lst:
    if j[0] == i:
      lsti.append(j[1])
  lsti.sort()
  dic[i] = lsti
for k in range(p):
  x = str(lst[k][0]).zfill(6)
  y = str(dic[lst[k][0]].index(lst[k][1])+1).zfill(6)
  print(x + y)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
d = [[]]*(n+1)
q = [[]]*(n+1)
p = [0]*m
y = [0]*m
for i in range(m):
  p[i], y[i] = map(int,input().split())
  d[p[i]] = d[p[i]] + [y[i]]
for i in range(n+1):
  d[i] = sorted(d[i])
  d[i] = dict([[d[i][j],j] for j in range(len(d[i]))])
for i in range(m):
  print("{0:06d}".format(p[i])+"{0:06d}".format(d[p[i]][y[i]]+1))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
data = [[int(i) for i in input().split()] for i in range(M)]
  
corresp = {}
for n in range(1, N+1):
    data_n = []
    for i in range(M):
        if data[i][0] == n:
            data_n.append(data[i])
    sort_n = sorted(data_n, key=lambda data_n_i: data_n_i[1])
    for k in range(len(sort_n)):
        corresp.update({str(sort_n[k]):k+1})
    

for i in range(M):
    print('{0:012d}'.format(data[i][0]*1000000+corresp[str(data[i])]))

Submission status: Time Limit Exceeded

Solution code:
nm=list(map(int,input().split()))
n=nm[0]
m=nm[1]
py=[]
y=[]
for i in range(m):
    py.append([])
for i in range (n):
    y.append([])
for i in range(m):
    py[i]=list(map(int,input().split()))
    y[py[i][0]-1].append(py[i][1])
for i in range(n):
    y[i].sort()
#print(py)
#print(y)
for i in range(m):
    if py[i][0]<10:
        print("00000"+str(py[i][0]*10**6+y[py[i][0]-1].index(py[i][1])+1))
    elif  py[i][0]<100:
        print("0000"+str(py[i][0]*10**6+y[py[i][0]-1].index(py[i][1])+1))
    elif py[i][0]<1000:
        print("000"+str(py[i][0]*10**6+y[py[i][0]-1].index(py[i][1])+1))
    elif py[i][0]<10000:
        print("00"+str(py[i][0]*10**6+y[py[i][0]-1].index(py[i][1])+1))
    elif py[i][0]<100000:
        print("0"+str(py[i][0]*10**6+y[py[i][0]-1].index(py[i][1])+1))
    else:
        print(str(py[i][0]*10**6+y[py[i][0]-1].index(py[i][1])+1))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque


def main():
    n, m = map(int, input().split())
    lines = sys.stdin.readlines()

    cities = {}
    mem_city = deque()
    for i in range(m):
        p, y = map(int, lines[i].split())
        mem_city.append((p, y))
        if p in cities:
            cities[p].append(y)
        else:
            cities[p] = [y]
    # print(cities)
    for key in cities:
        cities[key].sort()
    # print(cities)

    outputs = [None] * m
    for iter, p in enumerate(mem_city):
        idx = cities[p[0]].index(p[1]) + 1
        outputs[iter] = "{:06d}{:06d}".format(p[0], idx)

    for output in outputs:
        print(output)
    return


main()

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
PY = []
array1 = []
array2 = []

for _ in range(M):
    P, Y = map(int, input().split())
    PY.append([P, Y])
    if P in array1:
        array2[array1.index(P)].append(Y)
    else:
        array1.append(P)
        array2.append([Y])
        
for i in range(len(array2)):
    array2[i].sort()

for i in PY:
    print('{:06}'.format(i[0]), end='')
    print('{:06}'.format(array2[array1.index(i[0])].index(i[1])+1))