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

Solution (Python):
N,M = map(int,input().split())
arr = []
for i in range(M):
  pi,yi = map(int,input().split())
  arr.append((pi,yi))
sorted_arr = sorted(arr, key=lambda x:x[1])
numof_i_in_p = [1]*N
id_map = dict()
for factor in sorted_arr:
  id = "{:0>6}{:0>6}".format(factor[0], numof_i_in_p[factor[0]-1])
  numof_i_in_p[factor[0]-1] += 1
  id_map[factor]=id
for factor in arr:
  print(id_map[factor])

Solution (Python):
from heapq import heappush, heappop

class City(object):
    def __init__(self, i, year):
        self.i = i
        self.year = year
        self.id = None

    def __lt__(self, other):
        return self.year < other.year

    def __eq__(self, other):
        return self.year == other.year

    def __gt__(self, other):
        return self.year > other.year


class Solver(object):
    def __init__(self):
        self.n, self.m = list(map(int, input().split(" ")))
        self.prefectures = [[] for _ in range(self.n)]
        self.cities = []
        for i in range(self.m):
            p, y = list(map(int, input().split(" ")))
            city = City(i+1, y)
            self.cities.append(city)
            heappush(self.prefectures[p-1], city)

    def solve(self):
        for i in range(self.n):  
            for j in range(len(self.prefectures[i])):  
                city = heappop(self.prefectures[i])
                city.id = "{:06d}{:06d}".format(i+1, j+1)
        for city in self.cities:
            print(city.id)


if __name__ == "__main__":
    s = Solver()
    s.solve()

Solution (Python):
import sys

def main():
  input = sys.stdin.readline
  N, M = map(int, input().split())
  C = [0] * M  
  P = [0] * (N+1)
  for m in range(M):
    p, y = map(int, input().split())
    C[m] = [m, p, y, 0]
  C = sorted(C, key=lambda x: x[2])

  for c in C:
    P[c[1]] += 1
    c[3] = P[c[1]]
  C = sorted(C, key=lambda x: x[0])

  for c in C:
    print('{:06d}{:06d}'.format(c[1], c[3]))


if __name__ == '__main__':
  main()

Solution (Python):
n, m = map(int, input().split())
cities = [[i] + list(map(int, input().split())) for i in range(m)]
cities = sorted(cities, key=lambda x: (x[1], x[2]))

num = 1
pref = 0
for city in cities:
    if pref != city[1]:
        pref = city[1]
        num = 1
    city = city.append(num)
    num += 1

cities = sorted(cities, key=lambda x: x[0])

for city in cities:
    print(str(city[1]).zfill(6) + str(city[3]).zfill(6))

Solution (Python):


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

D.sort(key=lambda x:x[1])
D.sort(key=lambda x:x[0])

ID = [0] * M
tp, id = 1, 0
for i in range(M):
    p, y, ix = D[i]
    if tp == p:
        id += 1
    else:
        tp, id = p, 1
    ID[ix] = (p, id)

for i in range(M):
    print("{:0>6}{:0>6}".format(ID[i][0], ID[i][1]))

Solution (Python):
import heapq


def pad(p):
    n = 1
    q = p
    while True:
        q = q // 10
        if q == 0:
            break
        n += 1
    return "0" * (6 - n) + str(p)


n, m = list(map(int, input().split(' ')))
ps = []
ys = []
year_ids = {}
heaps = {}
for i in range(m):
    p, y = list(map(int, input().split(' ')))
    ps.append(p)
    ys.append(y)
    if p not in heaps:
        heaps[p] = []
        year_ids[p] = {}
    heapq.heappush(heaps[p], y)



for p in heaps:
    yid = 1
    while True:
        if len(heaps[p]) == 0:
            break
        y = heapq.heappop(heaps[p])
        year_ids[p][y] = yid
        yid += 1


for i in range(m):
    p = ps[i]
    y = ys[i]
    x = year_ids[p][y]
    print(pad(p) + pad(x))

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

X=[]

for i in range(m):
    a=i
    b,c=map(int,input().split())
    X.append([a,b,c])

sort_X=sorted(X,key=lambda x:(x[1],x[2]))
sort_X[0].append(1)

for i in range(1,m):
    if sort_X[i][1]!=sort_X[i-1][1]:
        sort_X[i].append(1)
    else:
        sort_X[i].append(sort_X[i-1][3]+1)
   
X=sorted(sort_X,key=lambda x:x[0])
for i in range(m):
    count=X[i][3]
    first_num="000000"
    last_num="000000"
    
    
    first_num=first_num[:6-len(str(X[i][1]))]+str(X[i][1])
    last_num=last_num[:6-len(str(count))]+str(count)
    print(first_num+last_num)

Solution (Python):
N,M = map(int, input().split())
py = [(0,0,0)]*M
for i in range(M):
    p,y = map(int,input().split())
    py[i] = (p,y,i)

py.sort(key=lambda x:(x[0], x[1]))

ans = [(0,0)]*M

year = 1
tmp_city = 0

for index, i in enumerate(py):
    city_str = "{}".format(i[0]).zfill(6)
    if tmp_city != i[0]:
        year = 1
    year_str = "{}".format(year).zfill(6)
    tmp_city = i[0]
    year += 1
    
    ans_str = city_str + year_str
    
    ans[index] = (ans_str,i[2])

ans.sort(key=lambda x:x[1])
for i in ans:
    print(i[0])

Solution (Python):
N, M = map(int, input().split())
PY = []
for i in range(M):
    PY.append(list(map(int, input().split())))
P_dict = {}
res_dict = {}
for i in range(1, N+1):
    P_dict[i] = []
    res_dict[i] = []
for n, i in enumerate(PY):
    P_dict[i[0]].append((i[1], n))
res = []
for i in range(1, N+1):
    P_dict[i] = sorted(P_dict[i], key=lambda x:x[0])
    tmp_P_num = '{}'.format(i).zfill(6)
    res += [(tmp_P_num + '{}'.format(nn+1).zfill(6), n[1]) for nn, n in enumerate(P_dict[i])]
res = sorted(res, key=lambda x:x[1])
for i in res:
    print(i[0])

Solution (Python):



def main():
  n, m = map(int, input().split())
  ipy = [(i+1, )+(tuple(map(int, input().split()))) for i in range(m)]
  ipy.sort(key=lambda x:x[2])
  kens = set([p for i,p,y in ipy])
  di = {ken:0 for ken in kens}

  ans = []
  for i,p,y in ipy:
    di[p] += 1
    number = ('%06d%06d' % (p, di[p]))
    ans += [(i, number)]
  ans.sort(key=lambda x:x[0])

  for _, number in ans:
    print(number)
  pass

if __name__=='__main__':
  main()

Solution (Python):
N, M = map(int, input().split())
dic = {}
raw = []
dic[1] = []
for i in range(M):
  P, Y = map(int, input().split())
  raw.append([P, Y, i])
raw.sort()
for i in range(M):
  if i == 0:
    raw[i][1] = 1
    p, y, ind = raw[i]
    continue
  pp, yy, iind = raw[i]
  if pp == p:
    raw[i][1] = y + 1
  else:
    raw[i][1] = 1
  p, y, ind = raw[i]
raw.sort(key = lambda x:x[2])
for r in raw:
    p, y, ind = r
    P = "{0:06d}".format(p)
    Y = "{0:06d}".format(y)
    print(P+Y)

Solution (Python):
import numpy as np
import math
from collections import defaultdict

n,m = input().split()
n = int(n)
m = int(m)
P,Y = [],[]
for _ in range(m):
	p,y = input().split()
	P.append(p)
	Y.append(y)

P = [int(p) for p in P]
Y = [int(y) for y in Y]

sorted = defaultdict(list)

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

for i,(p,y) in enumerate(zip(P,Y)):
	sorted[p-1].append((y,i))

ans = [None]*m
for i, d in enumerate(sorted):
	d.sort()
	for k, (y, j) in enumerate(d):
		ans[j] = "%06d%06d" % (i+1,k+1)
print(*ans, sep='\n')

Solution (Python):
N, M = map(int, input().split())
D = [[] for i in range(N)]
for i in range(M):
    p, y = map(int, input().split())
    D[p-1].append((y, i))
ans = [None]*M
for i, d in enumerate(D):
    d.sort()
    for k, (y, j) in enumerate(d):
        ans[j] = "%06d%06d" % (i+1,k+1)
print(*ans, sep='\n')

Solution (Python):






















def inpl(): return list(map(int, input().split()))

N, M = inpl()
A = [""]*M
P = [[] for _ in range(N+1)]
for i in range(M):
  p, y = inpl()
  P[p].append((y, i))


for ind, p in enumerate(P):
  p = sorted(p, key=lambda x: x[0])
  for j, (y, i) in enumerate(p):
    A[i] = "{}{}".format(str(ind).zfill(6), str(j+1).zfill(6))

print("\n".join(A))

Solution (Python):
n,m=map(int,input().split())
city=[]
for i in range(m):
    a=i
    b,c=map(int,input().split())
    city.append([a,b,c])
city=sorted(city,key=lambda x:(x[1],x[2]))
judge=1
count=1
for i in range(m):
    if city[i][1]==judge:
        city[i].append(count)
        count+=1
    else:
        city[i].append(1)
        judge=city[i][1]
        count=2
for i in range(m):
    city[i].append(str(city[i][1]).zfill(6)+str(city[i][3]).zfill(6))
city=sorted(city,key=lambda x:x[0])
for i in range(m):
    print(city[i][4])

Solution (Python):
n,m=map(int,input().split())
city=[]
for i in range(m):
    a=i
    b,c=map(int,input().split())
    city.append([a,b,c])
city=sorted(city,key=lambda x:(x[1],x[2]))
city[0].append(1)
for i in range(1,m):
    if city[i][1]!=city[i-1][1]:
        city[i].append(1)
    else:
        city[i].append(city[i-1][3]+1)
for i in range(m):
    city[i].append(str(city[i][1]).zfill(6)+str(city[i][3]).zfill(6))
city=sorted(city,key=lambda x:(x[0]))
for i in range(m):
    print(city[i][4])

Solution (Python):
N, M = map(int,input().split())
PY = [list(map(int,input().split())) + [0] for _ in range(M)]
sort_PY = sorted(PY, key = lambda x: x[1])
num = {}
for py in sort_PY:
  if(py[0] not in num):
    num[py[0]] = 1
  else:
    num[py[0]] += 1
  py[2] = num[py[0]]
for py in PY:
  print(str(py[0]).zfill(6) + str(py[2]).zfill(6))

Solution (Python):
n,m=map(int, input().split())
l = [[int(i) for i in input().split()] for i in range(m)]

for i in range(m):
    l[i].append(i)
l = sorted(l,key=lambda x: (x[0],x[1]))

l[0].append(1)
for i in range(1,m):
    if l[i][0]!=l[i-1][0]:
        l[i].append(1)
    else:
        l[i].append(l[i-1][3]+1)

for i in range(m):
    l[i].append(str(l[i][0]).zfill(6) + str(l[i][3]).zfill(6))

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

for i in range(m):
    print(l[i][4])

Solution (Python):
a, b = map(int, input().split())
l = [[int(i) for i in input().split()] for i in range(b)] 

for i in range(b):
    l[i].append(i)

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

l[0].append(1)
for i in range(1,b):
    if l[i][0] != l[i-1][0]:
        l[i].append(1)
    else:
        l[i].append(l[i-1][3] + 1)

for i in range(b):
    l[i].append(str(l[i][0]).zfill(6) + str(l[i][3]).zfill(6))

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

for i in range(b):
    print(l[i][4])

Solution (Python):
N, M = map(int,input().split())
PY = [list(map(int,input().split())) + [0] for _ in range(M)]
sort_PY = sorted(PY, key = lambda x: x[1])
d = {}
for py in sort_PY:
  if(py[0] not in d):
    d[py[0]] = 1
  else:
    d[py[0]] += 1
  py[2] = d[py[0]]
for py in PY:
  print(str(py[0]).zfill(6) + str(py[2]).zfill(6))

Solution (Python):
N,M=map(int,input().split())
city=[]
for i in range(M):
    p,y=map(int,input().split())
    city.append([i,p,y])

city.sort(key = lambda t:t[2])
city.sort(key = lambda t:t[1])

id=1
old_city=0
for city_id in city:
    if city_id[1]!=old_city:
        id=1
    city_id[2]=id
    id+=1
    old_city=city_id[1]

city.sort(key = lambda t:t[0])

for x in city:
    ans=x[1]*1000000+x[2]
    print("{0:012d}".format(ans))

Solution (Python):
import numpy as np
N, M = map(int, input().split())
data = np.zeros((4, M))
 
for i in range(M):
    data[0, i] = i+1
    data[1:3, i] = list(map(int, input().split()))
 
city = [0 for i in range(N)]
data = data[:, np.argsort(data[2, :])]
for i in range(M):
    city[int(data[1, i]-1)] += 1
    data[3, i] = city[int(data[1, i]-1)]
 
data = data[:, np.argsort(data[0, :])]
for i in range(M):
    tmp = str(int(data[1, i])).zfill(6) + str(int(data[3, i])).zfill(6)
    print(tmp)

Solution (Python):


N, M = map(int, input().split())

def gen_id(n):
    id = ""
    hokan = 6 - len(str(n))
    for _ in range(hokan):
        id += "0"
    id += str(n)
    return id

DIC = dict()
CITY = []

for pi in range(1, N+1):
    DIC[pi] = []

for i in range(M):
    pi, yi = map(int, input().split())
    DIC[pi].append({ "i":i, "p":pi, "y":yi})


for pi in range(1, N+1):
    DIC[pi] = sorted(DIC[pi], key=lambda x: x["y"])


for pi in range(1, N+1):
    for index, city in enumerate(DIC[pi]):
        id = gen_id(city["p"]) + gen_id(index+1)
        city["id"] = id
        CITY.append(city)


x =sorted(CITY, key=lambda x: x["i"])

for i in x:
    print(i["id"])

Solution (Python):
N,M = map(int,raw_input().split())
data = []





for i in range(M):
    A,B = map(int,raw_input().split())
    data.append([A,B,i])

data.sort()
P = data[0][0]
counter = 0

for i in range(len(data)):
    if P != data[i][0]:
        P = data[i][0]
        counter = 0
    data[i] = [data[i][2],P,counter+1]
    counter += 1

data.sort()
for i in data:
    print format(i[1],'06')+format(i[2],'06')

Solution (Python):
class struct:
  def __init__(self,i,a,b,c):
    self.i=i
    self.a=a
    self.b=b
    self.c=c
n,m=map(int,input().split())
l=[]
l2=[]
for i in range(n):
  l2.append(1)
for i in range(m):
  a,b=map(int,input().split())
  l.append(struct(i,a,b,0))
l=sorted(l,key=lambda x:(x.a,x.b))
for i in range(m):
  a=l[i].a
  l[i].c=l2[a-1]
  l2[a-1]+=1
l=sorted(l,key=lambda x:x.i)
for i in l:
  print(str(i.a).zfill(6)+str(i.c).zfill(6))

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

s = sorted(a,key=lambda x:(x[0],x[1]))
f=s[0][0]
d=1
for i in range(m):
    if s[i][0] == f:
        s[i][1]=d
        d+=1
    else:
        f=s[i][0]
        d=1
        s[i][1]=d
        d+=1
for i in range(m):
    print("{:06}".format(a[i][0]) +"{:06}".format(a[i][1]))

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

x=[]
for i in range(m):
    p,y = input().split()
    x.append({"i":i, "p":p, "y":int(y)})
cnt = {}
x=sorted(x, key=lambda z: z["y"])
for city in x:
    c = cnt.get(city["p"], 0)
    cnt[city["p"]] = c+1
    city["n"] = city["p"].zfill(6) + str(c+1).zfill(6)

x = sorted(x, key=lambda z: z["i"])
for city in x:
    print(city["n"])

Solution (Python):
import bisect
n,m=(int(i) for i in input().split())
a=[[] for _ in range(n)]
s=[]

for _ in range(m):
    x,c=(int(i) for i in input().split())
    a[x-1].append(c)
    s.append((x,c))

[a[i].sort() for i in range(n)]

for x,c in s:
    print("{:06}{:06}".format(x, bisect.bisect_left(a[x-1],c)+1))

Solution (Python):
from operator import itemgetter
N,M = [int(x) for x in input().split()]
P = [0]*M
Y = [0]*M
pref = [[] for _ in range(N)]
city = [0]*M
for i in range(M):
    P[i],Y[i] = [int(x) for x in input().split()]
    pref[P[i]-1].append([i,Y[i]])
for index,i in enumerate(pref):
    pref[index].sort(key=itemgetter(1))
for index,i in enumerate(pref):
    for index2,j in enumerate(i):
        city[j[0]]='{:0=6}'.format(index+1)+'{:0=6}'.format(index2+1)
for i in city:
    print(i)

Solution (Python):
import numpy as np
N, M = map(int, input().split())
data = np.zeros((4, M))

for i in range(M):
    data[0, i] = i+1
    data[1:3, i] = list(map(int, input().split()))

city = [0 for i in range(N)]
data = data[:, np.argsort(data[2, :])]
for i in range(M):
    city[int(data[1, i]-1)] += 1
    data[3, i] = city[int(data[1, i]-1)]

data = data[:, np.argsort(data[0, :])]
for i in range(M):
    tmp = str(int(data[1, i])).zfill(6) + str(int(data[3, i])).zfill(6)
    print(tmp)

Solution (Python):
N,M = map(int,input().split())
info = [list(map(int,input().split())) for i in range(M)]
info_new = sorted(info,key=lambda x:(x[0],x[1]))
note = info_new[0][0]
cnt = 1
for i in range(M):
    if info_new[i][0] == note:
        info_new[i][1] = cnt
        cnt += 1
    else:
        note = info_new[i][0]
        cnt = 1
        info_new[i][1] = cnt
        cnt += 1
        
for i in range(M):
    pri = str(info[i][0])
    bir = str(info[i][1])
    number = "0"*(6-len(pri)) + pri + "0"*(6-len(bir)) + bir
    print(number)

Solution (Python):
N,M = map(int,input().split())
info = [list(map(int,input().split())) for i in range(M)]
info_new = sorted(info,key=lambda x:(x[0],x[1]))
note = info_new[0][0]
cnt = 1
for i in range(M):
    if info_new[i][0] == note:
        info_new[i][1] = cnt
        cnt += 1
    else:
        note = info_new[i][0]
        cnt = 1
        info_new[i][1] = cnt
        cnt += 1
        
for i in range(M):
    pri = str(info[i][0])
    bir = str(info[i][1])
    number = "0"*(6-len(pri)) + pri + "0"*(6-len(bir)) + bir
    print(number)

Solution (Python):
nm_lst = list(map(int, input().split()))
city_lst = [list(map(int, (str(i)+' '+input()).split())) for i in range(nm_lst[1])]

city_lst = sorted(city_lst, key = lambda x: x[1]*(10**12) + x[2])

ans_lst = []
num_num = 0
for n, city in enumerate(city_lst):
    prefec_num = city[1]
    prefec_str = '0' * (6-len(str(prefec_num))) + str(prefec_num)
    if n == 0 or (prefec_num != city_lst[n-1][1]):
        num_num = 1
    else:
        num_num += 1
    num_str = '0' * (6-len(str(num_num))) + str(num_num)
    ans_lst.append([city[0], prefec_str, num_str])
    
ans_lst = sorted(ans_lst, key = lambda x:x[0])

for city in ans_lst:
    print(city[1]+city[2])

Solution (Python):
import bisect
n, m = (int(i) for i in input().split())
yl = [[] for _ in range(n)]
dl = []

for _ in range(m):
    p, y = (int(i) for i in input().split())
    yl[p-1].append(y)
    dl.append((p, y))

[yl[i].sort() for i in range(n)]

for p, y in dl:
    print("{:0>6}{:0>6}".format(p, bisect.bisect_left(yl[p-1], y)+1))

Solution (Python):
import bisect
n, m = (int(i) for i in input().split())
yl = []
[yl.append([]) for _ in range(n)]
dl = []

for _ in range(m):
    p, y = (int(i) for i in input().split())
    yl[p-1].append(y)
    dl.append((p, y))

[yl[i].sort() for i in range(n)]

for p, y in dl:
    print("{:0>6}{:0>6}".format(p, bisect.bisect_left(yl[p-1], y)+1))

Solution (Python):
import numpy as np
n, m = map(int,input().split())
l = [list(map(int,input().split()))+ [0] for i in range(m)]
l2 = sorted(l)
p = 0
for i in l2:
    x = 1 if i[0] != p else x + 1
    p = i[0]
    i[2] = x
for i in l:
    print(str(i[0]).zfill(6)+str(i[2]).zfill(6))

Solution (Python):


def main():
    [N, M] = list(map(int, input().split()))
    c_list = []
    for i in range(M):
        [p, y] = list(map(int, input().split()))
        c_list.append((i, p, y))

    c_list.sort(key = lambda t: t[2])

    ct = [0] * (N + 1)
    id_list = []
    for i in range(M):
        j, p, y = c_list[i]
        ct[p] += 1
        w = str(1000000 + p)[1:] + str(1000000 + ct[p])[1:]
        id_list.append((j, w))

    id_list.sort(key = lambda t: t[0])

    for i in range(M):
        print(id_list[i][1])



if __name__ == '__main__':
    main()

Solution (Python):
n,m = map(int, input().split())
a = [[] for _ in range(n)]
count = 1
for i in range(m):
    p,y = map(int, input().split())
    a[p-1].append([y, count])
    count += 1
b = []
for i,j in enumerate(a):
    j_sort = sorted(j)
    for k,l in enumerate(j_sort):
        b.append([l[1],str(i+1).zfill(6)+str(k+1).zfill(6)])
for i in sorted(b):
    print(i[1])

Solution (Python):
from collections import defaultdict
n, m = map(int, input().split())

a = [list(map(int, input().split())) +[i, None] for i in range(m)]
a.sort(key=lambda x: x[1])
d = defaultdict(int)
for i in range(m):
    p,y = a[i][0], a[i][1]
    d[p] += 1
    code = "{0:06d}{1:06d}".format(p, d[p])
    a[i][3] = code
a.sort(key=lambda x: x[2])
for i in a:
    print(i[3])

Solution (Python):
import sys
input = sys.stdin.readline
 
N, M = (int(i) for i in input().split())
py_list = [[int(i) for i in input().split()] + [0] for i in range(M)] 
m_list = [[] for j in range(N+1)]
 
sorted_py_list = sorted(py_list, key=lambda x:x[1])

city_list = {}
for py in sorted_py_list:
    city_list[py[0]] = city_list[py[0]]+1 if py[0] in city_list else 1
    py[2] = city_list[py[0]]
 
for py in py_list:
    print(str(py[0]).zfill(6) + str(py[2]).zfill(6))

Solution (Python):
N, M = map(int,input().split())
PY = [list(map(int,input().split())) + [0] for i in range(M)]
sort_PY = sorted(PY, key = lambda x: x[1])
city = {}
for py in sort_PY:
  if(py[0] not in city):
    city[py[0]] = 1
  else:
    city[py[0]] += 1
  py[2] = city[py[0]]
for py in PY:
  print(str(py[0]).zfill(6) + str(py[2]).zfill(6))

Solution (Python):
N, M = map(int, input().split())
P, Y = [], []
ydics = [{} for _ in range(N)]
for i in range(M):
    p, y = map(int, input().split())
    ydics[p-1][i] = (p, y)

odic = {}
for ydic in ydics:
    for o, (i, (p, y)) in enumerate(sorted(ydic.items(), key=lambda x: x[1][1])):
        odic[i] = (p, o+1)
for i, (p, o) in sorted(odic.items()):
    print(str(p).zfill(6) + str(o).zfill(6))

Solution (Python):
import numpy as np
from operator import itemgetter

N, M = input().split()
N, M = int(N), int(M)
C = [input().split() for i in range(M)]

for i, c in enumerate(C):
  C[i] = [int(c[0]), int(c[1])]
  C[i].append(i)

C_sorted = sorted(C, key = lambda x: (x[0], x[1]))

counter = 1
current = 1

for c in C_sorted:
  if current < c[0]:
    counter = 1
  pref = str(c[0])
  num = str(counter)
  number = ''
  for i in range(6 - len(pref)):
    number += '0'
  number += pref
  for i in range(6 - len(num)):
    number += '0'
  number += num

  c.append(number)
  counter += 1
  current = c[0]

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

for c in C_reversed:
  print(c[3])

Solution (Python):
n, m = map(int, input().split())
d = [list() for i in range(n+1)] 
out = []
for i in range(m):
    pi, yi = map(int, input().split())
    d[pi].append([yi, i])
    out.append(-1)

for i in range(len(d)):
    d[i].sort()

for i in range(1, n+1):
    for j in range(len(d[i])):
        out[d[i][j][1]] = str(i).zfill(6) + str(j+1).zfill(6)

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

Solution (Python):
from collections import defaultdict

N, M = map(int, input().split())
yp = []
d = defaultdict(lambda: [])
ans = ['' for _ in range(M)]
for i in range(M):
    p, y = input().split()
    d[int(p)].append([i, int(y)])


for key in d.keys():
    d[key].sort(key = lambda x : x[1])
    num = 1
    for i in range(len(d[key])):
        ans[d[key][i][0]] += '{:0=6}'.format(key) \
                + '{:0=6}'.format(num) 
        
        num += 1

for i in range(M):
    print(ans[i])

Solution (Python):
n, m = map(int, input().split())
index = 0
listForL = []
listForL2 = []
while index < m:
    p, y = map(int, input().split())
    listForL.append([p, y])
    listForL2.append([p, y, index])
    index+=1

listForL2.sort()
listForAns=[0]*m
index2 =0
count = 0
before=0
while index2 < len(listForL2):
    if before!=listForL2[index2][0]:
        count=1
        before = listForL2[index2][0]
    else:
        count+=1
    mojiR=str(listForL2[index2][0])
    mojiL=str(count)
    index3=0
    if len(mojiR)==1:
        mojiR="00000"+mojiR
    elif len(mojiR)==2:
        mojiR = "0000"+mojiR
    elif len(mojiR)==3:
        mojiR = "000"+mojiR
    elif len(mojiR)==4:
        mojiR = "00"+mojiR
    elif len(mojiR)==5:
        mojiR = "0"+mojiR
    if len(mojiL) == 1:
        mojiL = "00000"+mojiL
    elif len(mojiL) == 2:
        mojiL = "0000"+mojiL
    elif len(mojiL) == 3:
        mojiL = "000"+mojiL
    elif len(mojiL) == 4:
        mojiL = "00"+mojiL
    elif len(mojiL) == 5:
        mojiL = "0"+mojiL
    listForAns[listForL2[index2][2]]=mojiR+mojiL
    index2+=1

for ans in listForAns:
    print(ans)

Solution (Python):
n, m = map(int, input().split())
ipy = []

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

counter = {}
order = {}

for i, p, y in sorted(ipy, key = lambda x: x[2]):
    counter[p] = counter.get(p, 0) + 1
    order[i, p] = counter[p]
    
for i, p, y in ipy:
    print('{:06}{:06}'.format(p, order[i, p]))

Solution (Python):
n, m = [int(x) for x in input().split()]
pi_yi_list = [[] for i in range(n)]
input_list = []
iddict = {}
for i in range(m):
    pi, yi = [int(x) for x in input().split()]
    pi_yi_list[pi - 1].append(yi)
    input_list.append(str(pi) + str(yi))
    iddict[str(pi) + str(yi)] = -1
pi_yi_list_sorted = [sorted(x) for x in pi_yi_list]
for pi, pilist in enumerate(pi_yi_list_sorted):
    for j, yi in enumerate(pilist):
        iddict[str(pi + 1) + str(yi)] = '{:0=6}'.format(pi+1) + '{:0=6}'.format(j+1)

for input in input_list:
    print(iddict[input])

Solution (Python):
N, M = map(int, input().split())
PY = [list(map(int,input().split())) for i in range(M)]

ken_d = [[] for _ in range(N+1)]

rankdic = {} 

for i, p in enumerate(PY):
    
    ken_d[p[0]].append([i, p[1]])

for k in range(N+1):
    ken_d[k].sort(key=lambda x:x[1])
    for i, v in enumerate(ken_d[k]):
        rankdic[v[0]] = i 

for i, p in enumerate(PY):
    x = rankdic[i] + 1
    print(str(p[0]).zfill(6) + str(x).zfill(6))

Solution (Python):
N, M = map(int, input().split())
import sys
PY = [list(map(int,sys.stdin.readline().rstrip().split())) for _ in range(M)]



ken_d = [[] for _ in range(N+1)]

rankdic = {} 

for i, p in enumerate(PY):
    
    ken_d[p[0]].append([i, p[1]])

for k in range(N+1):
    ken_d[k].sort(key=lambda x:x[1])
    for i, v in enumerate(ken_d[k]):
        rankdic[v[0]] = i 

for i, p in enumerate(PY):
    x = rankdic[i] + 1
    print(str(p[0]).zfill(6) + str(x).zfill(6))

Solution (Python):
N, M = map(int, input().split())
import sys
PY = [list(map(int,sys.stdin.readline().rstrip().split())) for _ in range(M)]



ken_d = [[] for _ in range(N+1)]

rankdic = {} 

for i, p in enumerate(PY):
    
    ken_d[p[0]].append([i, p[1]])

for k in range(N+1):
    ken_d[k].sort(key=lambda x:x[1])
    for i, v in enumerate(ken_d[k]):
        rankdic[v[0]] = i 

for i, p in enumerate(PY):
    x = rankdic[i] + 1
    print(str(p[0]).zfill(6) + str(x).zfill(6))

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

N, M = map(int, input().split())
YPIs = []
for m in range(M):
    p, y = map(int, input().split())
    YPIs.append((y, p, m))
YPIs.sort()

ans = [None] * M
ctr = Counter()
for y, p, idx in YPIs:
    ans[idx] = "{:06d}{:06d}".format(p, ctr[p] + 1)
    ctr[p] += 1
for a in ans:
    print(a)

Solution (Python):
import numpy as np

n,m=[int(i) for i in input().split()]
inp=[]
for i in range(m):
    p,y=[int(i) for i in input().split()]
    inp.append([p,y,i])

P=[[] for i in range(n)]
for i in range(m):
    P[inp[i][0]-1].append(inp[i])

out=[]
for i in range(n):
    P[i].sort(key=lambda x:x[1])
    l=len(P[i])
    for j in range(l):
        P[i][j].append(j+1)
        out.append(P[i][j])
out.sort(key=lambda x:x[2])

for i in range(m):
    x=str(out[i][0])
    y=str(out[i][3])
    ID=x.zfill(6)+y.zfill(6)
    print(ID)

Solution (Python):
n, m = map(int, input().strip().split())
py = []
pyd = {}

for _ in range(m):
  p, y = map(int, input().strip().split())
  py.append((p, y))
  if p in pyd:
    pyd[p].append(y)
  else:
    pyd[p] = [y]

num = {}
for p, yl in pyd.items():
  for i, y in enumerate(sorted(yl)):
    num[(p,y)] = "%06d%06d" % (p, i+1)

for _py in py:
  print(num[_py])

Solution (Python):
import copy
def a():return input()
def ai():return int(input())
def ma():return map(int, input().split())
def ms():return map(str, input().split())
def lma():return list(map(int, input().split()))
def lms():return list(map(str, input().split()))

N, M = ma()
PY = [lma() + [i] for i in range(M)]
KY = PY.sort()
ans = []
cnt = [0]*N
for p, y, i in PY: 
    ans.append(["%06d%06d" % (p, cnt[p - 1] + 1), i])
    cnt[p - 1] += 1
ans = sorted(ans, key=lambda x: x[1])
for a,i in ans:
    print(a)

Solution (Python):
from operator import itemgetter
n,m = map(int,input().split())

p = [list(map(int,input().split())) for i in range(m)]

b=[[i] for i in range(m)]

for i in range(m):
  p[i].extend(b[i])
  
p = sorted(sorted(p, key=itemgetter(1)))

old_p=0
for i in range(m):
  if p[i][0] != old_p :
    old_p = p[i][0]
    count = 1
  else:
    count +=1
  p[i][0] = "0"*(6-len(str(p[i][0])))+str(p[i][0])+"0"*(6-len(str(count)))+str(count)

p = sorted(p, key=itemgetter(2))

for i in range(m):
  print(p[i][0])

Solution (Python):
N,M=map(int,input().split())
inp=[list(map(int,input().split())) for i in range(M)]
P=[[] for i in range(N)]

for i in range(0,len(inp)):
    P[inp[i][0]-1]+=[inp[i][1]]
for i in range(N):
    P[i].sort()

for i in range(0,len(inp)):
    a=P[inp[i][0]-1]
    b=inp[i][1]
    max=len(a)
    min=-1

    while(max>=min):
        mid=int((max+min)/2)
        if(a[mid]==b):
            print(str(inp[i][0]).zfill(6)+str(mid+1).zfill(6))
            break
        elif(a[mid]<b):
            min=mid+1
        else:
            max=mid-1

Solution (Python):
n, m = map(int, input().split())
D = [[] for _ in range(n)]
for i in range(m):
    p,y = map(int, input().split())
    D[p-1].append((y,i))
ans = [None] * m
for i,d in enumerate(D):
    d.sort()
    for j, (k,l) in enumerate(d):
        ans[l] = str(i+1).zfill(6) + str(j+1).zfill(6)
for i in ans:
    print(i)

Solution (Python):


class City:
    def __init__(self, i1, p, y, o=None):
        self.i1 = i1
        self.p = p
        self.y = y
        self.o = o

    
    

    def get_id(self):
        return "%06d%06d" % (self.p, self.o)

def put_ids(n, cities):
    years = {i: [] for i in range(1, n + 1)}
    for c in cities:
        years[c.p].append(c.y)

    year_to_order = {i: {} for i in range(1, n + 1)}
    for i in range(1, n + 1):
        ys = sorted(years[i])
        for j0, y in enumerate(ys):
            year_to_order[i][y] = j0 + 1

    for c in cities:
        c.o = year_to_order[c.p][c.y]

def main():
    n, m = map(int, input().split())
    cities = []
    for i0 in range(m):
        p, y = map(int, input().split())
        city = City(i0, p, y)
        cities.append(city)
    put_ids(n, cities)
    for c in cities:
        print(c.get_id())

main()

Solution (Python):
N, M = [int(a) for a in input().strip().split()]
P = []
Y = []
prefs = {}
YtoIdx = {}
results = ['' for i in range(0, M)]
for i in range(0, M):
    pk, yk = [int(a) for a in input().strip().split()]
    if not pk in prefs:
        prefs[pk] = []
    prefs[pk].append(yk)
    P.append(pk)
    Y.append(yk)
    YtoIdx[yk] = i

for p in prefs.keys():
    prefs[p].sort()
    for i, y in enumerate(prefs[p]):
        pre = str(p).zfill(6)
        suf = str(i+1).zfill(6)
        results[YtoIdx[y]] = pre + suf

for res in results:
    print(res)