Mandarin chinese
, Russian and Vietnamese as well.
Chef is organising a contest with $P$ problems (numbered $1$ through $P$). Each problem has $S$ subtasks (numbered $1$ through $S$).
The difficulty of a problem can be calculated as follows:
- Let's denote the score of the $k$-th subtask of this problem by $SC_k$ and the number of contestants who solved it by $NS_k$.
- Consider the subtasks sorted in the order of increasing score.
- Calculate the number $n$ of valid indices $k$ such that $NS_k > NS_{k + 1}$.
- For problem $i$, the difficulty is a pair of integers $(n, i)$.
You should sort the problems in the increasing order of difficulty levels. Since difficulty level is a pair, problem $a$ is more difficult than problem $b$ if the number $n$ is greater for problem $a$ than for problem $b$, or if $a > b$ and $n$ is the same for problems $a$ and $b$.

-----Input-----
- The first line of the input contains two space-separated integers $P$ and $S$ denoting the number of problems and the number of subtasks in each problem.
- $2P$ lines follow. For each valid $i$, the $2i-1$-th of these lines contains $S$ space-separated integers $SC_1, SC_2, \dots, SC_S$ denoting the scores of the $i$-th problem's subtasks, and the $2i$-th of these lines contains $S$ space-separated integers $NS_1, NS_2, \dots, NS_S$ denoting the number of contestants who solved the $i$-th problem's subtasks.

-----Output-----
Print $P$ lines containing one integer each — the indices of the problems in the increasing order of difficulty.

-----Constraints-----
- $1 \le P \le 100,000$
- $2 \le S \le 30$
- $1 \le SC_i \le 100$ for each valid $i$
- $1 \le NS_i \le 1,000$ for each valid $i$
- in each problem, the scores of all subtasks are unique

-----Subtasks-----
Subtask #1 (25 points): $S = 2$
Subtask #2 (75 points): original constraints

-----Example Input-----
3 3
16 24 60
498 861 589
14 24 62
72 557 819
16 15 69
435 779 232

-----Example Output-----
2
1
3

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Sorting

Constructive algorithms

Ad-hoc


Solutions:
(p, sb) = map(int, input().split())
ans = list()
for i in range(p):
	(s, n) = (list(map(int, input().split())), list(map(int, input().split())))
	lis = list()
	cnt = 0
	for j in range(sb):
		lis.append([s[j], n[j]])
	lis.sort()
	for j in range(sb - 1):
		if lis[j][1] > lis[j + 1][1]:
			cnt += 1
	ans.append([cnt, i])
ans.sort()
for i in ans:
	print(i[1] + 1)


(prob, s) = list(map(int, input().split()))
p = []
for i in range(prob):
	temp = []
	temp.append(list(map(int, input().split())))
	temp.append(list(map(int, input().split())))
	temp = [[temp[0][i], temp[1][i]] for i in range(s)]
	temp.sort()
	p.append(temp)
ans = []
for i in range(prob):
	n = 0
	for j in range(s - 1):
		if p[i][j][1] > p[i][j + 1][1]:
			n += 1
	ans.append((n, i + 1))
ans.sort(key=lambda x: (x[0], x[1]))
for i in range(len(ans)):
	print(ans[i][1])


(p, s) = map(int, input().split())
difficultyRating = []
for i in range(p):
	arSC = list(map(int, input().split()))
	arNS = list(map(int, input().split()))
	problem = list(zip(arSC, arNS))
	problem.sort(key=lambda x: x[0])
	n = 0
	for j in range(s - 1):
		if problem[j][1] > problem[j + 1][1]:
			n += 1
	difficultyRating.append([n, i])
difficultyRating.sort()
for i in range(p):
	print(difficultyRating[i][1] + 1)


(p, s) = map(int, input().split(' '))
pn = []

def getScore(ps):
	c = 0
	for i in range(1, s):
		if ps[i - 1][1] > ps[i][1]:
			c += 1
	return c
for i in range(p):
	ps = []
	ns = list(map(int, input().split(' ')))
	sc = list(map(int, input().split(' ')))
	for j in range(s):
		ps.append((ns[j], sc[j]))
	ps = sorted(ps)
	pn.append((getScore(ps), i))
pn = sorted(pn)
for (_, i) in pn:
	print(i + 1)


(p, s) = list(map(int, input().split()))
sub = []
pro = []
t = []
for i in range(p):
	sub.append(list(map(int, input().split())))
	pro.append(list(map(int, input().split())))
for i in range(p):
	t.append(list(zip(sub[i], pro[i])))
indices = []
for a in t:
	a.sort(key=lambda x: x[0])
	m = 0
	n = 0
	while m != s - 1:
		if a[m][1] > a[m + 1][1]:
			n += 1
		m += 1
	indices.append(n)
pb = [i + 1 for i in range(p)]
sol = list(zip(indices, pb))
sol.sort()
for a in sol:
	print(a[1])


(p, s) = tuple(map(int, input().split()))
ans = {}
for i in range(p):
	score = list(map(int, input().split()))
	contestant = list(map(int, input().split()))
	d = {}
	for j in range(s):
		d[score[j]] = contestant[j]
	inc_score = list(d.keys())
	inc_score.sort()
	new = []
	for j in range(s):
		new.append(d[inc_score[j]])
	count = 0
	for j in range(s - 1):
		if new[j] > new[j + 1]:
			count += 1
	ans[i + 1] = count
final = sorted(ans.items(), key=lambda x: x[1])
for i in range(p):
	print(final[i][0])


(P, S) = map(int, input().split())
res = []
for j in range(P):
	SC = list(map(int, input().split()))
	NS = list(map(int, input().split()))
	l = list(zip(SC, NS))
	l.sort(key=lambda x: x[0])
	cnt = 0
	for i in range(S - 1):
		if l[i][1] > l[i + 1][1]:
			cnt += 1
	res.append((cnt, j))
for ele in sorted(res):
	print(ele[1] + 1)


from operator import itemgetter

def getIntArray():
	return [int(x) for x in input().split()]
(P, S) = getIntArray()
difficulties = []
for i in range(P):
	SC = getIntArray()
	NS = getIntArray()
	Z = sorted(zip(SC, NS), key=itemgetter(0))
	difficulty = 0
	for x in range(len(Z) - 1):
		if Z[x][1] > Z[x + 1][1]:
			difficulty += 1
	difficulties.append((difficulty, i + 1))
difficulties.sort(key=itemgetter(0))
for (a, b) in difficulties:
	print(b)


(p, n) = map(int, input().split())
diffs = []
for i in range(p):
	scores = [int(_) for _ in input().split()]
	subs = [int(_) for _ in input().split()]
	pairs = [[scores[i], subs[i]] for i in range(n)]
	pairs.sort(key=lambda x: x[0])
	count = 0
	for j in range(1, n):
		if pairs[j][1] < pairs[j - 1][1]:
			count += 1
	diffs.append((count, i))
diffs.sort(key=lambda x: x[0])
for i in diffs:
	print(i[1] + 1)


from collections import OrderedDict
temp = input().split()
p = int(temp[0])
sc = int(temp[1])
ans = list()
pi = 1
while p > 0:
	cnt = 0
	p -= 1
	s = list(map(int, input().split()))
	n = list(map(int, input().split()))
	d = list()
	for i in range(sc):
		d.append([s[i], n[i]])
	d.sort()
	for i in range(sc - 1):
		if d[i][1] > d[i + 1][1]:
			cnt += 1
	ans.append([cnt, pi])
	pi += 1
ans.sort()
for i in ans:
	print(i[1])


(p, s) = map(int, input().split())
l = []
for i in range(p):
	ns = list(map(int, input().split()))
	n = list(map(int, input().split()))
	a = list(zip(ns, n))
	a.sort()
	count = 0
	for j in range(len(a) - 1):
		if a[j][1] > a[j + 1][1]:
			count += 1
	l.append((i, count))
l.sort(key=lambda x: x[1])
for i in range(p):
	print(l[i][0] + 1)


from collections import defaultdict
from operator import itemgetter
(p, s) = map(int, input().split())
diff = []
for _ in range(p):
	sc = list(map(int, input().split()))
	nk = list(map(int, input().split()))
	arr = list(zip(sc, nk))
	arr_sort = sorted(arr, key=lambda x: x[0])
	temp = 0
	for i in range(s - 1):
		if arr_sort[i][1] > arr_sort[i + 1][1]:
			temp += 1
	a = [temp, _ + 1]
	diff.append(a)
temp = diff[:]
temp.sort(key=lambda x: (x[0], x[1]))
diff = sorted(diff, key=lambda x: x[0])
for i in diff:
	print(i[1])


(p, s) = map(int, input().split())
DR = []
for i in range(p):
	SC = list(map(int, input().split()))
	NS = list(map(int, input().split()))
	Pr = list(zip(SC, NS))
	Pr.sort(key=lambda x: x[0])
	n = 0
	for j in range(s - 1):
		if Pr[j][1] > Pr[j + 1][1]:
			n += 1
	DR.append([n, i])
DR.sort()
for i in range(p):
	print(DR[i][1] + 1)


ps = list(map(int, input().split()))
p = ps[0]
s = ps[1]
pn = []
for i in range(p):
	sn = []
	sl = list(map(int, input().split()))
	nl = list(map(int, input().split()))
	for j in range(s):
		sn.append((sl[j], nl[j]))
	sn.sort(key=lambda x: x[0])
	c = 0
	for k in range(s - 1):
		if sn[k][1] > sn[k + 1][1]:
			c += 1
	pn.append((i + 1, c))
pn.sort(key=lambda x: (x[1], x[0]))
for a in pn:
	print(a[0])


(pavan, sai) = list(map(int, input().split()))
narr = []
for k in range(900):
	narr.append([-1])
for i2 in range(pavan):
	sc = list(map(int, input().split()))
	ns1 = list(map(int, input().split()))
	ns = [x for (_, x) in sorted(zip(sc, ns1))]
	count = 0
	for i in range(sai - 1):
		if ns[i] > ns[i + 1]:
			count += 1
	narr[count].append(i2 + 1)
for i in range(900):
	for h in narr[i]:
		if h != -1:
			print(h)


import copy

def compute_problem_difficulty(problem, S):
	difficulty = 0
	for i in range(S - 1):
		if problem[i][1] > problem[i + 1][1]:
			difficulty += 1
	return difficulty
(P, S) = list(map(int, input().split()))
problem_difficulty = [[] for _ in range(30)]
i = 0
while i < P:
	subtasks_scores = list(map(int, input().split()))
	subtasks_num_of_contestants = list(map(int, input().split()))
	problem = []
	for j in range(S):
		problem.append([subtasks_scores[j], subtasks_num_of_contestants[j]])
	problem.sort()
	difficulty = compute_problem_difficulty(problem, S)
	problem_difficulty[difficulty].append(i + 1)
	i += 1
for i in range(30):
	j = 0
	while j < len(problem_difficulty[i]):
		print(problem_difficulty[i][j])
		j += 1


(p, s) = map(int, input().split(' '))
res = []
for _ in range(p):
	sc = list(map(int, input().split(' ')))
	ns = list(map(int, input().split(' ')))
	arr = [[sc[i], ns[i]] for i in range(s)]
	arr.sort(key=lambda x: x[0])
	n = 0
	for i in range(1, s):
		if arr[i - 1][1] > arr[i][1]:
			n += 1
	res.append([n, _ + 1])
res.sort(key=lambda x: x[1])
res.sort(key=lambda x: x[0])
for ans in res:
	print(ans[1])


(p, s) = [int(i) for i in input().split()]
scores = {}
for j in range(1, p + 1):
	sc = [int(i) for i in input().split()]
	ns = [int(i) for i in input().split()]
	nsc = dict(zip(sc, ns))
	ssc = sorted(sc)
	score = 0
	for (a, b) in zip(ssc[:-1], ssc[1:]):
		if nsc[a] > nsc[b]:
			score += 1
	if score in scores.keys():
		scores[score].append(j)
	else:
		scores[score] = [j]
total_scores = sorted(list(scores.keys()))
final_list = []
for val in total_scores:
	final_list += sorted(scores[val])
for val in final_list:
	print(val)


(a, b) = map(int, input().split())
ans = list()
for _ in range(a):
	s = list(map(int, input().split()))
	n = list(map(int, input().split()))
	cnt = 0
	lis = [[s[i], n[i]] for i in range(b)]
	lis.sort()
	for i in range(b - 1):
		if lis[i][1] > lis[i + 1][1]:
			cnt += 1
	ans.append([cnt, _])
ans.sort()
for i in ans:
	print(i[1] + 1)


(p, s) = map(int, input().split())
diff = []
for j in range(1, p + 1):
	score = list(map(int, input().split()))
	n = list(map(int, input().split()))
	join = [[x, y] for (x, y) in zip(score, n)]
	join.sort()
	d = 0
	for i in range(s - 1):
		if join[i][1] > join[i + 1][1]:
			d += 1
	diff.append([d, j])
diff.sort()
for i in diff:
	print(i[1])


from functools import cmp_to_key

def comp(a, b):
	if a[0] > b[0]:
		return 1
	elif b[0] > a[0]:
		return -1
	elif a[1] > b[1]:
		return 1
	elif a[1] < b[1]:
		return -1
	else:
		return 0
(p, s) = list(map(int, input().split()))
problems = []
for i in range(1, p + 1):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	subtasks = []
	for k in range(s):
		subtasks.append((sc[k], ns[k]))
	subtasks.sort(key=lambda x: x[0])
	n = 0
	for k in range(s - 1):
		if subtasks[k][1] > subtasks[k + 1][1]:
			n += 1
	problems.append((n, i))
for (n, i) in sorted(problems, key=cmp_to_key(comp)):
	print(i)


(P, S) = map(int, input().split(' '))
ans = []
for i in range(0, P):
	sc = list(map(int, input().split(' ')))
	ns = list(map(int, input().split(' ')))
	temp = {}
	for j in range(0, len(ns)):
		temp[sc[j]] = ns[j]
	key = temp.keys()
	key = sorted(key)
	n = 0
	for k in range(0, len(key) - 1):
		index = key[k]
		index2 = key[k + 1]
		if temp[index] > temp[index2]:
			n = n + 1
	ans.append([i + 1, n])
sorted_list = sorted(ans, key=lambda x: x[1])
for h in sorted_list:
	print(h[0])


ans = []
(x, y) = map(int, input().split())
for _ in range(x):
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	L = list(zip(A, B))
	L.sort()
	co = 1
	for i in range(y - 1):
		if L[i][1] > L[i + 1][1]:
			co += 1
	ans.append([co, _])
ans.sort()
for i in ans:
	print(i[1] + 1)


import os.path
from math import gcd, floor, ceil, log2
from collections import *
import sys
mod = 1000000007
INF = float('inf')

def st():
	return list(sys.stdin.readline().strip())

def li():
	return list(map(int, sys.stdin.readline().split()))

def mp():
	return map(int, sys.stdin.readline().split())

def inp():
	return int(sys.stdin.readline())

def pr(n):
	return sys.stdout.write(str(n) + '\n')

def prl(n):
	return sys.stdout.write(str(n) + ' ')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
(p, s) = mp()
diff = []
for i in range(p):
	sc = li()
	ns = li()
	(sc, ns) = zip(*sorted(zip(sc, ns)))
	x = 0
	for j in range(1, s):
		if ns[j - 1] > ns[j]:
			x += 1
	diff.append((x, i))
diff.sort()
for i in diff:
	print(i[1] + 1)


def difficulty(SC, NS):
	n = 0
	zipped = sorted(zip(SC, NS))
	for i in range(len(zipped) - 1):
		if zipped[i][1] > zipped[i + 1][1]:
			n += 1
	return n
(P, S) = map(int, input().split())
difficulties = []
for i in range(P):
	SC = list(map(int, input().split()))
	NS = list(map(int, input().split()))
	diff = difficulty(SC, NS)
	difficulties.append((diff, i + 1))
difficulties.sort()
for (diff, index) in difficulties:
	print(index)


from collections import *
(P, S) = input().split()
P = int(P)
S = int(S)
list_of_dictionaries = []
for _ in range(P):
	scores = input().split()
	number_of_contestants = input().split()
	scores = map(int, scores)
	number_of_contestants = map(int, number_of_contestants)
	dictionary = dict(zip(scores, number_of_contestants))
	dictionary = OrderedDict(sorted(dictionary.items()))
	list_of_dictionaries.append(dictionary)
list_of_difficulty_tuples = []
for alpha in range(P):
	current_dictionary = list_of_dictionaries[alpha]
	old = -1
	n = 0
	for v in current_dictionary.values():
		if old == -1:
			old = v
		elif v < old:
			n = n + 1
			old = v
		else:
			old = v
	list_of_difficulty_tuples.append((n, alpha + 1))
list_of_difficulty_tuples = sorted(list_of_difficulty_tuples)
for result in range(len(list_of_difficulty_tuples)):
	print(list_of_difficulty_tuples[result][1])


(p, s) = map(int, input().split())
sc = []
n = []
diff = []
for i in range(2 * p):
	inp = [int(i) for i in input().split()]
	if i % 2:
		j = sc[-1]
		for i in range(s):
			n.append([j[i], inp[i]])
	else:
		sc.append(inp)
for j in range(0, len(n), s):
	tp = n[j:j + s]
	tp.sort(key=lambda v: v[0])
	d = 0
	for i in range(s - 1):
		if tp[i][1] > tp[i + 1][1]:
			d += 1
	diff.append([d, j // s])
diff.sort(key=lambda v: v[0])
for i in diff:
	print(i[1] + 1)


(p, s) = map(int, input().split())
M = []
for i in range(p):
	sc = list(map(int, input().split()))
	np = list(map(int, input().split()))
	d = []
	count = 0
	for j in range(s):
		d.append([sc[j], np[j]])
	d.sort()
	for x in range(s - 1):
		if d[x][1] > d[x + 1][1]:
			count += 1
	M.append([count, i + 1])
M.sort()
for z in M:
	print(z[1])


(p, s) = map(int, input().split())
a = []
for i in range(p):
	temp = []
	temp.append(list(map(int, input().split())))
	temp.append(list(map(int, input().split())))
	temp = [[temp[0][i], temp[1][i]] for i in range(s)]
	temp.sort()
	cnt = 0
	for j in range(s - 1):
		if temp[j][1] > temp[j + 1][1]:
			cnt += 1
	a.append([cnt, i])
a.sort()
s = ''
for i in range(p):
	print(a[i][1] + 1)


(P, S) = [int(_) for _ in input().split()]
res = []
for __ in range(P):
	c = [int(_) for _ in input().split()]
	n = [int(_) for _ in input().split()]
	arr = [[c[i], n[i]] for i in range(S)]
	arr.sort(key=lambda x: x[0])
	ans = 0
	for i in range(S - 1):
		if arr[i][1] > arr[i + 1][1]:
			ans += 1
	res.append([ans, __ + 1])
res.sort(key=lambda x: x[1])
res.sort(key=lambda x: x[0])
for ans in res:
	print(ans[1])


def mi():
	return map(int, input().split())

def li():
	return list(mi())

def si():
	return str(input())

def ni():
	return int(input())
(p, s) = mi()
A = []
for at in range(p):
	d = []
	n = 0
	L = li()
	X = li()
	for i in range(s):
		d.append((L[i], X[i]))
	d.sort()
	for i in range(s - 1):
		if d[i][1] > d[i + 1][1]:
			n += 1
	A.append((n, at + 1))
A.sort()
for fg in range(p):
	print(A[fg][1])


(p, s) = map(int, input().split())
res = []
for j in range(p):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	arr = [(sc[i], ns[i]) for i in range(s)]
	arr.sort()
	n = 0
	for i in range(s - 1):
		if arr[i][1] > arr[i + 1][1]:
			n += 1
	res.append((n, j + 1))
res.sort()
for r in res:
	print(r[1])


(p, s) = map(int, input().split())
difficulty = []
for i in range(p):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	(sc, ns) = zip(*sorted(zip(sc, ns)))
	n = 0
	for j in range(1, s):
		if ns[j - 1] > ns[j]:
			n += 1
	difficulty.append((n, i))
difficulty.sort()
print('\n'.join(map(str, list(map(lambda x: x[1] + 1, difficulty)))))


(p, s) = map(int, input().split())
ans = []
for i in range(p):
	m = list(map(int, input().split()))
	n = list(map(int, input().split()))
	count = 0
	d = list(zip(m, n))
	d.sort()
	for j in range(s - 1):
		if d[j][1] > d[j + 1][1]:
			count += 1
	ans.append([count, i])
ans.sort()
for i in range(p):
	print(ans[i][1] + 1)


l = input().split()
l[0] = int(l[0])
l[1] = int(l[1])
d = {}
kool = []
c = 0
for forbid in range(l[0]):
	(l, n) = (input().split(), input().split())
	for i in range(len(l)):
		l[i] = int(l[i])
		n[i] = int(n[i])
		d[l[i]] = n[i]
	l.sort()
	for i in range(len(l) - 1):
		(x, y) = (d[l[i]], d[l[i + 1]])
		if y < x:
			c += 1
	kool.append([c, forbid])
	c = 0
kool.sort()
for i in kool:
	print(i[1] + 1)


(p, s) = map(int, input().split())
dif = []
for i in range(p):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	n = 0
	scores = [(sc[j], ns[j]) for j in range(s)]
	scores.sort()
	for t in range(s - 1):
		if scores[t][1] > scores[t + 1][1]:
			n += 1
	dif.append((n, i))
dif.sort()
for v in dif:
	print(v[1] + 1)


(p, s) = map(int, input().split())
arr = []
for j in range(p):
	sc1 = list(map(int, input().split()))
	sc2 = list(map(int, input().split()))
	sc = list(zip(sc1, sc2))
	sc = sorted(sc)
	diff = 0
	for i in range(s - 1):
		if sc[i][1] > sc[i + 1][1]:
			diff += 1
	arr.append((diff, j))
arr = sorted(arr)
for i in range(p):
	print(arr[i][1] + 1)


(p, s) = list(map(int, input().split()))
nvalues = []
for i in range(p):
	difficult = [0, 0]
	n = 0
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	a = list(zip(sc, ns))
	a.sort()
	for j in range(s - 1):
		if a[j][1] > a[j + 1][1]:
			n = n + 1
	difficult[0] = n
	difficult[1] = i
	nvalues.append(difficult)
nvalues.sort(key=lambda x: x[0])
for j in range(p):
	print(nvalues[j][1] + 1)


(p, s) = map(int, input().split())
dic = {}
for i in range(p):
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	l2 = zip(l1, l2)
	l2 = sorted(l2, key=lambda x: x[0])
	cnt = 0
	for j in range(s - 1):
		if l2[j][1] > l2[j + 1][1]:
			cnt += 1
	if cnt not in dic.keys():
		dic[cnt] = []
	dic[cnt].append(i + 1)
for key in sorted(dic):
	for j in dic[key]:
		print(j)


(p, s) = map(int, input().split())
diff = {i: 0 for i in range(p)}
for i in range(p):
	score = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	a = [(score[i], arr[i]) for i in range(s)]
	a.sort()
	arr = [a[i][1] for i in range(s)]
	for j in range(s - 1):
		if arr[j] > arr[j + 1]:
			diff[i] += 1
lst = [(j, i) for (i, j) in diff.items()]
lst.sort()
for i in lst:
	print(i[1] + 1)


(P, S) = list(map(int, input().split()))
COUNT = [0] * 101010
A = []
B = []
for i in range(P):
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	(A, B) = list(zip(*sorted(zip(A, B))))
	for X in range(S - 1):
		if B[X] > B[X + 1]:
			COUNT[i] += 1
for i in range(31):
	for j in range(P):
		if COUNT[j] == i:
			print(j + 1)


(P, S) = map(int, input().split(' '))
difficulties = []
for p in range(P):
	scores = list(map(int, input().split(' ')))
	contestants = list(map(int, input().split(' ')))
	score_indices = sorted(list(range(S)), key=scores.__getitem__)
	n = 0
	for s in range(S - 1):
		if contestants[score_indices[s]] > contestants[score_indices[s + 1]]:
			n += 1
	difficulties.append((n, p + 1))
indices = sorted(list(range(P)), key=difficulties.__getitem__)
for index in indices:
	print(index + 1)


t = [int(a) for a in input().split()]
p = t[0]
s = t[1]
sc = []
ns = []
pair = []
ans = []
for x in range(2 * p):
	k = [int(a) for a in input().split()]
	if x % 2 == 0:
		sc.append(k)
	else:
		ns.append(k)
for x in range(p):
	l = []
	for y in range(s):
		l.append((sc[x][y], ns[x][y]))
	pair.append(l)
for x in range(p):
	pair[x].sort()
	su = 0
	for y in range(s - 1):
		if pair[x][y][1] > pair[x][y + 1][1]:
			su += 1
	ans.append([su, x + 1])
ans.sort()
for x in range(p):
	print(ans[x][1])


(p, s) = map(int, input().split())
tasks = []

def _difficulty(i, subtasks):
	subtasks = sorted(subtasks, key=lambda e: e[1])
	n = 0
	for k in range(len(subtasks) - 1):
		if subtasks[k][2] > subtasks[k + 1][2]:
			n += 1
	return (n, i)
for k in range(p):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	subtasks = []
	for i in range(len(sc)):
		subtasks.append((i, sc[i], ns[i]))
	tasks.append((k, subtasks, _difficulty(k, subtasks)))
tasks_sorted = sorted(tasks, key=lambda e: e[2])
for i in range(len(tasks_sorted)):
	print(tasks_sorted[i][0] + 1)


(P, S) = list(map(int, input().split()))
ans = []
for index in range(P):
	SC = list(map(int, input().split()))
	NS = list(map(int, input().split()))
	mlist = []
	for i in zip(SC, NS):
		mlist.append(i)
	mlist = sorted(mlist, key=lambda z: (z[0], z[1]), reverse=False)
	counter = 0
	for i in range(1, len(mlist)):
		if mlist[i - 1][1] > mlist[i][1]:
			counter += 1
	ans.append((counter, index + 1))
ans = sorted(ans, key=lambda v: v[0], reverse=False)
for i in ans:
	print(i[1])


(P, S) = [int(x) for x in input().split()]
problem_difficulties = []
for problem in range(P):
	SC = [int(x) for x in input().split()]
	NS = [int(x) for x in input().split()]
	SCNS = []
	for i in range(len(SC)):
		SCNS.append((SC[i], NS[i]))
	SCNS.sort(key=lambda sc: sc[0])
	n = 0
	for i in range(1, len(SCNS)):
		if SCNS[i - 1][1] > SCNS[i][1]:
			n += 1
	problem_difficulties.append((n, problem))
problem_difficulties.sort(key=lambda p: p[1])
problem_difficulties.sort(key=lambda p: p[0])
for problem in problem_difficulties:
	print(problem[1] + 1)


(n, s) = map(int, input().split())
arr = []
for _ in range(n):
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = list(zip(a, b))
	sorted_c = sorted(c)
	dif_count = 0
	for i in range(s - 1):
		if sorted_c[i][1] > sorted_c[i + 1][1]:
			dif_count += 1
	arr.append((dif_count, _))
arr = sorted(arr)
for i in range(n):
	print(arr[i][1] + 1)


(n, _) = map(int, input().split())
problem_difficulties = []
for i in range(n):
	scores = list(map(int, input().split()))
	number_of_contestents = list(map(int, input().split()))
	sorted_subtasks = sorted(zip(scores, number_of_contestents))
	difficulty = 0
	for j in range(len(sorted_subtasks) - 1):
		if sorted_subtasks[j][1] > sorted_subtasks[j + 1][1]:
			difficulty += 1
	problem_difficulties.append((difficulty, i))
problem_difficulties.sort()
for i in range(n):
	print(problem_difficulties[i][1] + 1)


res = []
(n, s) = map(int, input().split())
for _ in range(n):
	(*sub,) = map(int, input().split())
	(*stud,) = map(int, input().split())
	req = sorted(list(zip(sub, stud)), key=lambda x: x[0])
	count = 0
	prev = req[0][1]
	for (i, j) in req[1:]:
		if prev > j:
			count += 1
			prev = j
		else:
			prev = j
	res.append((count, _))
res.sort()
for cur in res:
	print(cur[1] + 1)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def outp(n):
	sys.stdout.write(str(n) + '\n')

def outlt(lst):
	sys.stdout.write(' '.join(map(str, lst)) + '\n')

def outplt(lst):
	sys.stdout.write('\n'.join(map(str, lst)))
(P, S) = inlt()
diff = [[0, x + 1] for x in range(P)]
for _ in range(P):
	SC = inlt()
	NS = inlt()
	T = [(SC[i], NS[i]) for i in range(S)]
	T.sort()
	n = 0
	for i in range(1, S):
		if T[i - 1][1] > T[i][1]:
			n += 1
	diff[_][0] = n
diff.sort(key=lambda x: (x[0], x[1]))
for d in diff:
	print(d[1])


(p, s) = map(int, input().split())
l = []
for u in range(p):
	s1 = list(map(int, input().split()))
	n1 = list(map(int, input().split()))
	d = [[0, 0] for dd in range(s)]
	for w in range(s):
		d[w][0] = s1[w]
		d[w][1] = n1[w]
	d.sort()
	c = 0
	for ss in range(s - 1):
		if d[ss][1] > d[ss + 1][1]:
			c += 1
	l.append(c * 1000000 + u + 1)
l.sort()
for hh in l:
	print(hh % 1000000)


(P, S) = list(map(int, input().split(' ')))
d = []
for p in range(0, P):
	ST = list(map(int, input().split(' ')))
	N = list(map(int, input().split(' ')))
	c = 0
	L = sorted(list(zip(ST, N)))
	for i in range(0, S - 1):
		if L[i][1] > L[i + 1][1]:
			c = c + 1
	dd = ()
	dd = (c, p + 1)
	d.append(dd)
d = sorted(d)
for i in range(0, P):
	print(d[i][1])


(a, b) = map(int, input().split())
lst = []
for j in range(a):
	c = list(map(int, input().split()))
	d = list(map(int, input().split()))
	arr = [(c[i], d[i]) for i in range(b)]
	arr.sort()
	q = 0
	for i in range(b - 1):
		if arr[i][1] > arr[i + 1][1]:
			q += 1
	lst.append((q, j + 1))
lst.sort()
for p in lst:
	print(p[1])


import bisect
(P, S) = list(map(int, input().split()))
results = []
for i in range(1, P + 1):
	scores = list(map(int, input().split()))
	solved = list(map(int, input().split()))
	solved = [x for (_, x) in sorted(zip(scores, solved), key=lambda pair: pair[0])]
	solved = [1 if solved[x] > solved[x + 1] else 0 for x in range(len(solved) - 1)]
	n = sum(solved)
	bisect.insort(results, (n, i))
for i in results:
	print(i[1])


(p, s) = map(int, input().rstrip().split())
res = []
for _ in range(p):
	a = list(map(int, input().rstrip().split()))
	b = list(map(int, input().rstrip().split()))
	arr = []
	for i in range(s):
		arr.append([a[i], b[i]])
	n = 0
	arr.sort(key=lambda x: x[0])
	for i in range(1, s):
		if arr[i - 1][1] > arr[i][1]:
			n += 1
	res.append([n, _ + 1])
res.sort(key=lambda x: x[0])
for x in res:
	print(x[1])


(p, s) = list(map(int, input().split()))
count = [0] * 101010
a = []
b = []
for i in range(p):
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	(a, b) = list(zip(*sorted(zip(a, b))))
	for x in range(len(b) - 1):
		if b[x] > b[x + 1]:
			count[i] += 1
for i in range(31):
	for j in range(p):
		if count[j] == i:
			print(j + 1)


(p, s) = map(int, input().split())
l = []
for i in range(1, p + 1):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	lr = []
	for j in range(s):
		lr.append([sc[j], ns[j]])
	lr.sort()
	cnt = 0
	for k in range(s - 1):
		if lr[k][1] > lr[k + 1][1]:
			cnt += 1
	l.append([cnt, i])
l.sort()
for j in range(p):
	print(l[j][1])


(p, s) = map(int, input().split())
l = []
din = []
for i in range(p):
	cnt = 0
	Cs = list(map(int, input().split()))
	Ns = list(map(int, input().split()))
	for j in range(s):
		din.append((Cs[j], Ns[j]))
	din.sort()
	for j in range(s - 1):
		if din[j][1] > din[j + 1][1]:
			cnt += 1
	l.append(cnt)
	din = []
lout = []
for i in range(p):
	lout.append((l[i], i + 1))
lout.sort()
for i in range(p):
	print(lout[i][1])


def calc(s, arr):
	count = 0
	for i in range(s - 1):
		if arr[i][1] > arr[i + 1][1]:
			count += 1
	return count
(p, s) = map(int, input().split())
res = []
for i in range(p):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	arr = sorted([(sc[i], ns[i]) for i in range(s)])
	res.append((calc(s, arr), i))
res.sort()
for cur in res:
	print(cur[1] + 1)


(a, b) = map(int, input().split())
lis = []
for _ in range(a):
	scores = list(map(int, input().split()))
	candidates = list(map(int, input().split()))
	a = sorted(list(zip(scores, candidates)))
	summ = 0
	for i in range(b - 1):
		if a[i][1] > a[i + 1][1]:
			summ += 1
	lis.append((summ, _ + 1))
for (i, j) in sorted(lis):
	print(j)


(p, s) = [int(i) for i in input().split()]
l = []
for j in range(p):
	l1 = [int(i) for i in input().split()]
	l2 = [int(i) for i in input().split()]
	l3 = [[l1[i], l2[i]] for i in range(s)]
	l3.sort()
	count = 0
	for i in range(s - 1):
		if l3[i][1] > l3[i + 1][1]:
			count += 1
	l.append([count, j + 1])
l.sort()
for i in l:
	print(i[1])


(p1, n1) = list(map(int, input().split(' ')))
x1 = list()
j = '0'
for i in range(p1):
	a1 = list(map(int, input().split(' ')))
	b1 = list(map(int, input().split(' ')))
	c = list()
	cx = 0
	al = len(a1)
	for z in range(al):
		c.append((a1[z], b1[z]))
	c = sorted(c, key=lambda x: x[0])
	ab = len(c)
	for z in range(ab - 1):
		if c[z + 1][1] < c[z][1]:
			cx = cx + 1
	x1.append((i, cx))
x1 = sorted(x1, key=lambda x: x[1])
for i in range(len(x1)):
	print(x1[i][0] + 1)


(p, s) = map(int, input().split())
diff = list()
for i in range(s):
	diff.append([])
for i in range(p):
	n = 0
	c = list(map(int, input().split()))
	sc = list(map(int, input().split()))
	dictionary = dict(zip(c, sc))
	c.sort()
	for j in range(s - 1):
		if dictionary[c[j]] > dictionary[c[j + 1]]:
			n += 1
	diff[n].append(i + 1)
for i in range(s):
	for j in range(len(diff[i])):
		print(diff[i][j])


from os import path
import sys
import collections
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
ans = []
(p, s) = map(int, input().split())
for _ in range(p):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	di = dict(zip(sc, ns))
	di = dict(sorted(di.items()))
	itr = list(di.values())
	r = len(itr)
	n = 0
	for i in range(r - 1):
		if itr[i + 1] < itr[i]:
			n += 1
	ans.append((_ + 1, n))
ans = sorted(ans, key=lambda x: (x[1], x[0]))
for i in ans:
	print(i[0])


import sys
import copy
import math
import heapq
import bisect
import random
import operator
import statistics
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from heapq import heapify, heappush, heappop
from itertools import permutations, combinations
from collections import Counter, OrderedDict, deque
from bisect import bisect_left as bl, bisect_right as br
from functools import lru_cache, reduce, total_ordering, update_wrapper, partial
sys.setrecursionlimit(100000000)
inp = lambda : int(input())
strr = lambda : input().strip()
jl = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
muli = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
mod1 = 1000000007
mod2 = 998244353
(p, s) = map(int, input().split())
l = []
z = 1
for _ in range(p):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	n = 0
	sc1 = []
	for i in range(s):
		sc1.append([sc[i], ns[i]])
	sc1.sort()
	for i in range(s - 1):
		if sc1[i][1] > sc1[i + 1][1]:
			n += 1
	l.append([n, z])
	z += 1
l.sort()
for j in range(len(l)):
	print(l[j][1])


(p, s) = map(int, input().split())
mixied = []
for i in range(p):
	l = list(map(int, input().split()))
	m = list(map(int, input().split()))
	m1 = []
	for j in range(s):
		m1.append((l[j], m[j]))
	m1.sort()
	n1 = 0
	for o in range(s - 1):
		if m1[o][1] > m1[o + 1][1]:
			n1 += 1
	mixied.append((n1, i + 1))
mixied.sort()
for _ in mixied:
	print(_[1])


(p, s) = map(int, input().split())
ans = []
for j in range(p):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	n = 0
	a = []
	for i in range(s):
		a.append((sc[i], ns[i]))
	a.sort()
	for i in range(s - 1):
		if a[i][1] > a[i + 1][1]:
			n += 1
	k = (n, j + 1)
	ans.append(k)
ans.sort()
for i in ans:
	print(i[1])


(P, S) = map(int, input().split())
scores = [[] for _ in range(P)]
num_contestants = [[] for _ in range(P)]
for i in range(P):
	scores[i] = list(map(int, input().split()))
	num_contestants[i] = list(map(int, input().split()))
difficulties = []
for i in range(P):
	(scores[i], num_contestants[i]) = zip(*sorted(zip(scores[i], num_contestants[i])))
	n = 0
	for j in range(S - 1):
		if num_contestants[i][j] > num_contestants[i][j + 1]:
			n += 1
	difficulties.append((n, i + 1))
difficulties.sort()
for difficulty in difficulties:
	print(difficulty[1])


(p, s) = map(int, input().split())
sc = [[0] * s for _ in range(p)]
ns = [[0] * s for _ in range(p)]
ans = [[i + 1, 0] for i in range(p)]
for i in range(p):
	sc[i] = list(map(int, input().split()))
	ns[i] = list(map(int, input().split()))
	zipped_pair = zip(sc[i], ns[i])
	zipped_pair2 = zip(sc[i], ns[i])
	sc[i] = [x for (x, _) in sorted(zipped_pair)]
	ns[i] = [x for (_, x) in sorted(zipped_pair2)]
	for j in range(s - 1):
		if ns[i][j] > ns[i][j + 1]:
			ans[i][1] += 1

def my_func(x):
	return x[1]
ans.sort(key=my_func)
for elem in ans:
	print(elem[0])


(p, s) = [int(x) for x in input().split()]
difficulty = {}
for i in range(p):
	problem = list(zip([int(x) for x in input().split()], [int(x) for x in input().split()]))
	problem.sort(key=lambda p: p[0])
	n = 0
	for j in range(1, s):
		if problem[j - 1][1] > problem[j][1]:
			n += 1
	difficulty[i + 1] = (n, i)
order = [x for x in range(1, p + 1)]
order.sort(key=lambda i: difficulty[i])
for i in order:
	print(i)


(p, s) = list(map(int, input().split()))
D = dict()
index = 1
for _ in range(p):
	S = list(map(int, input().split()))
	N = list(map(int, input().split()))
	T = [(S[i], N[i]) for i in range(s)]
	T = sorted(T)
	N = [T[i][1] for i in range(s)]
	score = 0
	for i in range(len(N) - 1):
		if N[i] > N[i + 1]:
			score += 1
	if score in D:
		D[score].append(index)
	else:
		D[score] = [index]
	index += 1
score_list = sorted(list(D.keys()))
for score in score_list:
	indexes = D[score]
	for index in indexes:
		print(index)


import math
import operator

def calculate_score(numbers):
	count = 0
	for (i, num) in enumerate(numbers):
		if i == len(numbers) - 1:
			continue
		if num > numbers[i + 1]:
			count += 1
	return count

def solve(numbers_array):
	out_arr = []
	res_arr = []
	for (i, numbers) in enumerate(numbers_array):
		score = calculate_score(numbers)
		out_arr.append((i + 1, score))
	sorted_out = sorted(out_arr, key=operator.itemgetter(1, 0))
	for (i, x) in sorted_out:
		res_arr.append(i)
	return res_arr
(p, s) = list(map(int, input().split(' ')))
numbers_array = []
for i in range(0, p):
	scores = list(map(int, input().strip().split(' ')))
	numbers = list(map(int, input().strip().split(' ')))
	mix_array = [(scores[i], numbers[i]) for i in range(s)]
	mix_array.sort()
	new_arr = [mix_array[i][1] for i in range(s)]
	numbers_array.append(new_arr)
result_array = solve(numbers_array)
for v in result_array:
	print(v)


(P, S) = map(int, input().split())
res = []
for probIdx in range(P):
	problem = []
	SC = list(map(int, input().split()))
	NS = list(map(int, input().split()))
	for i in range(S):
		problem.append((SC[i], NS[i]))
	problem.sort()
	n = 0
	for i in range(1, len(problem)):
		if problem[i - 1][1] > problem[i][1]:
			n += 1
	res.append((n, probIdx + 1))
res.sort(key=lambda x: (x[0], x[1]))
for p in res:
	print(p[1])


(p, s) = map(int, input().split())
diffs = []
for i in range(p):
	scs = list(map(int, input().split()))
	nss = list(map(int, input().split()))
	zips = sorted(zip(scs, nss))
	r = 0
	for k in range(s - 1):
		if zips[k][1] > zips[k + 1][1]:
			r += 1
	diffs.append([r, i])
diffs.sort()
for i in range(p):
	print(diffs[i][1] + 1)


from collections import defaultdict
(x, y) = list(map(int, input().split()))
m = []
p = defaultdict(list)
for __ in range(x):
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = defaultdict()
	for i in range(y):
		c[a[i]] = b[i]
	a.sort()
	d = []
	for i in range(y):
		d.append(c[a[i]])
	n = 0
	for i in range(1, y):
		if d[i] < d[i - 1]:
			n += 1
	m.append(n)
	p[n].append(__ + 1)
m.sort()
m = list(set(m))
for i in m:
	if len(p[i]) > 1:
		p[i].sort()
		for j in p[i]:
			print(j)
	else:
		print(*p[i])


(p, s) = input().split()
p = int(p)
s = int(s)
n = [0 for i in range(p)]
for prob in range(p):
	points = list(map(int, input().split()))
	nop = list(map(int, input().split()))
	for i in range(s):
		nop[i] = [points[i], nop[i]]
	nop.sort()
	for i in range(s):
		nop[i] = nop[i][1]
	sorted_nop = sorted(nop)
	n[prob] = 0
	for i in range(s):
		ind = sorted_nop.index(nop[i])
		n[prob] += ind
		sorted_nop.pop(ind)
for i in range(p):
	n[i] = [n[i], i]
n.sort()
for i in range(p):
	print(n[i][1] + 1)


(m, n) = map(int, input().split())
a = []
b = []
for i in range(m):
	a.append(list(map(int, input().split())))
	b.append(list(map(int, input().split())))
c = []
n2 = []
for j in range(m):
	for k in range(n):
		c.append([a[j][k], b[j][k]])
	c.sort()
	n1 = 0
	for r in range(n - 1):
		if c[r][1] > c[r + 1][1]:
			n1 += 1
	n2.append(n1)
	c = []
r = []
for m1 in range(m):
	r.append([n2[m1], m1 + 1])
r.sort()
for m1 in range(m):
	print(r[m1][1])


(p, s) = map(int, input().split())
lst = []
for prob in range(p):
	score = list(map(int, input().split()))
	number = list(map(int, input().split()))
	order = {}
	for i in range(s):
		order[score[i]] = i
	score.sort()
	order1 = [number[order[score[i]]] for i in range(s)]
	val = 0
	for i in range(s - 1):
		if order1[i] > order1[i + 1]:
			val += 1
	lst.append([val, prob + 1])
lst1 = [[] for i in range(s)]
for pair in lst:
	lst1[pair[0]].append(pair[1])
for i in range(s):
	lst1[i].sort()
for z in lst1:
	if len(z) != 0:
		for y in z:
			print(y)


(p, s) = map(int, input().split())
A = []
for at in range(p):
	d = []
	n = 0
	L = list(map(int, input().split()))
	X = list(map(int, input().split()))
	for i in range(s):
		d.append((L[i], X[i]))
	d.sort()
	for i in range(s - 1):
		if d[i][1] > d[i + 1][1]:
			n += 1
	A.append((n, at + 1))
A.sort()
for fg in range(p):
	print(A[fg][1])


(p, s) = input().split()
p = int(p)
s = int(s)
n = [0 for i in range(p)]
for prob in range(p):
	points = list(map(int, input().split()))
	nop = list(map(int, input().split()))
	for i in range(s):
		nop[i] = [points[i], nop[i]]
	nop.sort()
	for i in range(s - 1):
		if nop[i][1] > nop[i + 1][1]:
			n[prob] += 1
for i in range(p):
	n[i] = [n[i], i]
n.sort()
for i in range(p):
	print(n[i][1] + 1)


(s, p) = map(int, input().split())
arr = []
for i in range(s):
	sk = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	ns = sorted(zip(sk, ns), key=lambda x: x[0])
	n = 0
	for j in range(len(ns) - 1):
		if ns[j][1] > ns[j + 1][1]:
			n += 1
	arr.append([n, i])
arr.sort()
for i in arr:
	print(i[1] + 1)


l_inp = input().split(' ')
p = int(l_inp[0])
sub = int(l_inp[1])
diff = []
for i in range(p):
	score = [int(i) for i in input().split(' ')]
	ctr = 0
	att = [int(i) for i in input().split(' ')]
	final = list(zip(score, att))
	final.sort(key=lambda x: x[0])
	j = 0
	while j < sub - 1:
		if final[j][1] > final[j + 1][1]:
			ctr += 1
		j += 1
	diff.append((ctr, i + 1))
diff.sort(key=lambda x: x[0])
for i in diff:
	print(i[1])


(p, s) = map(int, input().split())
l = []
z = 1
for _ in range(p):
	sc = list(map(int, input().split()))
	ns = list(map(int, input().split()))
	n = 0
	sc1 = []
	for i in range(s):
		sc1.append([sc[i], ns[i]])
	sc1.sort()
	for i in range(s - 1):
		if sc1[i][1] > sc1[i + 1][1]:
			n += 1
	l.append([n, z])
	z += 1
l.sort()
for j in range(len(l)):
	print(l[j][1])


(a, s) = map(int, input().split())
l = []
for j in range(a):
	score = list(map(int, input().split()))
	nlist = list(map(int, input().split()))
	new = []
	for i in range(s):
		new.append([score[i], nlist[i]])
	new.sort(key=lambda x: int(x[0]))
	n = 0
	for i in range(0, s - 1):
		if new[i][1] > new[i + 1][1]:
			n += 1
	l.append([n, j + 1])
l.sort()
for t in range(a):
	print(l[t][1])