結果

問題 No.1391 ±1 Abs Sum
ユーザー trineutrontrineutron
提出日時 2021-02-17 17:35:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 793 bytes
コンパイル時間 2,730 ms
コンパイル使用メモリ 201,664 KB
実行使用メモリ 5,464 KB
最終ジャッジ日時 2023-10-12 04:29:05
合計ジャッジ時間 6,068 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 84 ms
5,340 KB
testcase_12 AC 63 ms
4,732 KB
testcase_13 AC 77 ms
5,048 KB
testcase_14 AC 50 ms
4,356 KB
testcase_15 AC 52 ms
4,420 KB
testcase_16 AC 65 ms
4,728 KB
testcase_17 AC 58 ms
4,640 KB
testcase_18 AC 78 ms
5,208 KB
testcase_19 AC 55 ms
4,520 KB
testcase_20 AC 82 ms
5,404 KB
testcase_21 AC 52 ms
4,728 KB
testcase_22 AC 52 ms
4,940 KB
testcase_23 AC 60 ms
5,160 KB
testcase_24 AC 56 ms
5,160 KB
testcase_25 AC 57 ms
5,300 KB
testcase_26 AC 59 ms
5,208 KB
testcase_27 AC 41 ms
4,364 KB
testcase_28 AC 43 ms
4,580 KB
testcase_29 AC 56 ms
4,884 KB
testcase_30 AC 47 ms
4,680 KB
testcase_31 AC 85 ms
5,256 KB
testcase_32 AC 45 ms
4,352 KB
testcase_33 AC 51 ms
4,348 KB
testcase_34 AC 76 ms
4,996 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 86 ms
5,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int n, k;

int64_t calc(const vector<int64_t> &s, int64_t x, int i, int l)
{
    return s.at(l) - l * x - (s.at(i) - s.at(l) - (i - l) * x) + (s.at(l + k) - s.at(i) - (l + k - i) * x) - (s.at(n) - s.at(l + k) - (n - l - k) * x);
}

int main()
{
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a.at(i);
    }
    vector<int64_t> s(n + 1);
    for (int i = 0; i < n; i++)
    {
        s.at(i + 1) = s.at(i) + a.at(i);
    }
    int64_t ans = 1e18;
    int l = 0;
    for (int i = 0; i < n; i++)
    {
        while (l + k < n and calc(s, a.at(i), i, l) > calc(s, a.at(i), i, l + 1))
        {
            l++;
        }
        ans = min(ans, calc(s, a.at(i), i, l));
    }
    cout << ans << endl;
}
0