結果

問題 No.370 道路の掃除
ユーザー ふーらくたるふーらくたる
提出日時 2016-08-15 08:55:54
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,089 bytes
コンパイル時間 610 ms
コンパイル使用メモリ 68,192 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 06:47:42
合計ジャッジ時間 1,779 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1 ms
5,376 KB
testcase_16 WA -
testcase_17 AC 1 ms
5,376 KB
testcase_18 WA -
testcase_19 AC 1 ms
5,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 2 ms
5,376 KB
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 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    int n, m;
    cin >> n >> m;
    // 正の近傍
    priority_queue<int, vector<int>, greater<int>> pos_neighbor;
    // 負の近傍
    priority_queue<int> neg_neighbor;
    for (int i = 0; i < m; i++) {
        int d;
        cin >> d;
        if (d >= 0) pos_neighbor.push(d);
        else neg_neighbor.push(d);
    }

    int pos_end = 0, neg_end = 0, num = 0;
    while (num < n) {
        if (neg_neighbor.empty()) {
            pos_end = pos_neighbor.top(); pos_neighbor.pop();
        } else if (pos_neighbor.empty()) {
            neg_end = neg_neighbor.top(); neg_neighbor.pop();
        } else if (abs(neg_neighbor.top() - neg_end) < abs(pos_neighbor.top() - pos_end)) {
            neg_end = neg_neighbor.top(); neg_neighbor.pop();
        } else {
            pos_end = pos_neighbor.top(); pos_neighbor.pop();
        }
        num++;
    }
    int ans = min(abs(neg_end), abs(pos_end)) + abs(neg_end) + abs(pos_end);
    cout << ans << endl;

    return 0;
}
0