結果

問題 No.738 平らな農地
ユーザー akakimidoriakakimidori
提出日時 2018-12-04 03:57:16
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 2,068 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 31,312 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-19 09:28:56
合計ジャッジ時間 8,940 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 0 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 0 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 49 ms
4,376 KB
testcase_16 AC 50 ms
4,380 KB
testcase_17 AC 54 ms
4,380 KB
testcase_18 AC 52 ms
4,376 KB
testcase_19 AC 61 ms
4,380 KB
testcase_20 AC 50 ms
4,376 KB
testcase_21 AC 59 ms
4,376 KB
testcase_22 AC 51 ms
4,376 KB
testcase_23 AC 57 ms
4,376 KB
testcase_24 AC 57 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 53 ms
4,376 KB
testcase_46 AC 49 ms
4,376 KB
testcase_47 AC 51 ms
4,376 KB
testcase_48 AC 48 ms
4,376 KB
testcase_49 AC 46 ms
4,376 KB
testcase_50 AC 49 ms
4,380 KB
testcase_51 AC 53 ms
4,380 KB
testcase_52 AC 49 ms
4,376 KB
testcase_53 AC 52 ms
4,376 KB
testcase_54 AC 53 ms
4,376 KB
testcase_55 AC 54 ms
4,380 KB
testcase_56 AC 52 ms
4,376 KB
testcase_57 AC 48 ms
4,376 KB
testcase_58 AC 51 ms
4,380 KB
testcase_59 AC 52 ms
4,376 KB
testcase_60 AC 51 ms
4,376 KB
testcase_61 AC 51 ms
4,380 KB
testcase_62 AC 47 ms
4,380 KB
testcase_63 AC 56 ms
4,376 KB
testcase_64 AC 53 ms
4,376 KB
testcase_65 AC 62 ms
4,376 KB
testcase_66 AC 65 ms
4,380 KB
testcase_67 AC 39 ms
4,376 KB
testcase_68 AC 39 ms
4,380 KB
testcase_69 AC 48 ms
4,380 KB
testcase_70 AC 44 ms
4,376 KB
testcase_71 AC 13 ms
4,380 KB
testcase_72 AC 18 ms
4,380 KB
testcase_73 AC 18 ms
4,376 KB
testcase_74 AC 18 ms
4,380 KB
testcase_75 AC 46 ms
4,380 KB
testcase_76 AC 42 ms
4,376 KB
testcase_77 AC 48 ms
4,376 KB
testcase_78 AC 52 ms
4,376 KB
testcase_79 AC 53 ms
4,380 KB
testcase_80 AC 46 ms
4,380 KB
testcase_81 AC 48 ms
4,376 KB
testcase_82 AC 50 ms
4,380 KB
testcase_83 AC 17 ms
4,376 KB
testcase_84 AC 20 ms
4,376 KB
testcase_85 AC 78 ms
4,376 KB
testcase_86 AC 69 ms
4,380 KB
testcase_87 AC 42 ms
4,380 KB
testcase_88 AC 41 ms
4,376 KB
testcase_89 AC 1 ms
4,380 KB
testcase_90 AC 1 ms
4,380 KB
testcase_91 AC 0 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>

typedef long long int int64;

int cmp(const void *a,const void *b){
  return *(int *)a-*(int *)b;
}

int compress(int *a,const int n){
  qsort(a,n,sizeof(int),cmp);
  int i=0;
  int j=0;
  while(i<n){
    a[j++]=a[i++];
    while(i<n && cmp(a+j-1,a+i)==0) i++;
  }
  return j;
}

int toIndex(const int *za,const int len,const int v){
  int l=0;
  int r=len;
  while(r-l>1){
    int m=(l+r)/2;
    if(cmp(za+m,&v)<=0){
      l=m;
    } else {
      r=m;
    }
  }
  return l;
}

void add(int64 *bit,int index,int64 v){
  int n=bit[0];
  for(int i=index;i<=n;i+=i&-i) bit[i]+=v;
}

int64 sum(int64 *bit,int index){
  int64 res=0;
  for(int i=index;i>0;i-=i&-i) res+=bit[i];
  return res;
}

int search(int64 *bit,int64 x){
  const int n=bit[0];
  int k=1;
  while(2*k<n) k*=2;
  int y=0;
  for(;k>0;k>>=1){
    if(y+k<=n && bit[y+k]<x){
      x-=bit[y+k];
      y+=k;
    }
  }
  return y+1;
}

int64 calc(int64 *cntBit,int64 *sumBit,int *za,int k){
  int med=(k+1)/2;
  int index=search(cntBit,med);
  int64 v=za[index-1];
  int64 s=sum(sumBit,sumBit[0]);
  int64 low=sum(sumBit,index)-v*(sum(cntBit,index)-med);
  int64 res=(med*v-low)+(s-low-v*(k-med));
  return res;
}

void run(void){
  int n,k;
  scanf("%d%d",&n,&k);
  int *a=(int *)calloc(n,sizeof(int));
  int i;
  for(i=0;i<n;i++) scanf("%d",a+i);
  int *za=(int *)calloc(n,sizeof(int));
  for(i=0;i<n;i++) za[i]=a[i];
  int len=compress(za,n);
  int64 *cntBit=(int64 *)calloc(len+1,sizeof(int64));
  int64 *sumBit=(int64 *)calloc(len+1,sizeof(int64));
  cntBit[0]=sumBit[0]=len;
  for(i=0;i<k;i++){
    int index=toIndex(za,len,a[i]);
    add(cntBit,index+1,1);
    add(sumBit,index+1,a[i]);
  }
  int64 min=calc(cntBit,sumBit,za,k);
  for(i=k;i<n;i++){
    int index=toIndex(za,len,a[i]);
    add(cntBit,index+1,1);
    add(sumBit,index+1,a[i]);
    index=toIndex(za,len,a[i-k]);
    add(cntBit,index+1,-1);
    add(sumBit,index+1,-a[i-k]);
    int64 t=calc(cntBit,sumBit,za,k);
    if(t<min) min=t;
  }
  printf("%lld\n",min);
}

int main(void){
  run();
  return 0;
}
0