結果
| 問題 | 
                            No.1391 ±1 Abs Sum
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2021-02-12 23:20:27 | 
| 言語 | C++17(clang)  (17.0.6 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,540 bytes | 
| コンパイル時間 | 1,897 ms | 
| コンパイル使用メモリ | 143,192 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-20 01:01:49 | 
| 合計ジャッジ時間 | 26,516 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 30 WA * 3 TLE * 1 | 
ソースコード
//
// 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 + 100 < 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;
}