IT/ps
백준 2592번 부등호
u149_cinderella
2025. 2. 8. 13:56
단순하게 0부터 9까지의 n길이를 가지는 순열을 만들어서 검사하면 될듯하다
import sys
n=int(sys.stdin.readline().rstrip())
str_com=list(sys.stdin.readline().rstrip().split())
stack=[]
v=[0]*10
def inspect():
result=True
for i in range(n):
if str_com[i]=='>':
if stack[i]>stack[i+1]:
continue
else:
result=False
break
else:
if stack[i]<stack[i+1]:
continue
else:
result=False
break
return result
def dfs():
if len(stack)==n+1:
if inspect()==True:
print(*stack,sep='')
return
for i in range(10):
if v[i]==1:
continue
v[i]=1
stack.append(i)
dfs()
stack.pop()
v[i]=0
dfs()
그래서 이렇게 작성했는데 문제를 보니 최대 최소값만 출력하라고 되어있다.
조금만 더 수정하면 될듯하다.
단순하게 제일 처음 숫자와 제일 마지막 숫자만 출력하면 될듯한데
import sys
n=int(sys.stdin.readline().rstrip())
str_com=list(sys.stdin.readline().rstrip().split())
def inspect():
result=True
for i in range(n):
if str_com[i]=='>':
if stack[i]>stack[i+1]:
continue
else:
result=False
break
else:
if stack[i]<stack[i+1]:
continue
else:
result=False
break
return result
check=[0]
stack=[]
v=[0]*10
def dfs():
if len(stack)==n+1:
if inspect()==True:
print(*stack,sep='')
check[0]=1
return
for i in range(9,-1,-1):
if v[i]==1:
continue
v[i]=1
stack.append(i)
if check[0]==1:
return
dfs()
stack.pop()
v[i]=0
def dfs2():
if len(stack)==n+1:
if inspect()==True:
print(*stack,sep='')
check[0]=1
return
for i in range(10):
if v[i]==1:
continue
v[i]=1
stack.append(i)
if check[0]==1:
return
dfs2()
stack.pop()
v[i]=0
dfs()
stack=[]
v=[0]*10
check[0]=0
dfs2()
그냥 제일 높은거 뽑는 함수 제일 낮은거 뽑는 함수 2개 작성했다