結果
問題 | No.1370 置換門松列 |
ユーザー | Mister |
提出日時 | 2021-01-29 21:52:06 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 41 ms / 2,000 ms |
コード長 | 2,720 bytes |
コンパイル時間 | 1,033 ms |
コンパイル使用メモリ | 88,740 KB |
実行使用メモリ | 14,624 KB |
最終ジャッジ日時 | 2024-09-14 19:19:12 |
合計ジャッジ時間 | 2,584 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | AC | 2 ms
6,944 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,944 KB |
testcase_14 | AC | 2 ms
6,944 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 2 ms
6,940 KB |
testcase_17 | AC | 2 ms
6,944 KB |
testcase_18 | AC | 2 ms
6,940 KB |
testcase_19 | AC | 2 ms
6,944 KB |
testcase_20 | AC | 2 ms
6,940 KB |
testcase_21 | AC | 33 ms
13,192 KB |
testcase_22 | AC | 25 ms
14,624 KB |
testcase_23 | AC | 31 ms
13,348 KB |
testcase_24 | AC | 12 ms
6,940 KB |
testcase_25 | AC | 41 ms
13,136 KB |
testcase_26 | AC | 41 ms
13,188 KB |
testcase_27 | AC | 20 ms
13,048 KB |
testcase_28 | AC | 21 ms
13,064 KB |
testcase_29 | AC | 14 ms
7,168 KB |
ソースコード
#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; }