반응형
생각보다 어려웠던 문제
두개를 정렬해서 하려고 했다.
하나의 첫번째 인덱스에 해당하는 값을 다른 리스트에서 찾고, 그 위치 이후에 있는 값을 outarr에 저장해서 n-len(outarr)를 하려고 했다.
import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
l=[]
for _ in range(n):
l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[0])
seoryu=l[:]
stop=l[0]
l.sort(key=lambda x:x[1])
myeongeop=l[:]
mtop=l[0]
outarr=[]
for x in range(n):
if myeongeop[x]==stop:
outarr.extend(list(map(tuple,myeongeop[x+1:])))
break
for x in range(n):
if seoryu[x]==mtop:
outarr.extend(list(map(tuple,seoryu[x+1:])))
break
print(n-len(set(outarr)))
그러나 이후의 인덱스에도 이런 경우가 발생할 수 있다는 것을 알게됐다.
그래서 고민해보니 리스트 두 개를 사용하려면 아무리 생각해도 이중 for문을 사용해야 했다.
그러나 생각해보니 굳이 두개의 배열을 사용하지 않아도 충분히 비교가 가능했다. 왜냐면 점수가 아니고 랭킹이 주어졌으므로.
import sys
input=sys.stdin.readline
t=int(input().rstrip())
for _ in range(t):
n=int(input().rstrip())
res=n
l=[]
result=[]
for _ in range(n):
l.append(list(map(int,input().rstrip().split())))
l.sort(key=lambda x:x[0])
for x in l:
result.append(x[1])
last=float('inf')
for x in result:
if last>x:
last=x
else:
res-=1
print(res)
반응형