728x90
반응형
https://www.acmicpc.net/problem/3649
3649번: 로봇 프로젝트
각 테스트 케이스마다 한 줄에 하나씩, 구멍을 완벽하게 막을 수 있는 두 조각이 없다면 'danger'를 출력한다. 막을 수 있는 경우에는 'yes ℓ1 ℓ2'를 출력한다. (ℓ1 ≤ ℓ2) 정답이 여러 개인 경우에
www.acmicpc.net
최종 코드
GitHub - hwayeon351/BEAKJOON-Algorithms: 백준 알고리즘 소스 코드 모음
백준 알고리즘 소스 코드 모음. Contribute to hwayeon351/BEAKJOON-Algorithms development by creating an account on GitHub.
github.com
import sys
input = sys.stdin.readline
while True:
try:
xx = int(input())*(10**7)
n = int(input())
lego = []
for _ in range(n):
lego.append(int(input()))
lego.sort()
check = False
left = 0
right = n-1
while left < right:
sum = lego[left] + lego[right]
if sum == xx:
check = True
print('yes', lego[left], lego[right])
break
elif sum < xx:
left += 1
elif sum > xx:
right -= 1
if not check: print('danger')
except: break
풀이 과정
풀이 시간 40분
import sys
input = sys.stdin.readline
while True:
try:
#xcm을 나노로 변환
xx = int(input())*(10**7)
n = int(input())
lego = []
for _ in range(n):
lego.append(int(input()))
#레고를 길이 순으로 정렬
lego.sort()
#두 레고 조각의 길이 합 = xx가 되는 경우가 있으면 True, 없으면 False
check = False
left = 0
right = n-1
while left < right:
sum = lego[left] + lego[right]
#두 레고 조각의 길이 합 = xx인 경우, 해당 경우가 가장 큰 차를 가지므로 답 출력
if sum == xx:
check = True
print('yes', lego[left], lego[right])
break
#두 레고 조각의 길이 합이 xx보다 작은 경우, left 포인터를 증가시킨다
elif sum < xx:
left += 1
#두 레고 조각의 길이 합이 xx보다 큰 경우, right 포인터를 감소시킨다
elif sum > xx:
right -= 1
#구멍을 막을 레고 조각이 없는 경우, danger 출력
if not check: print('danger')
except: break
#시간복잡도 = O(T*N), 공간복잡도 = O(T*N)
728x90
반응형
'코테 노트 > 백준' 카테고리의 다른 글
백준 17143 낚시왕 C++ (0) | 2021.10.05 |
---|---|
백준 16472 고냥이 Python3 (0) | 2021.10.04 |
백준 19237 어른 상어 C++ (4) | 2021.10.04 |
백준 17822 원판 돌리기 C++ (0) | 2021.10.03 |
백준 3686 성냥개비 Python 3 (0) | 2021.10.02 |