답안 #377409

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
377409 2021-03-14T07:34:04 Z NONAME Knjige (COCI20_knjige) C++17
50 / 50
5 ms 2024 KB
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int n;
vector <int> lft, rgt, need;
vector <array <string, 3> > res;

void shift(int x, vector <int>& v1, vector <int>& v2, int tp) {
    while ((int)(v1.size()) > (x + 1)) {
        res.push_back({"UZMI", "L", (tp ? "L" : "D")});
        res.push_back({"STAVI", "L", (tp ? "D" : "L")});
        v2.push_back(v1.back());
        v1.pop_back();
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;
    lft.reserve(n);
    rgt.reserve(n);
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        lft.push_back(x);
        need.push_back(x);
    }
    sort(need.rbegin(), need.rend());
    reverse(lft.begin(), lft.end());

    while (!lft.empty()) {
        res.push_back({"UZMI", "L", "L"});
        res.push_back({"STAVI", "L", "D"});
        rgt.push_back(lft.back());
        lft.pop_back();
    }

    // for (int i = 0; i < (int)(rgt.size()); ++i) {
    //     cerr << rgt[i] << " ";
    // }
    // cerr << "\n";

    for (auto& c : need) {
        int ps = -1;
        for (int i = 0; i < (int)(rgt.size()); ++i) {
            if (rgt[i] == c) {
                ps = i;
            }
        }

        int cnt = ((int)(rgt.size()) - 1) - ps;
        // cerr << ps << " " << cnt << "\n";
        shift(ps, rgt, lft, 0); // R -> L
        rgt.pop_back();
        res.push_back({"UZMI", "D", "D"});
        shift(((int)(lft.size()) - 1) - cnt, lft, rgt, 1); // L -> R
        lft.push_back(c);
        res.push_back({"STAVI", "D", "L"});
    }

    // for (int i = 0; i < (int)(lft.size()); ++i) {
    //     cerr << lft[i] << " ";
    // }
    // cerr << "\n";
    
    cout << (int)(res.size()) << "\n";
    for (int i = 0; i < (int)(res.size()); ++i) {
        cout << res[i][0] << " " << res[i][1] << " " << res[i][2] << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2024 KB Output is correct
2 Correct 4 ms 2024 KB Output is correct
3 Correct 4 ms 2024 KB Output is correct
4 Correct 4 ms 2024 KB Output is correct
5 Correct 4 ms 2024 KB Output is correct
6 Correct 4 ms 2024 KB Output is correct
7 Correct 3 ms 1260 KB Output is correct
8 Correct 4 ms 2024 KB Output is correct
9 Correct 5 ms 2024 KB Output is correct
10 Correct 4 ms 2024 KB Output is correct