結果
| 問題 |
No.2665 Minimize Inversions of Deque
|
| コンテスト | |
| ユーザー |
ぷら
|
| 提出日時 | 2024-03-08 23:41:13 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 50 ms / 2,000 ms |
| コード長 | 2,136 bytes |
| コンパイル時間 | 1,483 ms |
| コンパイル使用メモリ | 138,632 KB |
| 最終ジャッジ日時 | 2025-02-20 03:13:01 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 40 |
ソースコード
#include <string.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
template <typename T>
struct BinaryIndexedTree {
int N;
vector<T>bit;
BinaryIndexedTree(){}
BinaryIndexedTree(int x) {
N = x;
bit.resize(x+1);
}
void add(int x,T y) {
x++;
while(x <= N) {
bit[x] += y;
x += x&-x;
}
}
T sum(int x) {
x++;
T res = 0;
while(x) {
res += bit[x];
x -= x&-x;
}
return res;
}
inline T sum(int l, int r) {//[l,r)
return sum(r-1)-sum(l-1);
}
inline T operator[](int k) {
return sum(k)-sum(k-1);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
vector<int>p(n);
BinaryIndexedTree<int>bit(n+1);
deque<int>ans;
long long fans = 0;
for(int i = 0; i < n; i++) {
cin >> p[i];
int a = bit.sum(0,p[i]);
int b = bit.sum(p[i],n+1);
bit.add(p[i],1);
fans += min(a,b);
if(a < b) {
ans.push_front(p[i]);
}
else if(b > a) {
ans.push_back(p[i]);
}
else if(ans.empty() || ans.front() > p[i]) {
ans.push_front(p[i]);
}
else {
ans.push_back(p[i]);
}
}
cout << fans << "\n";
while(!ans.empty()) {
cout << ans.front() << ((ans.size() == 1)?"\n":" ");
ans.pop_front();
}
}
}
ぷら