結果

問題 No.1391 ±1 Abs Sum
ユーザー norikamenorikame
提出日時 2021-02-12 22:57:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,306 bytes
コンパイル時間 1,925 ms
コンパイル使用メモリ 202,572 KB
実行使用メモリ 11,040 KB
最終ジャッジ日時 2023-09-27 06:01:06
合計ジャッジ時間 5,246 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 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 2 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 84 ms
11,040 KB
testcase_12 AC 63 ms
8,792 KB
testcase_13 AC 77 ms
10,116 KB
testcase_14 AC 48 ms
7,576 KB
testcase_15 AC 50 ms
7,800 KB
testcase_16 AC 63 ms
9,104 KB
testcase_17 AC 56 ms
8,060 KB
testcase_18 AC 75 ms
10,244 KB
testcase_19 AC 57 ms
8,168 KB
testcase_20 AC 82 ms
10,436 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 84 ms
10,976 KB
testcase_32 AC 43 ms
7,000 KB
testcase_33 AC 50 ms
7,604 KB
testcase_34 AC 74 ms
4,580 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 86 ms
4,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
#define v(t) vector<t>
#define p(t) pair<t, t>
#define p2(t, s) pair<t, s>
#define vp(t) v(p(t))

#define rep(i, n) for (int i=0,i##_len=((int)(n)); i<i##_len; ++i)
#define rep2(i, a, n) for (int i=((int)(a)),i##_len=((int)(n)); i<=i##_len; ++i)
#define repr(i, n) for (int i=((int)(n)-1); i>=0; --i)
#define rep2r(i, a, n) for (int i=((int)(n)),i##_len=((int)(a)); i>=i##_len; --i)

#define repi(itr, c) for (__typeof((c).begin()) itr=(c).begin(); itr!=(c).end(); ++itr)
#define repir(itr, c) for (__typeof((c).rbegin()) itr=(c).rbegin(); itr!=(c).rend(); ++itr)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define eb emplace_back

#define INF (1e9)
#define LINF (1e18)
#define PI (acos(-1))
#define EPS (1e-7)
#define DEPS (1e-10)

int main(){
    int n, k;
    cin >> n >> k;
    v(ll) a(n);
    rep(i, n) cin >> a[i];
    if (k == n) {
        ll cval = 0, ans = 0;
        if (n%2==0) cval = (a[n/2-1]+a[n/2]) / 2;
        else cval = a[n/2];
        rep(i, n) ans += llabs(cval-a[i]);
        cout << ans << endl;
    }
    else {
        v(ll) al(n), ar(n);
        rep(i, n) al[i] = a[i] - a[0];
        repr(i, n) ar[i] = llabs(a[i] - a[n-1]);
        reverse(all(ar));
        v(ll) suml(n+1), sumr(n+1);
        rep(i, n) suml[i+1] = suml[i] + al[i];
        rep(i, n) sumr[i+1] = sumr[i] + ar[i];
        ll pvall = suml[n] - suml[k], pvalr = sumr[n] - sumr[k];
        if (k == 0) {
            cout << (-1)*max(suml[n],sumr[n]) << endl;
            return 0;
        }
        ll mval = (ll)(LINF);
        rep2(i, 1, k) {
            ll nval = ((i-1)*al[i-1] - suml[i-1]) + ((suml[k]-suml[i]) - (k-i)*al[i-1]) - (pvall - (n-k)*al[i-1]);
            mval = min(mval, nval);
        }
        rep2(i, 1, k) {
            ll nval = ((i-1)*ar[i-1] - sumr[i-1]) + ((sumr[k]-sumr[i]) - (k-i)*ar[i-1]) - (pvalr - (n-k)*ar[i-1]);
            mval = min(mval, nval);
        }
        cout << mval << endl;
    }
    return 0;
}
0