結果

問題 No.2089 置換の符号
ユーザー ruthenruthen
提出日時 2022-09-30 22:13:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 2,074 bytes
コンパイル時間 3,824 ms
コンパイル使用メモリ 205,104 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 15:47:15
合計ジャッジ時間 3,244 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 4 ms
4,376 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 4 ms
4,376 KB
testcase_32 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;



template <class T> struct fenwick_tree {
    int N;
    std::vector<T> seg;
    fenwick_tree(int N) : N(N), seg(N + 1, 0) {}
    fenwick_tree(std::vector<T> &A) {
        N = (int)A.size();
        seg.resize(N + 1);
        for (int i = 0; i < N; i++) add(i, A[i]);
    }
    // A[i] += x
    void add(int i, T x) {
        assert(0 <= i and i < N);
        i++;  // 1-indexed
        while (i <= N) {
            seg[i] += x;
            i += i & -i;
        }
    }
    // A[0] + ... + A[i - 1]
    T sum(int i) const {
        assert(0 <= i and i <= N);
        T s = 0;
        while (i > 0) {
            s += seg[i];
            i -= i & -i;
        }
        return s;
    }
    // A[a] + ... + A[b - 1]
    T sum(int a, int b) const {
        assert(0 <= a and a <= b and b <= N);
        return sum(b) - sum(a);
    }

    // output
    friend std::ostream &operator<<(std::ostream &os, const fenwick_tree &A) {
        for (int i = 0; i < A.N; i++) os << A.sum(i, i + 1) << " \n"[i == A.N - 1];
        return os;
    }
};

/**
 * @brief Fenwick Tree (Binary Indexed Tree)
 * @docs docs/data_structure/fenwick_tree.md
 */

template <class T> long long inversion_number(std::vector<T>& A) {
    auto B = A;
    sort(B.begin(), B.end());
    B.erase(unique(B.begin(), B.end()), B.end());
    int N = (int)B.size();
    fenwick_tree<int> fen(N);
    long long ret = 0;
    for (auto& ai : A) {
        int i = lower_bound(B.begin(), B.end(), ai) - B.begin();
        ret += fen.sum(i + 1, N);
        fen.add(i, 1);
    }
    return ret;
}

/**
 * @brief Inversion Number (転倒数)
 * @docs docs/dp/inversion_number.md
 */

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    V<int> S(N);
    rep(i, N) cin >> S[i];
    cout << (inversion_number(S) % 2 == 0 ? 1 : -1) << '\n';
    return 0;
}
0