티스토리 뷰
728x90
문제 링크
https://www.acmicpc.net/problem/25287
25287번: 순열 정렬
$1$부터 $N$까지의 정수를 임의로 배열한 순열은 총 $N! = N\times(N-1)\times(N-2)\times\cdots\times1$가지가 있다. 예를 들어 $1$부터 $3$까지의 수를 임의로 배열한 순열은 $\lbrace1,2,3\rbrace, \lbrace1,3,2\rbrace, \lbra
www.acmicpc.net
풀이
앞에서부터 탐색하며 각 자리의 수를 결정합니다. $i$와 $N-i+1$ 중, 앞자리의 수보다 크면서, 더 작은 수를 선택하면 됩니다.
정답 코드
// @BOJ ------------------------------------------ const fs = require('fs'); const stdin = fs.readFileSync('/dev/stdin').toString().split('\n'); const input = (() => { let line = 0; const input = () => stdin[line++]; input.num = () => input().split(' ').map(Number); input.rows = l => Array(l).fill().map(input); input.rows.num = l => Array(l).fill().map(input.num); return input; })(); // Solution ----------------------------------- const chunk = (arr, size) => { const ret = []; for (let i = 0; i < arr.length; i += size) { ret.push(arr.slice(i, i + size)); } return ret; }; const nonDecreaseOrderReducer = (prev, candidates) => Math.min(...candidates.filter(v => v >= prev)); const solve = (n, seq) => { const res = seq .map(v => [v, n - v + 1]) .reduce(nonDecreaseOrderReducer, -Infinity); if (res !== Infinity) return 'YES'; return 'NO'; }; const solution = function () { const t = +input(); const ans = chunk(input.rows.num(t * 2), 2) .map(([[n], seq]) => solve(n, seq)) .join('\n'); console.log(ans); }; solution();
728x90
'알고리즘 > 문제 풀이' 카테고리의 다른 글
BOJ 25289 - 가장 긴 등차 부분 수열 (Javascript) (0) | 2022.06.19 |
---|---|
BOJ 25288 - 영어 시험 (Javascript) (0) | 2022.06.19 |
BOJ 25286 - 11월 11일 (Javascript) (0) | 2022.06.19 |
BOJ 25285 - 심준의 병역판정검사 (Javascript) (0) | 2022.06.19 |
[BOJ] 백준 22983 - 조각 체스판 (SUAPC 2021 Summer) (0) | 2021.09.12 |