結果

問題 No.2665 Minimize Inversions of Deque
ユーザー Maffy-0
提出日時 2024-03-10 14:33:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,055 bytes
コンパイル時間 2,133 ms
コンパイル使用メモリ 197,712 KB
最終ジャッジ日時 2025-02-20 03:40:12
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 1 WA * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void fast_io() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
}

int T, N;
deque<int> q;

void solve() {
    cin >> N;
    q.clear();
    int X = 0;
    for (int i = 0; i < N; i++) {
        int P;
        cin >> P;
        if (q.empty()) {
            q.push_back(P);
        } else {
            int f = q.front();
            int b = q.back();
            if (P < f) {
                q.push_front(P);
            } else if (b < P) {
                q.push_back(P);
            } else {
                if (abs(f - P) <= abs(b - P)) {
                    X += abs(f - P);
                    q.push_front(P);
                } else {
                    X += abs(b - P);
                    q.push_back(P);
                }
            }
        }
    }
    cout << X << "\n";
    while (q.size()) {
        cout << q.front() << " ";
        q.pop_front();
    }
    cout << "\n";
}

signed main(void) {
    fast_io();
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
0