Created
January 20, 2026 08:09
-
-
Save maehrm/f6d007989bc0564dd32a7319e6316524 to your computer and use it in GitHub Desktop.
C - Previous Permutation https://atcoder.jp/contests/abc276/tasks/abc276_c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| N = int(input()) | |
| P = list(map(int, input().split())) | |
| # (1) 末尾の昇順区間を探す | |
| i = N - 1 | |
| while i > 0 and P[i - 1] < P[i]: | |
| i -= 1 | |
| # (2) jは、昇順の始まりのひとつ左(交換対象) | |
| j = i - 1 | |
| # (3) 交換対象を探す | |
| k = i | |
| while k < N and P[j] > P[k]: | |
| k += 1 | |
| k -= 1 | |
| # (4) 交換 | |
| P[j], P[k] = P[k], P[j] | |
| # (5) 右側を降順に | |
| P[j + 1 :] = P[:j:-1] | |
| print(*P) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment