728x90
반응형
https://www.acmicpc.net/problem/2831
문제 해결
- 음수-양수, 양수-음수만 짝이 가능하다.
- 음수쪽 절대값이 더 커야한다.
- sort()를 통해 수를 배열하고 음수인 부분이 더 클 경우에 짝을 만들어준다.
- 남자는 가장 작은수부터, 여자는 가장 큰 수부터 시작해서 짝을 맞춰본다.(절대값이 큰것끼리 짝을 맞춰야 더 많이 짝을 만들 수 있다.)
CODE
n = int(input())
M = list(map(int, input().split()))
W = list(map(int, input().split()))
M.sort()
W.sort()
answer = 0
start = 0
end = n-1
while start<n and 0<=end:
if M[start]<0 and 0<W[end] and abs(M[start])>W[end]:
answer += 1
start += 1; end -= 1
elif M[start]<0 and 0<W[end] and abs(M[start])<=W[end]:
end -= 1
elif 0<M[start] and W[end]<0 and M[start] < abs(W[end]):
answer += 1
start +=1; end -= 1
elif 0<M[start] and W[end]<0 and M[start] >= abs(W[end]):
end -= 1
elif M[start]<0 and W[end]<0:
start += 1
elif 0<M[start] and 0<W[end]:
end -= 1
print(answer)
728x90
반응형
'알고리즘 > [python] 백준 BOJ' 카테고리의 다른 글
[python] 백준 1132 합 (0) | 2023.05.07 |
---|---|
[python] 백준 2666 벽장문의 이동 (0) | 2023.05.06 |
[python] 백준 13144 List of Unique Numbers (0) | 2023.05.06 |
[python] 백준 1062 가르침 (0) | 2023.05.05 |
[python] 백준 1562 계단 수 (0) | 2023.05.05 |
댓글