結果
問題 | No.1888 Odd Insertion |
ユーザー |
![]() |
提出日時 | 2022-03-25 23:23:45 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,265 bytes |
コンパイル時間 | 1,224 ms |
コンパイル使用メモリ | 124,500 KB |
最終ジャッジ日時 | 2025-01-28 12:36:49 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 17 WA * 20 |
ソースコード
// ===== template.hpp ===== #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #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 <typename T> using Vec = vector<T>; template <typename T> bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } template <typename T> 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 <cassert> #include <vector> // ===== operations.hpp ===== #ifndef OPERATIONS_HPP #define OPERATIONS_HPP #include <limits> #include <utility> template <typename T> 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 <typename T> 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 <typename T> struct Min { using Value = T; static Value id() { return std::numeric_limits<T>::max(); } static Value op(const Value &lhs, const Value &rhs) { return std::min(lhs, rhs); } }; template <typename T> struct Max { using Value = T; static Value id() { return std::numeric_limits<Value>::min(); } static Value op(const Value &lhs, const Value &rhs) { return std::max(lhs, rhs); } }; template <typename T> 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 <typename Monoid> struct Reversible { using Value = std::pair<typename Monoid::Value, typename Monoid::Value>; 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 <typename CommutativeGroup> class FenwickTree { public: using Value = typename CommutativeGroup::Value; private: std::vector<Value> 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<i32> p(n); REP(i, n) { cin >> p[i]; --p[i]; } Vec<i32> pos(n); REP(i, n) { pos[p[i]] = i; } Vec<i32> mod2(n); FenwickTree<Add<i32>> fw(n); PER(i, n) { mod2[i] = (pos[i] - fw.sum(pos[i])) % 2; fw.add(pos[i], 1); } if (mod2[n - 1]) { cout << "No\n"; exit(0); } Vec<i32> pin(n, 0); Vec<i32> stc; i32 prv = 0; PER(i, n) { if (!mod2[i]) { if (i > 0 && mod2[i - 1]) { stc.clear(); } while (!stc.empty() && stc.back() < pos[i]) { stc.pop_back(); } stc.push_back(pos[i]); } else { i32 pinned = p[stc.back()]; pin[pinned] = 1; if (pos[pinned] > pos[i]) { cout << "No\n"; } } } Vec<pair<i32, i32>> ans; i32 it = n - 1; FenwickTree<Add<i32>> fw2(n); while (it >= 0) { if (pin[it]) { i32 nx = it - 1; while (nx >= 0 && !mod2[nx]) { ans.emplace_back(nx, pos[nx] - fw2.sum(pos[nx])); fw2.add(pos[nx], 1); --nx; } while (nx >= 0 && mod2[nx]) { ans.emplace_back(nx, pos[nx] - fw2.sum(pos[nx])); fw2.add(pos[nx], 1); --nx; } ans.emplace_back(it, pos[it] - fw2.sum(pos[it])); fw2.add(pos[it], 1); it = nx; } else { ans.emplace_back(it, pos[it] - fw2.sum(pos[it])); fw2.add(pos[it], 1); --it; } } reverse(ALL(ans)); cout << "Yes\n"; for (auto [i, po] : ans) { cout << i + 1 << ' ' << po + 1 << '\n'; } }