結果

問題 No.370 道路の掃除
ユーザー ふーらくたる
提出日時 2016-08-15 09:20:46
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 984 bytes
コンパイル時間 683 ms
コンパイル使用メモリ 67,532 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-07 17:38:18
合計ジャッジ時間 1,729 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

#define ALL(a) (a).begin(), (a).end()

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> neg_trash, pos_trash;

    for (auto i = 0; i < m; i++) {
        int d;
        cin >> d;
        if (d > 0) pos_trash.push_back(d);
        else if (d < 0) neg_trash.push_back(d);
        else n--;
    }

    sort(ALL(pos_trash));
    sort(ALL(neg_trash), greater<int>());

    auto ans = (1 << 28);
    // 正の位置にあるゴミからいくつとるかで全探索
    for (auto pos_num = 0; pos_num <= min(n, (int)pos_trash.size()); pos_num++) {
        auto neg_num = n - pos_num;
        if (neg_num > neg_trash.size()) continue;

        auto pos_end = pos_num > 0 ? pos_trash[pos_num - 1] : 0,
             neg_end = neg_num > 0 ? neg_trash[neg_num - 1] : 0;
        ans = min(ans, min(abs(pos_end), abs(neg_end)) + abs(pos_end - neg_end));
    }
    cout << ans << endl;

    return 0;
}
0