백준 알고리즘 2947번 : 나무 조각

Zoo Da·2022년 2월 3일
0

백준 알고리즘

목록 보기
313/337
post-thumbnail

링크

https://www.acmicpc.net/problem/2947

sol1) STL

#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/rope>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
using namespace __gnu_cxx;

#define X first
#define Y second
#define int int64_t
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define Compress(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define OOB(x, y) ((x) < 0 || (x) >= n || (y) < 0 || (y) >= m)
#define IDX(v, x) (lower_bound(all(v), x) - (v).begin())
#define debug(x) cout << (#x) << ": " << (x) << '\n'

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tii = tuple<int, int, int>;
template <typename T>
using wector = vector<vector<T>>;

void Print(vector<int> &v)
{
    for (const auto &c : v)
        cout << c << ' ';
    cout << "\n";
}

int32_t main()
{
    fastio;
    vector<int> v(5);
    for (auto &i : v)
        cin >> i;
    while (!is_sorted(all(v)))
    {
        for (int i = 1; i < 5; i++)
        {
            if (v[i - 1] > v[i])
            {
                swap(v[i - 1], v[i]);
                Print(v);
            }
        }
    }
}

문제에서 요구한 대로 swap이 일어날 때마다 출력을 해주면서 정렬이 될 때
까지 반복해주면 됩니다.
is_sorted stl은 정렬이 되었는지 안되었는지를 판단할 수 있는 stl입니다.

profile
메모장 겸 블로그

0개의 댓글