Submission #979087

#TimeUsernameProblemLanguageResultExecution timeMemory
979087peterandvoiXylophone (JOI18_xylophone)C++17
100 / 100
141 ms1520 KiB
#include <bits/stdc++.h> #include "xylophone.h" using namespace std; #ifdef ngu #include "debug.h" #else #define debug(...) 42 #endif void solve(int N) { vector<int> res, t(N), pos(N + 1), delta(N), dist(N + 1); res = {1, 2}; pos[1] = 0; pos[2] = 1; t[0] = query(1, 2); auto get = [&](int l, int r) { if (l == r) { return 0; } if (l > r) { swap(l, r); } return t[r - 1] - (l == 0 ? 0 : t[l - 1]); }; auto Right = [&](int u, int delta) { int ans = -1; for (int i = u; i < int(res.size()); ++i) { if (get(u, i) > delta) { break; } ans = i + 1; } return ans; }; auto Left = [&](int u, int delta) { int ans = -1; for (int i = u; i >= 0; --i) { if (get(u, i) > delta) { break; } ans = i; } return ans; }; for (int i = 3; i <= N; ++i) { int a = pos[i - 2], b = pos[i - 1]; for (int j = 1; j < i; ++j) { dist[j] = get(b, pos[j]); } int j = -1; int AB = dist[i - 2], BC = query(i - 1, i), AC = query(i - 2, i); dist[i] = BC; if (a < b) { if (AB + BC == AC) { j = Right(b, BC); } else { j = Left(b, BC); } } else { if (AB + BC == AC) { j = Left(b, BC); } else { j = Right(b, BC); } } assert(j != -1); res.insert(res.begin() + j, i); auto reset = [&]() { for (int i = 0; i < int(res.size()); ++i) { pos[res[i]] = i; } for (int i = 0; i + 1 < int(res.size()); ++i) { t[i] = abs(dist[res[i]] - dist[res[i + 1]]); if (i > 0) { t[i] += t[i - 1]; } } }; reset(); } assert(int(res.size()) == N); if (res[0] > res.back()) { reverse(res.begin(), res.end()); } for (int i = 0; i < N; ++i) { answer(res[i], i + 1); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...