結果

問題 No.2665 Minimize Inversions of Deque
ユーザー hiro1729hiro1729
提出日時 2024-03-08 21:19:17
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 4,925 bytes
コンパイル時間 4,742 ms
コンパイル使用メモリ 322,004 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-09-29 18:55:34
合計ジャッジ時間 7,984 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 30 ms
6,820 KB
testcase_02 AC 30 ms
6,816 KB
testcase_03 AC 30 ms
6,820 KB
testcase_04 AC 29 ms
6,816 KB
testcase_05 AC 29 ms
6,820 KB
testcase_06 AC 29 ms
6,820 KB
testcase_07 AC 29 ms
6,816 KB
testcase_08 AC 29 ms
6,820 KB
testcase_09 AC 29 ms
6,816 KB
testcase_10 AC 29 ms
6,820 KB
testcase_11 AC 30 ms
6,820 KB
testcase_12 AC 29 ms
6,820 KB
testcase_13 AC 29 ms
6,820 KB
testcase_14 AC 29 ms
6,820 KB
testcase_15 AC 31 ms
6,816 KB
testcase_16 AC 29 ms
6,816 KB
testcase_17 AC 29 ms
6,816 KB
testcase_18 AC 28 ms
6,816 KB
testcase_19 AC 6 ms
6,816 KB
testcase_20 AC 2 ms
6,816 KB
testcase_21 AC 3 ms
6,820 KB
testcase_22 AC 3 ms
6,816 KB
testcase_23 AC 2 ms
6,820 KB
testcase_24 AC 3 ms
6,820 KB
testcase_25 AC 2 ms
6,816 KB
testcase_26 AC 2 ms
6,816 KB
testcase_27 AC 2 ms
6,816 KB
testcase_28 AC 2 ms
6,820 KB
testcase_29 AC 2 ms
6,816 KB
testcase_30 AC 40 ms
6,820 KB
testcase_31 AC 37 ms
6,820 KB
testcase_32 AC 38 ms
6,816 KB
testcase_33 AC 39 ms
6,816 KB
testcase_34 AC 36 ms
6,816 KB
testcase_35 AC 36 ms
6,816 KB
testcase_36 AC 37 ms
6,820 KB
testcase_37 AC 37 ms
6,816 KB
testcase_38 AC 39 ms
6,816 KB
testcase_39 AC 41 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;
using ld = long double;
template<typename T>
using vec = vector<T>;
template<typename T>
using pqu = priority_queue<T>;
template<typename T>
using pqug = priority_queue<T, vector<T>, greater<T>>;
using Pint = pair<int, int>;
using Pll = pair<ll, ll>;

const int inf = 2147483647;
const ll linf = 9223372036854775807LL;
const Pint dxy4[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const Pint dxy8[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
map<char, Pint> dir4 {{'L', {0, -1}}, {'R', {0, 1}}, {'U', {-1, 0}}, {'D', {1, 0}}};

#define v1 first
#define v2 second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define bisl(v, n) (lower_bound(v.begin(), v.end(), n) - v.begin())
#define bisr(v, n) (upper_bound(v.begin(), v.end(), n) - v.begin())
#define index(v, a) (find(all(v), a) - v.begin())
#define pcnt(n) __builtin_popcountll(n)

#define overload4(a, b, c, d, e, ...) e
#define range1(n) for (int _ = 0; _ < n; _++)
#define range2(i, n) for (int i = 0; i < n; i++)
#define range3(i, a, b) for (int i = a; i < b; i++)
#define range4(i, a, b, c) for (int i = a; i < b; i += c)
#define range(...) overload4(__VA_ARGS__, range4, range3, range2, range1)(__VA_ARGS__)
#define rrange2(i, n) for (int i = n; i > 0; i--)
#define rrange3(i, a, b) for (int i = a; i > b; i--)
#define rrange4(i, a, b, c) for (int i = a; i > b; i -= c)
#define rrange(...) overload4(__VA_ARGS__, rrange4, rrange3, rrange2, range1)(__VA_ARGS__)
#define lcases(c) for (char c = 'a'; c <= 'z'; c++)
#define ucases(c) for (char c = 'A'; c <= 'Z'; c++)

/*
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
//*/

struct _IOSetUp { _IOSetUp() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(12); cerr << fixed << setprecision(12); } } _iosetup;

// ac-library
#if __has_include(<atcoder/all>)

#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using mint1 = modint1000000007;

istream &operator>>(istream &is, mint &m) {
	string S; is >> S;
	for (char c: S) m = m * 10 + c;
	return is;
}

ostream &operator<<(ostream &os, const mint &m) {
	os << m.val();
	return os;
}

istream &operator>>(istream &is, mint1 &m) {
	string S; is >> S;
	for (char c: S) m = m * 10 + c;
	return is;
}

ostream &operator<<(ostream &os, const mint1 &m) {
	os << m.val();
	return os;
}

#endif

template<typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
	is >> p.first >> p.second;
	return is;
}

template<typename S, typename T>
ostream &operator<<(ostream &os, pair<S, T> &p) {
	os << p.first << ' ' << p.second;
	return os;
}

template<typename T>
istream &operator>>(istream &is, vector<T> &v) {
	for (auto &e: v) {
		is >> e;
	}
	return is;
}

template<typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
	for (int i = 0; i < v.size() - 1; i++) {
		os << v[i] << ' ';
	}
	os << v.back();
	return os;
}

template<typename T>
vector<T> &operator++(vector<T> &v) {
	for (auto &e: v) e++;
	return v;
}

template<typename T>
vector<T> operator++(vector<T> &v, int) {
	auto res = v;
	for (auto &e: v) e++;
	return res;
}

template<typename T>
vector<T> &operator--(vector<T> &v) {
	for (auto &e: v) e--;
	return v;
}

template<typename T>
vector<T> operator--(vector<T> &v, int) {
	auto res = v;
	for (auto &e: v) e--;
	return res;
}

template<typename T>
vector<T> repl(vector<T> v, T a, T b) {
	vector<T> res;
	for (T i: v) {
		res.push_back((i == a ? b : i));
	}
	return res;
}

string repl(string v, char a, char b) {
	string res;
	for (char i: v) {
		res.push_back((i == a ? b : i));
	}
	return res;
}

template<typename S, typename T>
map<S, T> repl(map<S, T> m, T a, T b) {
	map<S, T> res;
	for (auto [i, j]: m) {
		res[i] = (j == a ? b : j);
	}
	return res;
}

template<typename T>
void replace(vector<T> &v, T a, T b) {
	for (int i = 0; i < v.size(); i++) {
		if (v[i] == a) v[i] = b;
	}
}

void replace(string &v, char a, char b) {
	for (int i = 0; i < v.size(); i++) {
		if (v[i] == a) v[i] = b;
	}
}

template<typename S, typename T>
void replace(map<S, T> &m, T a, T b) {
	for (auto [i, j]: m) {
		m[i] = (j == a ? b : j);
	}
}

void move_dxy4(int &x, int &y, char c) {
	x += dir4[c].first;
	y += dir4[c].second;
}

int main() {
	int T;
	cin >> T;
	range(T) {
		int n; cin >> n; vec<int> P(n); cin >> P;
		fenwick_tree<int> fw(n); deque<int> dq; long long ans = 0;
		for (int i: P) {
			int a = fw.sum(0, i - 1);
			int b = fw.sum(i, n);
			ans += min(a, b);
			if (a < b) {
				dq.push_front(i);
			} else if (a > b) {
				dq.push_back(i);
			} else {
				if (!dq.empty()) {
					int b = dq.front();
					if (i < b) dq.push_front(i);
					else dq.push_back(i);
				} else {
					dq.push_back(i);
				}
			}
			fw.add(i - 1, 1);
		}
		cout << ans << '\n';
		for (int i: dq) cout << i << ' ';
		cout << '\n';
	}
}
0