백준 1059

김당찬·2022년 4월 17일
0

문제 : https://www.acmicpc.net/problem/1059
아이디어 : 새로운 수를 넣고 정렬, n의 직전 수와 직후 수까지의 간격을 바탕으로 개수 구하기

# 변수
import sys
L = int(sys.stdin.readline())
S = [int(s) for s in sys.stdin.readline().split()]
n = int(sys.stdin.readline())
# Code
if n in S:
    print(0)
else:
    S.append(n)
    S.sort()
    i = S.index(n)
    if i != 0:
        a = n - S[i-1] # 이전 수까지 간격
        b = S[i+1] - n # 다음 수까지 간격
        print(a*b -1)
    else: # n이 S의 최소보다 작은경우
        print(n*(S[1]-n)-1)
profile
블로그 이사했습니다 https://ddangchani.github.io

0개의 댓글