結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-12-23 04:31:56
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 123 ms / 3,000 ms
コード長 2,673 bytes
コンパイル時間 774 ms
コンパイル使用メモリ 31,220 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-26 02:24:33
合計ジャッジ時間 4,345 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 24 ms
4,348 KB
testcase_04 AC 120 ms
4,348 KB
testcase_05 AC 123 ms
4,348 KB
testcase_06 AC 60 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 49 ms
4,348 KB
testcase_13 AC 43 ms
4,348 KB
testcase_14 AC 42 ms
4,348 KB
testcase_15 AC 61 ms
4,348 KB
testcase_16 AC 36 ms
4,348 KB
testcase_17 AC 54 ms
4,348 KB
testcase_18 AC 60 ms
4,348 KB
testcase_19 AC 69 ms
4,348 KB
testcase_20 AC 77 ms
4,348 KB
testcase_21 AC 86 ms
4,348 KB
testcase_22 AC 91 ms
4,348 KB
testcase_23 AC 100 ms
4,348 KB
testcase_24 AC 105 ms
4,348 KB
testcase_25 AC 113 ms
4,348 KB
testcase_26 AC 122 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 49 ms
4,348 KB
testcase_31 AC 40 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef struct binaryHeap{
  void *h;
  size_t heapSize;
  size_t maxSize;
  size_t size;
  int (*cmp)(const void *,const void *);
} heap;

heap* newHeap(const size_t size,int (*cmpF)(const void *,const void *)){
  const size_t maxSize=1;
  heap *res=(heap *)malloc(sizeof(heap));
  res->h=malloc(size*(maxSize+1));
  res->heapSize=0;
  res->maxSize=maxSize;
  res->size=size;
  res->cmp=cmpF;
  return res;
}

int isEmpty(const heap *h){
  return h->heapSize==0;
}

void freeHeap(heap *h){
  free(h->h);
  free(h);
  return;
}

void initHeap(heap *h){
  h->heapSize=0;
  return;
}

inline void heapFunc_swap(void * restrict a,void * restrict b,void * restrict tmp,size_t size){
  memcpy(tmp,a,size);
  memcpy(a,b,size);
  memcpy(b,tmp,size);
  return;
}

void push(heap *q,const void *p){
  if(q->heapSize==q->maxSize){
    q->maxSize*=2;
    q->h=realloc(q->h,q->size*(q->maxSize+1));
  }
  q->heapSize+=1;
  char *h=(char *)(q->h);
  int k=q->heapSize;
  const size_t size=q->size;
  int (*cmp)(const void *,const void *)=q->cmp;
  memcpy(h+k*size,p,size);
  while(k>1){
    size_t parent=k/2;
    if(cmp(h+parent*size,h+k*size)<=0) return;
    heapFunc_swap(h+parent*size,h+k*size,h,size);
    k=parent;
  }
  return;
}

void pop(heap *q,void *res){
  char *h=(char *)(q->h);
  const size_t size=q->size;
  if(res!=NULL) memcpy(res,h+size,size);
  memcpy(h+size,h+size*q->heapSize,size);
  q->heapSize-=1;
  int (*cmp)(const void *,const void *)=q->cmp;
  const size_t n=q->heapSize;
  size_t k=1;
  while(2*k+1<=n){
    size_t next=cmp(h+2*k*size,h+(2*k+1)*size)<=0?2*k:2*k+1;
    if(cmp(h+k*size,h+next*size)<=0) return;
    heapFunc_swap(h+k*size,h+next*size,h,size);
    k=next;
  }
  if(2*k<=n && cmp(h+k*size,h+2*k*size)>0) heapFunc_swap(h+k*size,h+2*k*size,h,size);
  return;
}

void top(const heap *q,void *res){
  memcpy(res,(char *)q->h+q->size,q->size);
  return;
}

typedef long long int int64;

int cmpMin(const void *a,const void *b){
  int64 p=*(int64 *)a;
  int64 q=*(int64 *)b;
  return p==q?0:p<q?-1:1;
}

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

void run(void){
  int q,k;
  scanf("%d%d",&q,&k);
  heap *small=newHeap(sizeof(int64),cmpMax);
  heap *large=newHeap(sizeof(int64),cmpMin);
  while(q--){
    int c;
    scanf("%d",&c);
    if(c==1){
      int64 v;
      scanf("%lld",&v);
      push(small,&v);
      if(small->heapSize>=k){
	pop(small,&v);
	push(large,&v);
      }
    } else if(isEmpty(large)){
      printf("-1\n");
    } else {
      int64 v;
      pop(large,&v);
      printf("%lld\n",v);
    }
  }
  return;
}

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