結果

問題 No.1391 ±1 Abs Sum
ユーザー LayCurseLayCurse
提出日時 2021-02-12 21:25:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,042 bytes
コンパイル時間 3,091 ms
コンパイル使用メモリ 211,976 KB
実行使用メモリ 6,152 KB
最終ジャッジ日時 2023-09-27 02:46:13
合計ジャッジ時間 4,745 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 9 ms
6,048 KB
testcase_12 AC 7 ms
5,604 KB
testcase_13 AC 9 ms
5,920 KB
testcase_14 AC 6 ms
5,416 KB
testcase_15 AC 6 ms
5,336 KB
testcase_16 AC 7 ms
5,584 KB
testcase_17 AC 8 ms
5,496 KB
testcase_18 AC 9 ms
6,068 KB
testcase_19 AC 8 ms
5,444 KB
testcase_20 AC 9 ms
5,852 KB
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 10 ms
5,968 KB
testcase_32 AC 6 ms
5,192 KB
testcase_33 AC 7 ms
5,320 KB
testcase_34 AC 10 ms
6,120 KB
testcase_35 WA -
testcase_36 AC 11 ms
5,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
int N;
int K;
long long A[200000];
int main(){
  int e98WHCEY;
  int i;
  int k;
  long long res = 4611686016279904256LL;
  long long tmp;
  rd(N);
  rd(K);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  for(e98WHCEY=(0);e98WHCEY<(2);e98WHCEY++){
    int j;
    for(j=(-2);j<(3);j++){
      for(i=(0);i<(N);i++){
        A[i] *= -1;
      }
      reverse(A, A+N);
      tmp = 0;
      k =max_L(0, N / 2 - (N - K) + j);
      for(i=(0);i<(K);i++){
        tmp += abs(A[i] - A[k]);
      }
      for(i=(K);i<(N);i++){
        tmp -= abs(A[i] - A[k]);
      }
      chmin(res, tmp);
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay version 20210103-1 [bug fixed 2]

// --- original code ---
// int N, K; ll A[2d5];
// {
//   int i, k;
//   ll res = ll_inf, tmp;
//   rd(N,K,A(N));
// 
//   rep(2) rep(j,-2,3){
//     rep(i,N) A[i] *= -1;
//     reverse(A, A+N);
// 
//     tmp = 0;
//     k = max(0, N / 2 - (N - K) + j);
//     rep(i,K) tmp += abs(A[i] - A[k]);
//     rep(i,K,N) tmp -= abs(A[i] - A[k]);
//     res <?= tmp;
//   }
//   wt(res);
// }
0