結果

問題 No.1370 置換門松列
ユーザー MisterMister
提出日時 2021-01-29 21:52:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 2,720 bytes
コンパイル時間 954 ms
コンパイル使用メモリ 89,204 KB
実行使用メモリ 14,620 KB
最終ジャッジ日時 2023-10-12 21:00:45
合計ジャッジ時間 2,996 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 2 ms
4,356 KB
testcase_21 AC 32 ms
12,992 KB
testcase_22 AC 25 ms
14,620 KB
testcase_23 AC 29 ms
13,232 KB
testcase_24 AC 11 ms
6,212 KB
testcase_25 AC 43 ms
13,060 KB
testcase_26 AC 42 ms
13,040 KB
testcase_27 AC 19 ms
12,972 KB
testcase_28 AC 19 ms
13,060 KB
testcase_29 AC 13 ms
6,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/topological_sort.hpp"

#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp"

#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;

    Edge() = default;
    Edge(int src, int dst, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return cost > e.cost; }
};

template <class Cost = int>
struct Graph : public std::vector<std::vector<Edge<Cost>>> {
    using std::vector<std::vector<Edge<Cost>>>::vector;

    void span(bool direct, int src, int dst, Cost cost = 1) {
        (*this)[src].emplace_back(src, dst, cost);
        if (!direct) (*this)[dst].emplace_back(dst, src, cost);
    }
};
#line 4 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/topological_sort.hpp"

#include <algorithm>

template <class Cost = int>
struct TopologicalSort {
    Graph<Cost> graph;
    std::vector<bool> visited;
    std::vector<int> vs, id;

    explicit TopologicalSort(const Graph<Cost>& graph)
        : graph(graph), visited(graph.size(), false), id(graph.size()) {
        for (int v = 0; v < (int)graph.size(); ++v) dfs(v);
        std::reverse(vs.begin(), vs.end());

        for (int i = 0; i < (int)graph.size(); ++i) id[vs[i]] = i;
    }

    void dfs(int v) {
        if (visited[v]) return;
        visited[v] = true;
        for (const auto& e : graph[v]) dfs(e.dst);
        vs.push_back(v);
    }
};
#line 2 "main.cpp"
#include <iostream>
#line 5 "main.cpp"

using namespace std;

bool judge(int a, int b, int c) {
    if (a == c) return false;
    return (a < b && b > c) ||
           (a > b && b < c);
}

bool judge_all(const vector<int>& xs) {
    int n = xs.size();
    for (int i = 0; i + 2 < n; ++i) {
        if (!judge(xs[i], xs[i + 1], xs[i + 2])) return false;
    }
    return true;
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> xs(n);
    for (auto& x : xs) {
        cin >> x;
        --x;
    }

    Graph<> graph(m);
    for (int i = 0; i + 1 < n; ++i) {
        if (i % 2 == 0) {
            graph.span(true, xs[i], xs[i + 1]);
        } else {
            graph.span(true, xs[i + 1], xs[i]);
        }
    }

    TopologicalSort ts(graph);
    auto ys = ts.id;

    vector<int> zs(n);
    for (int i = 0; i < n; ++i) zs[i] = ys[xs[i]];
    if (!judge_all(zs)) {
        cout << "No\n";
        return;
    }

    cout << "Yes\n";
    for (auto y : ys) cout << y + 1 << " ";
    cout << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0