結果

問題 No.1391 ±1 Abs Sum
ユーザー tnakao0123tnakao0123
提出日時 2021-02-16 16:30:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,146 bytes
コンパイル時間 870 ms
コンパイル使用メモリ 91,252 KB
実行使用メモリ 4,428 KB
最終ジャッジ日時 2023-10-11 04:42:22
合計ジャッジ時間 6,069 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,356 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,348 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 17 ms
4,352 KB
testcase_15 AC 17 ms
4,356 KB
testcase_16 AC 21 ms
4,352 KB
testcase_17 AC 19 ms
4,348 KB
testcase_18 AC 26 ms
4,348 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 28 ms
4,368 KB
testcase_32 AC 15 ms
4,348 KB
testcase_33 AC 18 ms
4,348 KB
testcase_34 WA -
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 30 ms
4,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1391.cc:  No.1391 ±1 Abs Sum - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;

/* global variables */

int as[MAX_N];

/* subroutines */

ll f(int n, int k, int x) {
  ll sum = 0;
  int r = n - k;
  for (int i = 0, j = n - 1; i <= j;) {
    int di = abs(x - as[i]), dj = abs(x - as[j]);
    if (di >= dj) {
      if (r > 0) sum -= di, r--;
      else sum += di;
      i++;
    }
    else {
      if (r > 0) sum -= dj, r--;
      else sum += dj;
      j--;
    }
  }
  return sum;
}

/* main */

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  ll f0 = f(n, k, as[0]);
  ll f1 = f(n, k, as[n - 1]);
  printf("%lld\n", min(f0, f1));
  return 0;
}
0