結果

問題 No.1391 ±1 Abs Sum
ユーザー se1ka2se1ka2
提出日時 2021-02-13 13:21:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,149 bytes
コンパイル時間 1,567 ms
コンパイル使用メモリ 67,432 KB
実行使用メモリ 5,104 KB
最終ジャッジ日時 2023-09-27 21:45:15
合計ジャッジ時間 3,887 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 73 ms
5,104 KB
testcase_12 AC 57 ms
4,652 KB
testcase_13 WA -
testcase_14 AC 43 ms
4,380 KB
testcase_15 AC 45 ms
4,380 KB
testcase_16 AC 56 ms
4,544 KB
testcase_17 AC 51 ms
4,392 KB
testcase_18 AC 67 ms
4,792 KB
testcase_19 AC 51 ms
4,376 KB
testcase_20 AC 75 ms
4,840 KB
testcase_21 AC 45 ms
4,576 KB
testcase_22 AC 47 ms
4,596 KB
testcase_23 AC 52 ms
4,832 KB
testcase_24 AC 48 ms
4,672 KB
testcase_25 AC 51 ms
4,852 KB
testcase_26 AC 54 ms
4,892 KB
testcase_27 AC 38 ms
4,380 KB
testcase_28 AC 36 ms
4,380 KB
testcase_29 AC 50 ms
4,644 KB
testcase_30 AC 43 ms
4,488 KB
testcase_31 AC 74 ms
4,896 KB
testcase_32 AC 40 ms
4,376 KB
testcase_33 AC 45 ms
4,376 KB
testcase_34 AC 70 ms
4,860 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 80 ms
4,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

const ll INF = 100000000000000000;

int main()
{
    int n, k;
    cin >> n >> k;
    ll a[200005];
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = n - 1; i >= 0; i--) a[i] -= a[0];
    ll ans = INF;
    int x = 0;
    if(k * 2 >= n) x = (k * 2 - n) / 2;
    if(k * 2 < n){
        for(int c = 0; c < 2; c++){
            ll s = 0;
            reverse(a, a + n);
            for(int i = 0; i < k; i++) s += abs(a[i] - a[x]);
            for(int i = k; i < n; i++) s -= abs(a[i] - a[x]);
            ans = min(ans, s);
        }
    }
    else{
        ll s = 0;
        ll l = 0;
        for(int i = 0; i < n; i++){
            if(i >= x && i < k){
                s += a[i];
                l--;
            }
            else{
                s -= a[i];
                l++;
            }
        }
        ans = s + a[x] * l;
        for(int i = 0; x + i <= n - 1 - x - 2; i += 2){
            s += (a[i / 2] - a[x + i] - a[x + i + 1] + a[k + i / 2]) * 2;
            ans = min(ans, s + a[x + i + 2] * l);
        }
    }
    cout << ans << endl;
}
0