Algorithm (PS)

[백준] 1253번 : 좋다 (파이썬/Python)

minjiwoo 2022. 12. 9. 16:43
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

ㅎㅎ 이분탐색으로 쉽게 풀리는 문제이다 ~

다만 target 이 되는 숫자는 당연히 조합에 사용하지 못하므로 index를 조절해주는 것이 필요하다 ~~ 

n = int(input())
array = list(map(int, input().split()))
array.sort()
answer = 0

def solve(target):
    global answer
    left, right = 0, n - 1
    while left < right:
        if i == left:
            left += 1
            continue
        if i == right:
            right -= 1
            continue
        temp = array[left] + array[right]
        if target > temp:
            left += 1
        elif target < temp:
            right -= 1
        else:  # target == temp
            return True
    return False

for i in range(n):
    target = array[i]
    if solve(target):
        answer += 1
print(answer)
728x90