結果
問題 | No.2665 Minimize Inversions of Deque |
ユーザー | k1suxu |
提出日時 | 2024-03-08 22:07:23 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 7,638 bytes |
コンパイル時間 | 3,465 ms |
コンパイル使用メモリ | 264,072 KB |
実行使用メモリ | 17,216 KB |
最終ジャッジ日時 | 2024-09-29 19:42:00 |
合計ジャッジ時間 | 11,099 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | AC | 135 ms
6,816 KB |
testcase_04 | AC | 136 ms
6,816 KB |
testcase_05 | AC | 138 ms
6,816 KB |
testcase_06 | WA | - |
testcase_07 | AC | 138 ms
6,816 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | AC | 260 ms
16,172 KB |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
ソースコード
// #pragma GCC target("avx") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) #define FOR(n) for(int i = 0; i < (int)n; i++) #define repi(i,a,b) for(int i = (int)a; i < (int)b; i++) #define all(x) x.begin(),x.end() //#define mp make_pair #define vi vector<int> #define vvi vector<vi> #define vvvi vector<vvi> #define vvvvi vector<vvvi> #define pii pair<int,int> #define vpii vector<pair<int,int>> template<typename T> bool chmax(T &a, const T b) {if(a<b) {a=b; return true;} else {return false;}} template<typename T> bool chmin(T &a, const T b) {if(a>b) {a=b; return true;} else {return false;}} using ll = long long; using ld = long double; using ull = unsigned long long; const ll INF = numeric_limits<long long>::max() / 2; const ld pi = 3.1415926535897932384626433832795028; const ll mod = 998244353; int dx[] = {1, 0, -1, 0, -1, -1, 1, 1}; int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; #define int long long struct fenwick_tree { int n, sz; vector<int> dat; function<int(int, int)> f = [](int x, int y) {return x + y;}; fenwick_tree(int n_) : dat(4*n_, 0), sz(n_) { int x = 1; while(x < n_) x *= 2; n = x; } void add(int i, int x) { i += n - 1; dat[i] += x; while(i > 0) { i = (i - 1) / 2; dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } } void insert(int i) { return add(i, 1); } void erase(int i) { return add(i, -1); } void update(int i, int x) { i += n - 1; dat[i] = x; while(i > 0) { i = (i - 1) / 2; dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } } int get_sum(int a, int b) { return get_sum_sub(a, b, 0, 0, n); } int get_sum_sub(int a, int b, int k, int l, int r) { if(r <= a || b <= l) { return 0; }else if(a <= l && r <= b) { return dat[k]; }else { int vl = get_sum_sub(a, b, k * 2 + 1, l, (l + r) / 2); int vr = get_sum_sub(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } } int get(int i) { return dat[i + n - 1]; } inline void print() { cout << "{ "; for(int i = 0; i < sz; i++) { cout << dat[i + n - 1] << " "; } cout << "}\n"; } }; namespace internal { // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` constexpr int bsf_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal template <class S, S (*op)(S, S), S (*e)()> struct segtree { public: segtree() : segtree(0) {} explicit segtree(int n) : segtree(std::vector<S>(n, e())) {} explicit segtree(const std::vector<S>& v) : _n((int)v.size()) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) const { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) const { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() const { return d[1]; } template <bool (*f)(S)> int max_right(int l) const { return max_right(l, [](S x) { return f(x); }); } template <class F> int max_right(int l, F f) const { assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*f)(S)> int min_left(int r) const { return min_left(r, [](S x) { return f(x); }); } template <class F> int min_left(int r, F f) const { assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; using S = int; S op(S x, S y) { return x + y; } S e() { return 0; } template <typename T> vector<T> compress(vector<T> &X) { vector<T> vals = X; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); for (int i = 0; i < (int)X.size(); i++) { X[i] = lower_bound(vals.begin(), vals.end(), X[i]) - vals.begin(); } return vals; } int the_number_of_inversions(vector<long long> a) { int n = (int)a.size(); compress<long long>(a); segtree<S, op, e> seg(n); int inversion = 0; FOR(n) { inversion += seg.prod(a[i]+1, n); seg.set(a[i], seg.get(a[i]) + 1); } return inversion; } void solve() { int n; cin >> n; vi p(n); FOR(n) { cin >> p[i]; --p[i]; } int inv = the_number_of_inversions(p); fenwick_tree fw(n); FOR(n) fw.insert(p[i]); vi pref, suff; for(int i = n-1; i >= 0; i--) { fw.erase(p[i]); if(fw.get_sum(0, p[i])-fw.get_sum(p[i], n) < 0 || (fw.get_sum(0, p[i])-fw.get_sum(p[i], n) == 0 && p[i] < p[0])) { pref.push_back(p[i]); inv += fw.get_sum(0, p[i])-fw.get_sum(p[i], n); }else { suff.push_back(p[i]); } } reverse(all(suff)); vi ans; for(auto e : pref) ans.push_back(e); for(auto e : suff) ans.push_back(e); cout << inv << endl; for(auto e : ans) cout << e+1 << " "; cout << endl; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); int t; cin >> t; while(t--) solve(); return 0; }