結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2019-02-12 14:47:43
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 88 ms / 3,000 ms
コード長 3,109 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 31,068 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-11 10:54:25
合計ジャッジ時間 3,444 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 0 ms
4,352 KB
testcase_02 AC 0 ms
4,348 KB
testcase_03 AC 22 ms
4,352 KB
testcase_04 AC 84 ms
4,348 KB
testcase_05 AC 81 ms
4,348 KB
testcase_06 AC 60 ms
4,348 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 39 ms
4,352 KB
testcase_13 AC 36 ms
4,352 KB
testcase_14 AC 35 ms
4,352 KB
testcase_15 AC 43 ms
4,348 KB
testcase_16 AC 32 ms
4,352 KB
testcase_17 AC 43 ms
4,356 KB
testcase_18 AC 48 ms
4,352 KB
testcase_19 AC 54 ms
4,348 KB
testcase_20 AC 58 ms
4,348 KB
testcase_21 AC 64 ms
4,352 KB
testcase_22 AC 68 ms
4,348 KB
testcase_23 AC 73 ms
4,352 KB
testcase_24 AC 78 ms
4,352 KB
testcase_25 AC 83 ms
4,348 KB
testcase_26 AC 88 ms
4,352 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 33 ms
4,352 KB
testcase_31 AC 30 ms
4,348 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.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;
}

static inline void heapFunc_swap(void * restrict a,void * restrict b,void * restrict tmp,const size_t size){
  if((size&7)==0){
    uint64_t *pa=a;
    uint64_t *pb=b;
    for(size_t k=0;k<size/sizeof(uint64_t);k++){
      uint64_t t=*pa;
      *pa=*pb;
      *pb=t;
      pa++;
      pb++;
    }
  } else if((size&3)==0){
    uint32_t *pa=a;
    uint32_t *pb=b;
    for(size_t k=0;k<size/sizeof(uint32_t);k++){
      uint32_t t=*pa;
      *pa=*pb;
      *pb=t;
      pa++;
      pb++;
    }
  } else {
    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