Baekjoon 11970번 [Fence Painting]
https://www.acmicpc.net/problem/11970
11970번: Fence Painting
Several seasons of hot summers and cold winters have taken their toll on Farmer John's fence, and he decides it is time to repaint it, along with the help of his favorite cow, Bessie. Unfortunately, while Bessie is actually remarkably proficient at paintin
www.acmicpc.net
- 사용언어: Python (PyPy3)
- 알고리즘: 수학, 구현
문제
코드-1
a, b = map(int, input().split())
c, d = map(int, input().split())
John = b-a
if d < a or b < c: # 겹치는 부분이 없는 경우
Bessie = d-c
elif a<=c and d<=b: # 전부 다 겹치는 경우
Bessie = 0
elif c < a and a <= d <= b: # 소가 칠한 부분의 왼쪽이 겹치지 않는 경우
Bessie = a-c
elif a <= c <= b and b < d: # 소가 칠한 부분의 오른쪽이 겹치지 않는 경우
Bessie = d-b
else: # 소가 칠한 부분의 양쪽이 겹치지 않는 경우
Bessie = (a-c) + (d-b)
ans = John + Bessie
print(ans)
코드-2
- 다시 보니까 더 간단하게 풀 수 있을 것 같아서 풀어봄
a, b = map(int, input().split())
c, d = map(int, input().split())
if d < a or b < c: # 겹치는 부분이 없는 경우
ans = b-a + d-c
else: # 겹치는 부분이 있는 경우
min_f, max_f = min(a, b, c, d), max(a, b, c, d)
ans = max_f-min_f
print(ans)
'백준 > USACO bronze 기출' 카테고리의 다른 글
[백준] 14530번 The Lost Cow (0) | 2022.10.03 |
---|---|
[백준] 14175번 The Cow-Signal (0) | 2022.10.03 |
[백준] 15751번 Teleportation (0) | 2022.10.03 |
[백준] 10675번 Cow Routing (0) | 2022.10.03 |
[백준] 14173번 Square Pasture (0) | 2022.10.02 |