結果

問題 No.1888 Odd Insertion
ユーザー ForestedForested
提出日時 2022-03-25 23:41:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,821 bytes
コンパイル時間 1,436 ms
コンパイル使用メモリ 126,644 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2024-04-22 08:45:49
合計ジャッジ時間 8,703 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 39 ms
9,088 KB
testcase_07 AC 39 ms
9,088 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 39 ms
8,960 KB
testcase_17 WA -
testcase_18 AC 40 ms
8,960 KB
testcase_19 AC 39 ms
8,960 KB
testcase_20 AC 39 ms
8,960 KB
testcase_21 AC 39 ms
8,960 KB
testcase_22 AC 40 ms
8,960 KB
testcase_23 AC 40 ms
8,960 KB
testcase_24 AC 39 ms
8,960 KB
testcase_25 AC 40 ms
8,960 KB
testcase_26 AC 60 ms
10,368 KB
testcase_27 AC 58 ms
10,368 KB
testcase_28 AC 58 ms
10,112 KB
testcase_29 AC 37 ms
9,728 KB
testcase_30 AC 33 ms
8,960 KB
testcase_31 AC 33 ms
8,832 KB
testcase_32 AC 64 ms
10,240 KB
testcase_33 AC 60 ms
10,240 KB
testcase_34 AC 60 ms
10,112 KB
testcase_35 AC 60 ms
10,240 KB
testcase_36 AC 61 ms
10,240 KB
testcase_37 AC 61 ms
10,240 KB
testcase_38 AC 59 ms
10,112 KB
権限があれば一括ダウンロードができます

ソースコード

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);
    }
    
    Vec<i32> ord;
    ord.reserve(n);
    i32 last = n;
    i32 trouble_min_pos = n;
    FenwickTree<Add<i32>> 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<Add<i32>> fw3(n);
    Vec<pair<i32, i32>> 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';
    }
}
0