結果

問題 No.1391 ±1 Abs Sum
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2021-02-12 23:07:57
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,539 bytes
コンパイル時間 1,233 ms
コンパイル使用メモリ 108,092 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2023-09-27 06:24:57
合計ジャッジ時間 13,855 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 354 ms
6,156 KB
testcase_12 AC 341 ms
5,416 KB
testcase_13 AC 323 ms
6,004 KB
testcase_14 AC 134 ms
4,884 KB
testcase_15 AC 143 ms
4,824 KB
testcase_16 AC 169 ms
5,372 KB
testcase_17 AC 144 ms
5,144 KB
testcase_18 AC 228 ms
5,900 KB
testcase_19 AC 284 ms
5,076 KB
testcase_20 WA -
testcase_21 AC 537 ms
5,436 KB
testcase_22 WA -
testcase_23 AC 649 ms
5,880 KB
testcase_24 AC 985 ms
5,804 KB
testcase_25 AC 601 ms
5,828 KB
testcase_26 AC 1,061 ms
5,900 KB
testcase_27 AC 646 ms
4,924 KB
testcase_28 AC 426 ms
5,140 KB
testcase_29 AC 966 ms
5,644 KB
testcase_30 AC 474 ms
5,304 KB
testcase_31 AC 265 ms
6,176 KB
testcase_32 AC 112 ms
4,768 KB
testcase_33 AC 142 ms
4,852 KB
testcase_34 AC 770 ms
5,944 KB
testcase_35 WA -
testcase_36 AC 194 ms
6,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
// Created by zeronosu77108 on Feb 12, 2021.
//
#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <numeric>
#include <set>
#include <complex>
#include <optional>

using namespace std;
struct aaa{aaa(){cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(20);};}aaa;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v){o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
#define debug(v) {cerr<<"\033[1;36m[debug]\033[m "<<#v<<" : "<<(v)<<endl;}

using int64 = long long;

int main() {
    int n, k;
    cin >> n >> k;

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

    sort(a.begin(), a.end());

    long amin = *min_element(a.begin(), a.end());
    long amax = *max_element(a.begin(), a.end());

    const auto f = [&](long ai) {
        vector b(n, 0L);
        for (int i=0; i<n; i++) b[i] = abs(a[i] - ai);
        sort(b.begin(), b.end());

        return accumulate(b.begin(), b.begin()+k, 0L) - accumulate(b.begin()+k, b.end(), 0L);
    };

    int l = 0, r = n-1;
    while(l + 10 < r) {
        int ll = (l+l+r) / 3;
        int rr = (l+r+r) / 3;

        if (f(a[ll]) < f(a[rr])) r = rr;
        else l = ll;

    }

    long ans = min(f(amin), f(amax));
    ans = min(ans, f(a[n/2-1]));
    ans = min(ans, f(a[n/2]));
    for (int i=l; i<=r; i++) {
        ans = min(ans, f(a[i]));
    }
    cout << ans << endl;
}
0