728x90

https://www.acmicpc.net/problem/1253

 

1253번: 좋다

첫째 줄에는 수의 개수 N(1 ≤ N ≤ 2,000), 두 번째 줄에는 i번째 수를 나타내는 Ai가 N개 주어진다. (|Ai| ≤ 1,000,000,000, Ai는 정수)

www.acmicpc.net

공유기와 비슷하게 함수를 만들고 하나씩 돌아가면서 확인하도록 만들었다.

 

import sys

N = int(sys.stdin.readline().strip())
A = list(map(int, sys.stdin.readline().split()))

A.sort()

result = 0


def isGood(goal):
    left, right = 0, N - 1

    while left < right:
        if A[left] + A[right] == A[goal]:
            if left == goal:
                left += 1
            elif right == goal:
                right -= 1
            else:
                return True
        elif A[left] + A[right] > A[goal]:
            right -= 1
        elif A[left] + A[right] < A[goal]:
            left += 1


for i in range(N):
    result += 1 if isGood(i) is True else 0

print(result)

 

'알고리즘 > 이분탐색' 카테고리의 다른 글

백준 2110 공유기 설치 (Python)  (0) 2024.03.10
백준 1654 랜선 자르기 (Python)  (0) 2024.03.10
백준 2805 나무 자르기 (Python)  (0) 2024.03.10

+ Recent posts