結果

問題 No.1888 Odd Insertion
ユーザー ForestedForested
提出日時 2022-03-25 23:23:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,265 bytes
コンパイル時間 1,777 ms
コンパイル使用メモリ 127,316 KB
実行使用メモリ 10,312 KB
最終ジャッジ日時 2024-04-22 08:21:33
合計ジャッジ時間 9,293 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 72 ms
10,188 KB
testcase_07 AC 71 ms
10,180 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 70 ms
10,312 KB
testcase_17 WA -
testcase_18 AC 71 ms
10,056 KB
testcase_19 AC 68 ms
10,184 KB
testcase_20 AC 70 ms
10,188 KB
testcase_21 AC 30 ms
6,784 KB
testcase_22 AC 70 ms
10,312 KB
testcase_23 AC 31 ms
6,528 KB
testcase_24 AC 31 ms
6,656 KB
testcase_25 AC 70 ms
10,060 KB
testcase_26 WA -
testcase_27 AC 57 ms
10,052 KB
testcase_28 WA -
testcase_29 AC 29 ms
6,784 KB
testcase_30 AC 28 ms
6,784 KB
testcase_31 AC 27 ms
6,784 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// ===== 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';
    }
}
0