結果

問題 No.1425 Yet Another Cyclic Shifts Sorting
ユーザー nikkukun
提出日時 2021-03-12 22:26:26
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,298 bytes
コンパイル時間 1,916 ms
コンパイル使用メモリ 182,500 KB
実行使用メモリ 33,152 KB
最終ジャッジ日時 2024-10-14 12:57:07
合計ジャッジ時間 7,643 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

map<int, set<int>> intToSet;

int a[N];

struct BITree {
	int t[N];
	int lowbit(int x) {
		return x & -x;
	}
	void add(int x, int det) {
		for (; x < N; x += lowbit(x))
			t[x] += det;
	}
	int query(int x) {
		int ret = 0;
		for (; x; x -= lowbit(x))
			ret += t[x];
		return ret;
	}
};
BITree bt;

int main() {
	ios::sync_with_stdio(0);

	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		intToSet[a[i]].insert(i);
		bt.add(i, 1);
	}
	sort(a + 1, a + n + 1);

	int pre = n + 1;
	int ans = 0;
	for (int i = n; i >= 1; i--) {
		auto &s = intToSet[a[i]];
		auto p = s.lower_bound(pre);
		int now;
		int det = 0;
		if (p != s.begin()) {
			p--;
			now = *p;
			det = bt.query(pre) - bt.query(now);
		} else {
			now = *s.rbegin();
			det = bt.query(pre) - (bt.query(n) - bt.query(now));
		}
		s.erase(now);
		bt.add(now, -1);
		ans += det != 0;
		pre = now;
	}
	cout << ans << endl;

	return 0;
}
0