ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1301-D, *2000
    코드포스 2021. 12. 24. 02:06
     

    Problem - D - Codeforces

     

    codeforces.com

    문제

    • 오일러 경로에 관련된 문제다

     

    풀이

    ※ m = 1 인 경우 따로 처리해줘야한다

    R ~ (m-1) 번
    L ~ (m-1) 번
    이후에는 다음 문자열이 (n-1) 번 반복된다
    D/RUD/RUD/ ... /RUD ~ RUD 가 (m-1)개
    LLL ... LLL ~ L 이 (m-1)개

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    #include <bits/stdc++.h>
    #define endl "\n"
    #define ooop(i, n) for(int i = 0; i < n; i++)
    #define loop(i, n) for(int i = 1; i <= n; i++)
    #define all(v) (v).begin(), (v).end()
     
    using namespace std;
    typedef long long ll;
    typedef pair<intint> pi;
    typedef pair<ll, ll> pl;
     
    int n, m, k;
    string cycle = "";
    string piece = "RUD";
     
    void make_cycle()
    {
        cycle += "D";
        loop(i, m-1) cycle += "RUD";
        loop(i, m-1) cycle += "L";
    }
     
    void sub_fill_in(vector<pair<intstring> >& res, int end// end [1, cycle.size())
    {
        res.emplace_back(1"D");
        end -= 1;
        if(end == 0return;
        if(end <= 3*(m-1)){
            int q = end/3;
            if(q == 0){
                res.emplace_back(1, piece.substr(0end - 3*q));
                return;
            }
            res.emplace_back(q, piece);
            end -= 3*q;
            if(end == 0return;
            res.emplace_back(1, piece.substr(0end));
            return;
        }
        else{
            res.emplace_back(m-1, piece);
            end -= 3*(m-1);
            if(end == 0return;
            res.emplace_back(end"L");
            return;
        }
     
    }
     
    void fill_in(vector<pair<intstring> >& res)
    {
        if(m == 1){
            if(k <= n-1){
                res.emplace_back(k, "D");
                return;
            }
            else{
                res.emplace_back(n-1"D");
                k -= n-1;
            }
            res.emplace_back(k, "U");
            return;
        }
     
        if(k <= m-1){
            res.emplace_back(k, "R");
            return;
        }
        else{
            res.emplace_back(m-1"R");
            k -= m-1;
        }
        if(k <= m-1){
            res.emplace_back(k, "L");
            return;
        }
        else{
            res.emplace_back(m-1"L");
            k -= m-1;
        }
        int q = k/cycle.size();
        if(q == n-1){
            loop(i, n-1){
                res.emplace_back(1"D");
                res.emplace_back(m-1, piece);
                res.emplace_back(m-1"L");
            }
            k -= q*cycle.size();
            if(k == 0return;
            else{
                res.emplace_back(k, "U");
                return;
            }
        }
        else{
            if(q == 0){
                sub_fill_in(res, k);
                return;
            }
            else{
                loop(i, q){
                    res.emplace_back(1"D");
                    res.emplace_back(m-1, piece);
                    res.emplace_back(m-1"L");
                }
                k -= q*cycle.size();
                if(k != 0) sub_fill_in(res, k);
                return;
            }
        }
    }
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        cin >> n >> m >> k;
        int cri = 4*n*m-2*n-2*m;
        if(k <= cri){
            cout << "YES" << endl;
            vector<pair<intstring> > res;
            make_cycle();
            fill_in(res);
            cout << res.size() << endl;
            for(auto [f, s]: res) cout << f << ' ' << s << endl;
        }
        else cout << "NO" << endl;
     
        return 0;

    '코드포스' 카테고리의 다른 글

    1301-B, *1500  (0) 2021.12.24
    1301-C, *1700  (0) 2021.12.24
    1295-D, *1800  (0) 2021.11.10
    1295-C, *1600  (0) 2021.11.10
    1295-B, *1700  (0) 2021.11.10

    댓글

Designed by Tistory.