結果

問題 No.365 ジェンガソート
ユーザー AnchorBluesAnchorBlues
提出日時 2023-03-10 19:47:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,514 bytes
コンパイル時間 3,206 ms
コンパイル使用メモリ 173,060 KB
実行使用メモリ 6,436 KB
最終ジャッジ日時 2023-10-18 06:47:36
合計ジャッジ時間 4,552 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 21 ms
6,096 KB
testcase_20 AC 7 ms
4,348 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 22 ms
6,360 KB
testcase_37 AC 21 ms
6,360 KB
testcase_38 WA -
testcase_39 AC 21 ms
6,360 KB
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;

const ll INF = 1LL << 60;

template <class T>
void chmax(T& a, T b) {
    if (b > a) a = b;
}
template <class T>
void chmin(T& a, T b) {
    if (b < a) a = b;
}

template <typename T, typename S>
std::ostream& operator<<(std::ostream& os, const pair<T, S>& x) noexcept {
    return os << "(" << x.first << ", " << x.second << ")";
}

template <typename T>
void print_vector(vector<T> a) {
    cout << '[';
    for (int i = 0; i < a.size(); i++) {
        cout << a[i];
        if (i != a.size() - 1) {
            cout << ", ";
        }
    }
    cout << ']' << endl;
}

class BIT {
   public:
    // コンストラクタ
    BIT();
    BIT(int);

    // 区間加算
    void add(int, int, ll);

    // i番目の要素への加算
    void add(int, ll);

    // 区間和
    ll sum(int, int) const;

    // i番目の要素の値
    ll at(int) const;

   private:
    void add_sub(int, int, ll);
    ll sum_sub(int, int) const;
    vector<vector<ll>> _bit;
    int _size;
};

// コンストラクタ
BIT::BIT() {}

BIT::BIT(int N) {
    _size = N + 1;
    _bit = vector<vector<ll>>(2, vector<ll>(_size, 0));
}

void BIT::add_sub(int p, int i, ll x) {
    for (int idx = i; idx < _size; idx += (idx & -idx)) {
        _bit[p][idx] += x;
    }
}

void BIT::add(int l, int r, ll x) {
    l++;
    r++;
    add_sub(0, l, -x * (l - 1));
    add_sub(0, r, x * (r - 1));
    add_sub(1, l, x);
    add_sub(1, r, -x);
}

void BIT::add(int i, ll x) { this->add(i, i + 1, x); }

ll BIT::sum_sub(int p, int i) const {
    ll s = 0;
    for (int idx = i; idx > 0; idx -= (idx & -idx)) {
        s += _bit[p][idx];
    }
    return s;
}

ll BIT::sum(int l, int r) const {
    ll sl = sum_sub(0, l) + sum_sub(1, l) * l;
    ll sr = sum_sub(0, r) + sum_sub(1, r) * r;
    return sr - sl;
}

ll BIT::at(int i) const { return this->sum(i, i + 1); }

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll N;
    cin >> N;
    vector<ll> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        A[i]--;
    }
    auto bit = BIT(N);
    ll ret = 0;
    for (int i = 0; i < N; i++) {
        ll a = A[i];
        if (bit.sum(a, N) > 0) {
            ret++;
        }
        bit.add(a, 1);
    }
    std::cout << ret << "\n";
    return 0;
}
0