結果

問題 No.1061 素敵な数列
ユーザー MisterMister
提出日時 2020-05-03 19:13:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 3,588 bytes
コンパイル時間 1,229 ms
コンパイル使用メモリ 110,848 KB
実行使用メモリ 10,112 KB
最終ジャッジ日時 2024-04-10 09:55:00
合計ジャッジ時間 4,550 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,948 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 11 ms
6,944 KB
testcase_20 AC 10 ms
6,940 KB
testcase_21 AC 21 ms
6,940 KB
testcase_22 AC 37 ms
9,216 KB
testcase_23 AC 29 ms
7,808 KB
testcase_24 AC 29 ms
7,936 KB
testcase_25 AC 29 ms
7,808 KB
testcase_26 AC 8 ms
6,940 KB
testcase_27 AC 17 ms
6,940 KB
testcase_28 AC 20 ms
6,944 KB
testcase_29 AC 27 ms
7,680 KB
testcase_30 AC 33 ms
8,704 KB
testcase_31 AC 19 ms
6,940 KB
testcase_32 AC 41 ms
10,112 KB
testcase_33 AC 8 ms
6,944 KB
testcase_34 AC 11 ms
6,940 KB
testcase_35 AC 10 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <vector>
#include <cassert>

bool judge(const std::vector<int>& xs) {
    int n = xs.size() / 3;
    std::vector<std::vector<int>> idxs(n, std::vector<int>(3, -1));

    bool ret = true;
    for (int i = 0; i < n * 3; ++i) {
        auto& is = idxs[xs[i]];
        for (int j = 0; j < 3; ++j) {
            if (is[j] == -1) {
                is[j] = i;
                break;
            }
        }

        if (is.back() == -1) continue;
        auto d = std::abs((is[1] - is[0]) - (is[2] - is[1]));
        if (d != xs[i]) {
            ret = false;
            break;
        }
    }

    return ret;
}

void brute() {
    int l, n;
    std::cin >> l >> n;

    std::vector<int> xs(n * 3);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 3; ++j) {
            xs[i * 3 + j] = l + i;
        }
    }

    do {
        if (!judge(xs)) continue;
        for (auto x : xs) std::cout << x << " ";
        std::cout << std::endl;
        break;
    } while (std::next_permutation(xs.begin(), xs.end()));
}

const std::vector<std::vector<int>>
    normal_sol{
        {},
        {0, 0, 0},
        {},
        {2, 2, 1, 1, 2, 1, 0, 0, 0},
        {3, 3, 0, 0, 0, 3, 2, 2, 1, 1, 2, 1},
        {0, 0, 0, 1, 1, 2, 1, 3, 4, 4, 2, 3, 3, 2, 4}},
    sep_sol{
        {},
        {},
        {1, -1, 1, 1, 0, 0, 0},
        {1, -1, 1, 0, 2, 1, 0, 2, 2, 0},
        {3, -1, 3, 1, 1, 2, 1, 3, 2, 2, 0, 0, 0},
    };

void solve() {
    int n;
    std::cin >> n;

    if (n == 2) {
        std::cout << -1 << std::endl;
        return;
    }

    int l = 0, r = n * 3 - 1;
    std::vector<int> ans(n * 3, -1);
    bool sep = false;

    while (n > (sep ? 4 : 5)) {
        if (!sep) {
            if (n % 2 != 0) {
                // 665544654...
                for (int x = n - 1; x >= n / 2; --x) {
                    ans[l] = ans[l + 1] = ans[l + x + 2] = x;
                    l += 2;
                }
                l += n - n / 2;
                n = n / 2;

            } else {
                // 77665544376543.3...
                for (int x = n - 1; x >= n / 2; --x) {
                    ans[l] = ans[l + 1] = ans[l + 1 + x + 1] = x;
                    l += 2;
                }
                {
                    int x = n / 2 - 1;
                    ans[l] = ans[l + x + 2] = ans[l + x + 4] = x;
                    l += 1;
                }
                l += n - (n / 2 - 1);
                n = n / 2 - 1;
                sep = true;
            }
        } else {
            if (n % 2 != 0) {
                // ...456445566
                for (int x = n - 1; x >= n / 2; --x) {
                    ans[r] = ans[r - 1] = ans[r - x - 2] = x;
                    r -= 2;
                }
                r -= n - n / 2;
                n = n / 2;

            } else {
                // 7x66554477654...
                ans[l] = ans[l + n] = ans[l + n + 1] = n - 1;
                l += 2;
                for (int x = n - 2; x >= n / 2; --x) {
                    ans[l] = ans[l + 1] = ans[l + 1 + x + 1] = x;
                    l += 2;
                }
                l += n - n / 2 + 1;
                n = n / 2;
                sep = false;
            }
        }
    }

    auto sol = (!sep ? normal_sol : sep_sol);
    for (auto x : sol[n]) {
        if (x != -1) ans[l] = x;
        ++l;
    }

    for (auto a : ans) std::cout << a << " ";
    std::cout << std::endl;

    assert(judge(ans));
}

int main() {
    // brute();
    solve();
    return 0;
}
0