結果

問題 No.1391 ±1 Abs Sum
ユーザー milanis48663220milanis48663220
提出日時 2021-02-12 22:20:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,402 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 101,344 KB
最終ジャッジ日時 2025-01-18 18:51:17
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0