結果

問題 No.1391 ±1 Abs Sum
ユーザー trineutrontrineutron
提出日時 2021-02-17 17:34:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 804 bytes
コンパイル時間 2,248 ms
コンパイル使用メモリ 195,712 KB
最終ジャッジ日時 2025-01-18 21:50:34
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18 WA * 16
権限があれば一括ダウンロードができます

ソースコード

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 < i and l + k <= i 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