結果

問題 No.365 ジェンガソート
ユーザー ふーらくたる
提出日時 2016-07-06 10:24:32
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,177 bytes
コンパイル時間 432 ms
コンパイル使用メモリ 59,400 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-12 20:25:41
合計ジャッジ時間 1,909 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16 WA * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
/*
 * Fenwick Treeの実装.
 * 区間[1, n]において, いわゆるfoldlの操作を高速化することができる.
 */
template<typename T>
class FenwickTree {
public:
    vector<T> elt_;
    int n_;

    FenwickTree() { }

    FenwickTree(int n) {
        elt_ = vector<T>(n + 1);
        n_ = n;
        for (int i = 0; i <= n_; i++) {
            elt_[i] = 0;
        }
    }

    void add(int i, T x) {
        while (i <= n_) {
            elt_[i] += x;
            i += i & -i;
        }
    }

    T query(int i) {
        T s = 0;
        while (i > 0) {
            s += elt_[i];
            i -= i & -i;
        }
        return s;
    }
};

const int kMAX_N = 100010;

int N;
int a[kMAX_N];

void Solve() {
    FenwickTree<int> tree(N);
    int ans = 0;
    for (int i = 0; i < N; i++) {
        tree.add(a[i], 1);
        if (tree.query(N) - tree.query(a[i]) > 0) {
            ans++;
        }
    }
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> N;

    for (int i = 0; i < N; i++) {
        cin >> a[i];
    }

    Solve();

    return 0;
}
0