from collections import defaultdict import sys import time class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) def clc_score(num,minskil,maxskil,x,y): a=(num*(num-1)//2) * (200-(maxskil-minskil)*(maxskil-minskil)) - (num*(num-1)//2)*(y-x) # print(a) return max(a,0) # print (clc_score(2,3,4,3,5)) # print (clc_score(2,3,4,3,14)) stime=time.perf_counter() T,R=map(int,input().split()) t=0 N=5400 uf=UnionFind(N+1) p=[-1]*(N+1) tick=[-1]*(N+1) room4set=set() # score,minskill,maxskill,x,sz scores=[[0,0,100,0,1] for i in range (N+1)] s=set() # スキル値, プレイヤーi, 出現チック s.add((-10000000,-1,-1)) s.add((100000000,-1,-1)) pi=1 t_ans=set() import bisect while t<3600 : nowtime=time.perf_counter() L=list(map(int,input().split())) if nowtime - stime >4.75: t+=1 print(0) sys.stdout.flush() continue if t %50 ==0: for e in list(s): if e == (-10000000,-1,-1) or e ==(100000000,-1,-1): continue else: if t - e[2] >100 or e[1] in room4set: s.discard(e) if L[0] == 0: t+=1 print(0) sys.stdout.flush() continue for i in range(L[0]): # print(s) nowi = (L[i+1],pi,t) p[pi] = L[i+1] tick[pi] = t scores[pi] = [0,L[i+1],L[i+1],t,1] sorteds=sorted(list(s)) target=bisect.bisect_right(sorteds,nowi) for _t in list(t_ans): if abs(p[pi] - p[_t]) < 2: score,minskill,maxskill,x,sz = scores[_t] # print(score,minskill,maxskill,x,sz) sz=len(uf.members(_t)) if sz < 4: _score = clc_score(sz+1,min(minskill,p[pi]),max(maxskill,p[pi]),x,t) if _score > score: uf.union(_t, pi) t_ans.add(pi) scores[_t] = [_score, min(minskill,p[pi]), max(maxskill,p[pi]),x,sz+1] scores[pi] = [_score, min(minskill,p[pi]), max(maxskill,p[pi]),x,sz+1] break if abs(sorteds[target-1][0] - L[i+1])<3: score1,minskill1,maxskill1,x1,sz1 = scores[sorteds[target-1][1]] sz1 = len(uf.members(sorteds[target-1][1])) # score1=1000 score2,minskill2,maxskill2,x2,sz2 = scores[pi] sz2 = len(uf.members(pi)) # score2=1000 if sz1 + sz2 <= 3: _score = clc_score((sz1+sz2),min(minskill1,minskill2),max(maxskill1,maxskill2),min(x1,x2),t) # _score = 100000000000000000000 if _score > score1 + score2: uf.union(pi, sorteds[target-1][1]) t_ans.add(pi) scores[sorteds[target-1][1]] = [_score, min(minskill1,minskill2),max(maxskill1,maxskill2),min(x1,x2),(sz1+sz2)] scores[pi] = [_score, min(minskill1,minskill2),max(maxskill1,maxskill2),min(x1,x2),(sz1+sz2)] # s.discard(sorteds[target-1]) if abs(sorteds[target][0] - L[i+1])<3: score1,minskill1,maxskill1,x1,sz1 = scores[sorteds[target][1]] # score1=1000 sz1 = len(uf.members(sorteds[target][1])) score2,minskill2,maxskill2,x2,sz2 = scores[pi] # score2 = 10000 sz2 = len(uf.members(pi)) if sz1 + sz2 <= 4: _score = clc_score((sz1+sz2),min(minskill1,minskill2),max(maxskill1,maxskill2),min(x1,x2),t) # _score=10000000000 if _score > score1 + score2: uf.union(pi, sorteds[target][1]) t_ans.add(pi) scores[sorteds[target][1]] = [_score, min(minskill1,minskill2),max(maxskill1,maxskill2),min(x1,x2),(sz1+sz2)] scores[pi] = [_score, min(minskill1,minskill2),max(maxskill1,maxskill2),min(x1,x2),(sz1+sz2)] # s.discard(sorteds[target]) s.add(nowi) pi+=1 t+=1 # print (t_ans) ans4print=[] ans4print_num=0 for ans in list(t_ans): m=uf.members(ans) if len(m)>=4: mls=[] for i in range(1,4):#len(m)) : mls.append([m[0],m[i]]) t_ans.discard(m[0]) t_ans.discard(m[i]) ans4print_num+=1 room4set.add(m[0]) room4set.add(m[i]) ans4print.append(mls) else: mls=[] for i in range (1,len(m)): mls.append([m[0],m[i]]) ans4print_num+=1 ans4print.append(mls) print(ans4print_num) sys.stdout.flush() for i in range (len(ans4print)): for e in ans4print[i]: print(*e) sys.stdout.flush()