-
[21554] 마법의 돌 장난감BOJ 2022. 2. 22. 20:34
https://www.acmicpc.net/problem/21554
21554번: 마법의 돌 장난감
만약 100번 이하의 ‘조작’으로 장난감을 정렬하는 것이 불가능하다면, $-1$을 출력하고 프로그램을 종료합니다. 만약 장난감을 정렬하는 것이 가능하다면, 첫 줄에는 ‘조작’의 횟수 $Q$를 출
www.acmicpc.net
팬케이크 정렬이라는 이름으로 불리는 듯 하다
#include <bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGEconstexpr bool local = false;#elseconstexpr bool local = true;#endifusing ll = long long;using pi = pair<ll, ll>;int n;vector<int> v;vector<pi> ans;int main(void) {if (!local) ios_base::sync_with_stdio(0), cin.tie(0);cin >> n;v.resize(n);int i, j;for (i = 0; i < n; i++) cin >> v[i];for (i = 0; i < n; i++) {if (v[i] == i + 1) continue;j = find(v.begin(), v.end(), i + 1) - v.begin();ans.push_back({i + 1, j + 1});reverse(v.begin() + i, v.begin() + j + 1);}for (i = 0; i < n; i++) assert(v[i] == i + 1);cout << ans.size() << '\n';for (auto& i : ans) cout << i.first << ' ' << i.second << '\n';return 0;}
'BOJ' 카테고리의 다른 글
[16120] PPAP (0) 2022.02.24 [3991] 한번 쏘면 멈출 수 없어 (0) 2022.02.23 [2445] 별 찍기 - 8 (0) 2022.02.22 [20291] 파일 정리 (0) 2022.02.22 [10728] XOR삼형제 1 (0) 2022.02.21