백준 algorithm

백준 10973 - 이전 순열

cosmohoo 2020. 6. 5. 22:23
반응형

문제 설명

 

 

=> 이전에 풀었던 다음 순열과 같은 문제입니다.

=> prev_permutation() 함수를 사용할 줄 안다면 쉽게 풀 수 있는 문제입니다. 

 

https://codingham.tistory.com/145

 

백준 10972 - 다음 순열

=> STL의 Algorithm 헤더에는 next_permutation과 prev_permutation 함수가 있습니다. (Library) => 해당 함수를 통해 구현할 수 있는 간단한 문제입니다. => 시간이 될 때 직접 구현해보는 방향으로 다시 풀어보..

codingham.tistory.com

 

=> 위의 포스팅을 확인하시면 쉽게 알 수 있습니다. 

 

 

 

<code>

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>


using namespace std;


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    vector<int> arr;
       int n;
       cin >> n;
       
       for(int i = 0; i < n; i++){
           int tmp;
           cin >> tmp;
           arr.push_back(tmp);
       }
       
       
    if(prev_permutation(arr.begin(), arr.end()))
    {
        for(int i = 0; i < n; i++)
        {
            cout << arr[i]<<" ";
        }
    }
    else{
        cout << -1<<"\n";
    }
    
    return 0;
}

 

 

 

 

 

 

실행 화면

반응형