// ===== template.hpp ===== #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define OVERRIDE(a, b, c, d, ...) d #define REP2(i, n) for (i32 i = 0; i < (i32) (n); ++i) #define REP3(i, m, n) for (i32 i = (i32) (m); i < (i32) (n); ++i) #define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__) #define PER(i, n) for (i32 i = (i32) (n) - 1; i >= 0; --i) #define ALL(x) begin(x), end(x) using namespace std; using u32 = unsigned int; using u64 = unsigned long long; using u128 = __uint128_t; using i32 = signed int; using i64 = signed long long; using i128 = __int128_t; template using Vec = vector; template bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } template bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; } [[maybe_unused]] constexpr i32 inf = 1000000100; [[maybe_unused]] constexpr i64 inf64 = 3000000000000000100; struct SetIO { SetIO() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(10); } } set_io; // ===== template.hpp ===== #ifdef DEBUGF #include "../new_library/other/debug.hpp" #else #define DBG(x) (void) 0 #endif // ===== fenwick_tree.hpp ===== #ifndef FENWICK_TREE_HPP #define FENWICK_TREE_HPP #include #include // ===== operations.hpp ===== #ifndef OPERATIONS_HPP #define OPERATIONS_HPP #include #include template struct Add { using Value = T; static Value id() { return T(0); } static Value op(const Value &lhs, const Value &rhs) { return lhs + rhs; } static Value inv(const Value &x) { return -x; } }; template struct Mul { using Value = T; static Value id() { return Value(1); } static Value op(const Value &lhs, const Value &rhs) { return lhs * rhs; } static Value inv(const Value &x) { return Value(1) / x; } }; template struct Min { using Value = T; static Value id() { return std::numeric_limits::max(); } static Value op(const Value &lhs, const Value &rhs) { return std::min(lhs, rhs); } }; template struct Max { using Value = T; static Value id() { return std::numeric_limits::min(); } static Value op(const Value &lhs, const Value &rhs) { return std::max(lhs, rhs); } }; template struct Xor { using Value = T; static Value id() { return T(0); } static Value op(const Value &lhs, const Value &rhs) { return lhs ^ rhs; } static Value inv(const Value &x) { return x; } }; template struct Reversible { using Value = std::pair; static Value id() { return Value(Monoid::id(), Monoid::id()); } static Value op(const Value &v1, const Value &v2) { return Value( Monoid::op(v1.first, v2.first), Monoid::op(v2.second, v1.second)); } }; #endif // ===== operations.hpp ===== template class FenwickTree { public: using Value = typename CommutativeGroup::Value; private: std::vector data; public: FenwickTree(std::size_t n) : data(n, CommutativeGroup::id()) {} void add(std::size_t idx, const Value &x) { assert(idx < data.size()); for (; idx < data.size(); idx |= idx + 1) { data[idx] = CommutativeGroup::op(data[idx], x); } } Value sum(std::size_t r) const { assert(r <= data.size()); Value ret = CommutativeGroup::id(); for (; r > 0; r &= r - 1) { ret = CommutativeGroup::op(ret, data[r - 1]); } return ret; } Value sum(std::size_t l, std::size_t r) const { assert(l <= r && r <= data.size()); return CommutativeGroup::op(sum(r), CommutativeGroup::inv(sum(l))); } }; #endif // ===== fenwick_tree.hpp ===== int main() { i32 n; cin >> n; Vec p(n); REP(i, n) { cin >> p[i]; --p[i]; } Vec pos(n); REP(i, n) { pos[p[i]] = i; } Vec ord; ord.reserve(n); i32 last = n; i32 trouble_min_pos = n; FenwickTree> fw2(n); PER(i, n) { i32 po = pos[i] - fw2.sum(pos[i]); fw2.add(pos[i], 1); if (pos[i] < trouble_min_pos && po % 2 == 0) { ord.emplace_back(i); for (i32 j = last - 1; j > i; --j) { ord.emplace_back(j); } last = i; trouble_min_pos = n; continue; } chmin(trouble_min_pos, pos[i]); } if (last != 0) { cout << "No\n"; exit(0); } DBG(ord); FenwickTree> fw3(n); Vec> ans; ans.reserve(n); for (i32 i : ord) { i32 po = pos[i] - fw3.sum(pos[i]); if (po % 2) { cout << "No\n"; exit(0); } fw3.add(pos[i], 1); ans.emplace_back(i, po); } /*reverse(ALL(ans)); cout << "Yes\n"; for (auto [i, po] : ans) { cout << i + 1 << ' ' << po + 1 << '\n'; }*/ exit(1); }