結果

問題 No.3754 Mischievous Resident (Hard)
コンテスト
ユーザー marc2825
提出日時 2026-08-11 10:55:05
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,027 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,121 ms
コンパイル使用メモリ 220,520 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-10-02 20:55:47
合計ジャッジ時間 8,417 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other AC * 6 WA * 42
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int Q;
    cin >> Q;
    
    while (Q--) {
        int N, M;
        cin >> N >> M;
        
        vector<long long> S(M), G(M);
        for (int i = 0; i < M; i++) cin >> S[i];
        for (int i = 0; i < M; i++) cin >> G[i];
        
        // 目標配置がソート済みか確認
        vector<long long> G_sorted = G;
        sort(G_sorted.begin(), G_sorted.end());
        
        if (G != G_sorted) {
            cout << "-1\n";
            continue;
        }
        
        // 初期配置をソート
        vector<long long> S_sorted = S;
        sort(S_sorted.begin(), S_sorted.end());
        
        // 最小操作回数 = 対応する要素間の距離合計
        long long total_cost = 0;
        for (int i = 0; i < M; i++) {
            total_cost += abs(S_sorted[i] - G_sorted[i]);
        }
        
        cout << total_cost << "\n";
    }
    
    return 0;
}
0