結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-10-05 21:58:49
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 117 ms / 3,000 ms
コード長 2,346 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 31,744 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 17:14:23
合計ジャッジ時間 3,402 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 23 ms
5,376 KB
testcase_04 AC 110 ms
5,376 KB
testcase_05 AC 117 ms
5,376 KB
testcase_06 AC 59 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 48 ms
5,376 KB
testcase_13 AC 40 ms
5,376 KB
testcase_14 AC 41 ms
5,376 KB
testcase_15 AC 59 ms
5,376 KB
testcase_16 AC 34 ms
5,376 KB
testcase_17 AC 50 ms
5,376 KB
testcase_18 AC 56 ms
5,376 KB
testcase_19 AC 65 ms
5,376 KB
testcase_20 AC 71 ms
5,376 KB
testcase_21 AC 80 ms
5,376 KB
testcase_22 AC 83 ms
5,376 KB
testcase_23 AC 93 ms
5,376 KB
testcase_24 AC 97 ms
5,376 KB
testcase_25 AC 104 ms
5,376 KB
testcase_26 AC 112 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 49 ms
5,376 KB
testcase_31 AC 37 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long int int64;

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

heap* newHeap(size_t size,int (*cmpF)(const void *,const void *)){
  const int 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(heap *h){
  return h->heapSize==0;
}

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

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

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

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

int cmpMin(const void *a,const void *b){
  return *(int64 *)a<=*(int64 *)b;
}

int cmpMax(const void *a,const void *b){
  return *(int64 *)a>=*(int64 *)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