結果

問題 No.1391 ±1 Abs Sum
ユーザー ocvret_ocvret_
提出日時 2021-02-13 17:32:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 839 bytes
コンパイル時間 1,490 ms
コンパイル使用メモリ 166,636 KB
実行使用メモリ 4,764 KB
最終ジャッジ日時 2023-09-28 02:09:06
合計ジャッジ時間 4,843 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 80 ms
4,760 KB
testcase_12 AC 56 ms
4,376 KB
testcase_13 AC 68 ms
4,384 KB
testcase_14 AC 44 ms
4,376 KB
testcase_15 AC 46 ms
4,380 KB
testcase_16 AC 57 ms
4,380 KB
testcase_17 AC 52 ms
4,380 KB
testcase_18 AC 69 ms
4,376 KB
testcase_19 AC 48 ms
4,380 KB
testcase_20 AC 73 ms
4,572 KB
testcase_21 AC 46 ms
4,376 KB
testcase_22 AC 47 ms
4,376 KB
testcase_23 AC 52 ms
4,452 KB
testcase_24 AC 48 ms
4,380 KB
testcase_25 AC 48 ms
4,384 KB
testcase_26 AC 52 ms
4,636 KB
testcase_27 AC 36 ms
4,376 KB
testcase_28 AC 37 ms
4,380 KB
testcase_29 AC 47 ms
4,376 KB
testcase_30 AC 40 ms
4,376 KB
testcase_31 AC 74 ms
4,616 KB
testcase_32 AC 38 ms
4,376 KB
testcase_33 AC 43 ms
4,376 KB
testcase_34 AC 68 ms
4,764 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 81 ms
4,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>

using namespace std;
// using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007



int main(){
  
  ll n,k;
  cin >> n >> k;
  k = n-k;
  ll r = n-k,x = k,l = 0;
  ll d = 0;
  vector<ll> a(n);
  rep(i,n)cin >> a[i],d += a[i]-a[0];
  for(ll i = r;i < n;i++)d -= (a[i]-a[0])*2;
  r %= n;
  ll res = d;
  for(ll i = 1;i < n;i++){
    x = min(x,n-i);
    d -= (n-i-2*x)*(a[i]-a[i-1]);
    d += (i-2*(k-x))*(a[i]-a[i-1]);
    while(abs(a[i]-a[r]) < abs(a[i]-a[l]) && x > 0){
      d += 2*(-abs(a[i]-a[l])+abs(a[i]-a[r]));
      l++;r++;
      l %= n;r %= n;
      x--;
    }
    res = min(res,d);
  }
  cout << res << "\n";
  

  return 0;
}
0