結果

問題 No.1391 ±1 Abs Sum
ユーザー merom686merom686
提出日時 2021-02-12 22:36:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 935 bytes
コンパイル時間 765 ms
コンパイル使用メモリ 90,504 KB
実行使用メモリ 4,692 KB
最終ジャッジ日時 2023-09-27 05:21:44
合計ジャッジ時間 3,738 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 31 ms
4,532 KB
testcase_12 AC 26 ms
4,380 KB
testcase_13 AC 30 ms
4,488 KB
testcase_14 AC 15 ms
4,376 KB
testcase_15 AC 17 ms
4,376 KB
testcase_16 AC 21 ms
4,376 KB
testcase_17 AC 21 ms
4,380 KB
testcase_18 AC 24 ms
4,476 KB
testcase_19 AC 25 ms
4,376 KB
testcase_20 AC 29 ms
4,692 KB
testcase_21 WA -
testcase_22 WA -
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 AC 28 ms
4,612 KB
testcase_32 AC 14 ms
4,380 KB
testcase_33 AC 16 ms
4,384 KB
testcase_34 AC 45 ms
4,628 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 29 ms
4,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    k = n - k;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    ll r = 1LL << 60;
    for (int h = 0; h < 2; h++) {
        int x = a[min(k + n / 2, n - 1)];
        ll t = 0;
        vector<int> b(n);
        for (int i = 0; i < n; i++) {
            b[i] = abs(x - a[i]);
        }
        sort(b.begin(), b.end());
        reverse(b.begin(), b.end());
        for (int i = 0; i < n; i++) {
            t += b[i] * (i < k ? -1 : 1);
        }
        r = min(r, t);

        reverse(a.begin(), a.end());
        for (int i = 0; i < n; i++) {
            a[i] *= -1;
        }
    }

    cout << r << endl;

    return 0;
}
0