Problem: 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.

Submission attempts (in order):
n, m = map(int, input().split())
p, y = [0] * m, [0] * m
sort_year = [0] * m
initial_index, count_city = [0] * n, [0] * n
tmp_counter = [0] * n 
for i in range(m):
    p[i], y[i] = map(int, input().split())
    count_city[p[i] - 1] += 1
for j in range(1, n):
  	initial_index[j] = initial_index[j] + count_city[j - 1]
for i in range(m):
    sort_year[initial_index[p[i] - 1] + tmp_counter[p[i] - 1]] = y[i]
    tmp_counter[p[i] - 1] += 1
for j in range(n):
    sort_year[initial_index[j] : initial_index[j] + count_city[j - 1]].sort()
for i in range(m):
    x = sort_year.index(y[i]) - initial_index[p[i] - 1] + 1
    print(str(p[i]).zfill(6) + str(x).zfill(6))


n, m = map(int, input().split())
p, y = [0] * m, [0] * m
sort_year = [0] * m
initial_index, count_city = [0] * n, [0] * n
tmp_counter = [0] * n 
for i in range(m):
    p[i], y[i] = map(int, input().split())
    count_city[p[i] - 1] += 1
for j in range(1, n):
  	initial_index[j] = initial_index[j] + count_city[j - 1]
for i in range(m):
    sort_year[initial_index[p[i] - 1] + tmp_counter[p[i] - 1]] = y[i]
    tmp_counter[p[i] - 1] += 1
for j in range(n):
    sort_year[initial_index[j] : initial_index[j] + count_city[j - 1]].sort()
for i in range(m):
    x = sort_year.index(y[i]) - initial_index[p[i] - 1] + 1
    print(str(p[i]).zfill(8) + str(x).zfill(8))


n, m = map(int, input().split())
p, y = [0] * m, [0] * m
sort_year = [0] * m
initial_index, count_city = [0] * n, [0] * n
tmp_counter = [0] * n 
for i in range(m):
    p[i], y[i] = map(int, input().split())
    count_city[p[i] - 1] += 1
for j in range(1, n):
  	initial_index[j] = initial_index[j - 1] + count_city[j - 1]
for i in range(m):
    sort_year[initial_index[p[i] - 1] + tmp_counter[p[i] - 1]] = y[i]
    tmp_counter[p[i] - 1] += 1
for j in range(n):
    sort_year[initial_index[j] : initial_index[j] + count_city[j - 1]] = sorted(sort_year[initial_index[j] : initial_index[j] + count_city[j - 1]])
for i in range(m):
    x = sort_year.index(y[i]) - initial_index[p[i] - 1] + 1
    print(str(p[i]).zfill(6) + str(x).zfill(6))

import bisect
def index(alist, x):
    i = bisect.bisect_left(alist, x)
    if i != len(alist) and alist[i] == x:
        return i
    raise ValueError

n, m = map(int, input().split())
p, y = [0] * m, [0] * m
year_list = [[] for _ in range(n)]
for i in range(m):
    p[i], y[i] = map(int, input().split())
    year_list[p[i] - 1].append(y[i])
for j in range(n):
    year_list[j].sort()
for i in range(m):
    x = index(year_list[p[i] - 1], y[i]) + 1
    print(str(p[i]).zfill(6) + str(x).zfill(6))

Verdicts: Runtime Error

Runtime Error

Wrong Answer

Accepted