結果

問題 No.1391 ±1 Abs Sum
ユーザー milanis48663220milanis48663220
提出日時 2021-02-12 22:20:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 1,402 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 107,240 KB
実行使用メモリ 6,200 KB
最終ジャッジ日時 2023-09-27 04:48:06
合計ジャッジ時間 3,244 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 27 ms
6,164 KB
testcase_12 AC 21 ms
5,616 KB
testcase_13 AC 25 ms
6,000 KB
testcase_14 AC 16 ms
4,896 KB
testcase_15 AC 17 ms
4,948 KB
testcase_16 AC 22 ms
5,360 KB
testcase_17 AC 19 ms
5,220 KB
testcase_18 AC 26 ms
5,936 KB
testcase_19 AC 19 ms
5,216 KB
testcase_20 AC 27 ms
5,880 KB
testcase_21 AC 18 ms
5,492 KB
testcase_22 AC 18 ms
5,404 KB
testcase_23 AC 20 ms
5,952 KB
testcase_24 AC 19 ms
5,688 KB
testcase_25 AC 21 ms
5,740 KB
testcase_26 AC 20 ms
5,952 KB
testcase_27 AC 13 ms
4,948 KB
testcase_28 AC 15 ms
5,208 KB
testcase_29 AC 18 ms
5,624 KB
testcase_30 AC 16 ms
5,140 KB
testcase_31 AC 28 ms
6,160 KB
testcase_32 AC 14 ms
4,572 KB
testcase_33 AC 16 ms
4,824 KB
testcase_34 AC 25 ms
5,900 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 28 ms
6,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;
const ll INF = 1e18;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, k; cin >> n >> k;
    vector<ll> a(n);
    vector<ll> cumsum_a(n+1);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        cumsum_a[i+1] = cumsum_a[i]+a[i];
    }
    // a[l]+...+a[r-1]
    auto range_sum = [&](int l, int r){
        return cumsum_a[r]-cumsum_a[l];
    };
    ll ans = INF;
    int l = 0, r = k;
    for(int c = 0; c < n; c++){
        while(r < n && a[c]-a[l] > a[r]-a[c]){
            l++; r++;
        }
        ll tmp = -(a[c]*l-range_sum(0, l));
        tmp -= range_sum(r, n)-a[c]*(n-r);
        tmp += a[c]*(c-l)-range_sum(l, c);
        tmp += range_sum(c, r)-a[c]*(r-c);
        chmin(ans, tmp);
    }
    cout << ans << endl;
}
0