結果
| 問題 |
No.2665 Minimize Inversions of Deque
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2024-04-25 19:24:25 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 56 ms / 2,000 ms |
| コード長 | 1,333 bytes |
| コンパイル時間 | 805 ms |
| コンパイル使用メモリ | 58,112 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-11-08 07:03:03 |
| 合計ジャッジ時間 | 6,272 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 40 |
ソースコード
/* -*- coding: utf-8 -*-
*
* 2665.cc: No.2665 Minimize Inversions of Deque - yukicoder
*/
#include<cstdio>
#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
/* constant */
const int MAX_N = 200000;
/* typedef */
typedef long long ll;
typedef deque<int> dqi;
template <typename T>
struct BIT {
int n;
vector<T> bits;
BIT() {}
BIT(int _n) { init(_n); }
void init(int _n) {
n = _n;
bits.assign(n + 1, 0);
}
T sum(int x) {
x = min(x, n);
T s = 0;
while (x > 0) {
s += bits[x];
x -= (x & -x);
}
return s;
}
void add(int x, T v) {
if (x <= 0) return;
while (x <= n) {
bits[x] += v;
x += (x & -x);
}
}
};
/* global variables */
int ps[MAX_N];
BIT<int> bit;
/* subroutines */
/* main */
int main() {
int tn;
scanf("%d", &tn);
while (tn--) {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", ps + i);
bit.init(n);
dqi as;
ll x = 0;
for (int i = 0; i < n; i++) {
int c0 = bit.sum(ps[i]), c1 = i - c0;
if (c0 < c1) as.push_front(ps[i]), x += c0;
else as.push_back(ps[i]), x += c1;
bit.add(ps[i], 1);
}
printf("%lld\n", x);
for (int i = 0; i < n; i++)
printf("%d%c", as[i], (i + 1 < n) ? ' ' : '\n');
}
return 0;
}
tnakao0123