백준 2529번 부등호 (Python 3)
2022. 9. 1. 15:44ㆍProblem solving
Baekjoon online judge 2529번 파이썬
링크
https://www.acmicpc.net/problem/2529
2529번: 부등호
두 종류의 부등호 기호 ‘<’와 ‘>’가 k개 나열된 순서열 A가 있다. 우리는 이 부등호 기호 앞뒤에 서로 다른 한 자릿수 숫자를 넣어서 모든 부등호 관계를 만족시키려고 한다. 예를 들어, 제시
www.acmicpc.net
permutations 이용해서 풀어도 10! = 3628800 개 만 확인하면 되기 때문에 풀린다.
백트래킹을 이용해서 풀었다.
지금까지의 숫자 문자열을 들고다니면서, 문자열의 길이가 k+1이 되었을 때 max,min 값을 갱신한다.
소스코드
import sys
input = sys.stdin.readline
now_min=sys.maxsize
now_max=-1
k=int(input())
op=list(map(str,input().split()))
chk=[False for _ in range(10)]
def f(s):
global now_max
global now_min
if len(s)==k+1:
now_max=max(now_max,int(s))
now_min=min(now_min,int(s))
return
for i in range(10):
if chk[i]:
continue
if len(s)==0 or eval(s[-1]+op[len(s)-1]+str(i)):
chk[i]=True
f(s+str(i))
chk[i]=False
f('')
print(str(now_max).zfill(k+1))
print(str(now_min).zfill(k+1))
'Problem solving' 카테고리의 다른 글
백준 17837번 새로운 게임 2 (Python 3) (0) | 2022.09.01 |
---|---|
백준 17780번 새로운 게임 (Python 3) (0) | 2022.09.01 |
백준 2003번 수들의 합 2 (Python 3) (0) | 2022.09.01 |
백준 16198번 에너지 모으기 (Python 3) (0) | 2022.09.01 |
백준 14225번 부분수열의 합 (Python 3) (0) | 2022.09.01 |