結果

問題 No.365 ジェンガソート
ユーザー hotpepsi
提出日時 2016-04-30 00:31:34
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 821 bytes
コンパイル時間 492 ms
コンパイル使用メモリ 63,704 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-12 01:50:06
合計ジャッジ時間 2,224 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <cstdio>

using namespace std;

class BIT {
	std::vector<int> bit;
	int size;
public:
	BIT() { }
	BIT(int sz) { init(sz); }
	void init(int sz) {
		bit = std::vector<int>((size = sz) + 1);
	}
	int sum(int i) {
		int s = 0;
		while (i > 0) {
			s += bit[i];
			i -= i & -i;
		}
		return s;
	}
	void add(int i, int x) {
		while (i <= size) {
			bit[i] += x;
			i += i & -i;
		}
	}
};

int main(int argc, char *argv[]) {
	int N;
	cin >> N;
	BIT bit(131072);
	int pos[131072];
	for (int i = 1; i <= N; ++i) {
		int a;
		cin >> a;
		pos[a] = i;
	}
	int ans = 0;
	for (int i = N; i >= 1; --i) {
		int s = bit.sum(pos[i]);
		if (s > 0) {
			++ans;
			bit.add(1, 1);
		} else {
			bit.add(pos[i], 1);
		}
	}
	cout << ans << endl;
	return 0;
}
0