티스토리 뷰


>> 문제 바로가기 (https://www.acmicpc.net/problem/25181)

풀이 과정
특정 i번째 숫자 앞뒤로, 바꿨을 때 Ai != Bj && Aj != Bi 를 만족하는 j번째 숫자를 찾아 바꾸면 된다.
import sys
input = sys.stdin.readline
n = int(input().rstrip())
arr = list(map(int, input().rstrip().split()))
narr = []
for a in arr: narr.append(a)
done = True
for i in range(n):
if arr[i] == narr[i]:
idx = i + 1
while idx < n and narr[i] == narr[idx]: idx += 1
if idx == n:
idx = i - 1
while idx >= 0 and (narr[idx] == arr[i] or narr[i] == arr[idx]): idx -= 1
if idx == -1:
done = False
break
tmp = narr[i]
narr[i] = narr[idx]
narr[idx] = tmp
if done == True: print(' '.join(map(str, narr)))
else: print(-1)'(예전 글) > PS' 카테고리의 다른 글
| [ BOJ ] 1929 : 소수 구하기 ( SILVER 3 ) / Python (0) | 2024.03.10 |
|---|---|
| [ BOJ ] 16112 : 5차 전직 ( SILVER 2 ) / Python (0) | 2024.03.10 |
| [ BOJ ] 23845 : 마트료시카 ( GOLD 3 ) / Python (0) | 2024.02.17 |
| [ BOJ ] 17141 : 연구소 2 ( GOLD 4 ) / Python (1) | 2024.02.17 |
| [ BOJ ] 1025 : 제곱수 찾기 ( GOLD 5 ) / Python (0) | 2024.02.17 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- lca
- string
- C++
- Greedy
- Sort
- math
- BFS
- Binary-Search
- ad_hoc
- stack
- Python
- kmp
- codeup
- bitmask
- number_theory
- lazy-propagation
- segment-tree
- Prefix-Sum
- DP
- 백준
- bruteforcing
- BOJ
- PS
- implementation
- queue
- sparse_table
- C
- backtracking
- knapsack
- java
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
글 보관함
