結果

問題 No.3016 ハチマキおじさん
コンテスト
ユーザー 麻崎 系
提出日時 2026-11-14 07:26:41
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 131 ms / 2,000 ms
+ 931µs
コード長 1,494 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,037 ms
コンパイル使用メモリ 369,956 KB
実行使用メモリ 14,932 KB
最終ジャッジ日時 2026-11-14 07:26:53
合計ジャッジ時間 9,978 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int main() {
    int N;
    cin >> N;
    
    vector<long long> A(N), B(N-1);
    for (int i = 0; i < N; i++) {
        long long a;
        cin >> a;
        A.at(i) = a;
    }
    for (int i = 0; i < N-1; i++) {
        long long b;
        cin >> b;
        B.at(i) = b;
    }

    sort(A.begin(), A.end());
    sort(B.begin(), B.end());

    vector<long long> P(N-1);
    P.at(0) = abs(A.at(0) - B.at(0));
    for (int i = 1; i < N-1; i++) {
        P.at(i) = P.at(i-1) + abs(A.at(i) - B.at(i));
    }

    vector<long long> Q(N-1);
    Q.at(N-2) = abs(A.at(N-1) - B.at(N-2));
    for (int i = N-3; i >= 0; i--) {
        Q.at(i) = Q.at(i+1) + abs(A.at(i+1) - B.at(i));
    }

    vector<int> I;
    long long complaint = Q.at(0);
    I.push_back(0);
    for (int i = 0; i < N-2; i++) {
        if (complaint > P.at(i) + Q.at(i + 1)) {
            I.clear();
            complaint = P.at(i) + Q.at(i + 1);
            I.push_back(i + 1);
        } else if (complaint == P.at(i) + Q.at(i + 1)) {
            I.push_back(i + 1);
        }
    }
    if (complaint > P.at(N-2)) {
        I.clear();
        I.push_back(N-1);
    } else if (complaint == P.at(N-2)) {
        I.push_back(N-1);
    }

    set<int> length;
    for (int i : I) {
        length.insert(A.at(i));
    }

    cout << length.size() << endl;
    for (auto value : length) {
        if (value != *begin(length)) cout << " ";
        cout << value;
    }
    cout << endl;
}
0