💻 현생/⛓ 알고리즘
[백준] 10972번 : 다음 순열
영이오
2021. 4. 14. 19:15
문제
풀이
myunji.tistory.com/19?category=1154147
[EPPER] 11회 6번
문제 풀이 접근을 잘못했던 문제. 하필이면 그 접근으로도 입력 예시는 전부 다 맞게 나와서 난 그게 맞는 줄 알았다. 근데 혹시나 하는 마음에 돌려본 테스트 케이스에서 오류를 발견했다. 먼저
myunji.tistory.com
비슷한 문제를 푼 적 있다.
이때는 알고리즘을 막 공부할 떄라 그렇게 효율적인 코드를 짠 것 같진 않지만 어떤식으로 푸는지에 대한 풀이는 알고 있으니까
www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_permutation
next_permutation - C++ Reference
function template std::next_permutation default (1)template bool next_permutation (BidirectionalIterator first, BidirectionalIterator last); custom (2)template bool next_permutation (BidirectionalIterator first, BidirectionalIterator last, Comp
www.cplusplus.com
난 이걸 쓰도록 하겠다. 정말 C++엔 별게 다 있는 것 같다.
소스코드
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N, input;
vector<int> arr;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> input;
arr.push_back(input);
}
if (next_permutation(arr.begin(), arr.end())) { //다음 순열이 있다면
for (int i = 0; i < N; i++)
cout << arr[i] << ' ';
} else //없다면
cout << -1;
}