結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-10-21 06:20:10
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 3,026 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 33,184 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-04-29 20:09:46
合計ジャッジ時間 2,537 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 9 ms
5,376 KB
testcase_04 AC 36 ms
7,808 KB
testcase_05 AC 33 ms
8,064 KB
testcase_06 AC 34 ms
7,936 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 WA -
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 19 ms
5,376 KB
testcase_13 AC 19 ms
5,376 KB
testcase_14 AC 19 ms
5,376 KB
testcase_15 AC 22 ms
5,376 KB
testcase_16 AC 19 ms
5,376 KB
testcase_17 AC 29 ms
5,376 KB
testcase_18 AC 33 ms
5,376 KB
testcase_19 AC 36 ms
5,376 KB
testcase_20 AC 40 ms
5,376 KB
testcase_21 AC 44 ms
5,632 KB
testcase_22 AC 46 ms
5,760 KB
testcase_23 AC 51 ms
6,016 KB
testcase_24 AC 55 ms
6,272 KB
testcase_25 AC 56 ms
6,656 KB
testcase_26 AC 61 ms
6,784 KB
testcase_27 WA -
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 14 ms
5,376 KB
testcase_31 AC 21 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 struct pairingHeapNode{
  struct pairingHeapNode *next;
  struct pairingHeapNode *child;
  unsigned char val[];
} heapNode;

typedef struct pairingHeap{
  heapNode *root;
  int heapSize;
  size_t size;
  int (*cmp)(const void *,const void *);
} heap;

heapNode* newHeapNode(const void *v,const size_t size){
  heapNode *res=(heapNode *)malloc(sizeof(heapNode)+size);
  memcpy(res->val,v,size);
  res->next=NULL;
  res->child=NULL;
  return res;
}

void freeOneNode(heapNode *t){
  free(t);
  return;
}

void freeAllNode(heapNode *t){
  if(t==NULL) return;
  freeAllNode(t->next);
  freeAllNode(t->child);
  freeOneNode(t);
  return;
}

heap* newHeap(const size_t size,int (*cmp)(const void *,const void *)){
  heap *res=(heap *)malloc(sizeof(heap));
  res->root=NULL;
  res->heapSize=0;
  res->size=size;
  res->cmp=cmp;
  return res;
}

int getSize(const heap *h){
  return h->heapSize;
}

int isEmpty(const heap *h){
  return getSize(h)==0;
}

heapNode* meld(heapNode *a,heapNode *b,int (*cmp)(const void *a,const void *b)){
  if(a==NULL) return b;
  if(b==NULL) return a;
  if(cmp(a->val,b->val)<=0){
    b->next=a->child;
    a->child=b;
    return a;
  } else {
    a->next=b->child;
    b->child=a;
    return b;
  }
}

void push(heap *h,const void *v){
  heapNode *p=newHeapNode(v,h->size);
  h->heapSize++;
  h->root=meld(h->root,p,h->cmp);
  return;
}

void pop(heap *h,void *v){
  memcpy(v,h->root->val,h->size);
  heapNode *lst=h->root->child;
  int (*cmp)(const void *a,const void *b)=h->cmp;
  freeOneNode(h->root);
  h->heapSize--;
  heapNode *r=NULL;
  while(lst!=NULL && lst->next!=NULL){
    heapNode *a=lst;
    heapNode *b=lst->next;
    heapNode *next=lst->next->next;
    a->next=b->next=NULL;
    r=meld(r,meld(a,b,cmp),cmp);
    lst=next;
  }
  if(lst!=NULL){
    r=meld(r,lst,cmp);
  }
  h->root=r;
  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<0?-1:1;
}

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

//positive only
inline int64 readInt(void){
  int64 res=0;
  char c=fgetc(stdin);
  while(!('0'<=c && c<='9')) c=fgetc(stdin);
  while('0'<=c && c<='9'){
    res=10*res+c-'0';
    c=fgetc(stdin);
  }
  return res;
}

inline void printInt(int64 v){
  char c[19];
  int k=0;
  while(v>0){
    c[k++]=v%10+'0';
    v/=10;
  }
  while(k>0) fputc(c[--k],stdout);
  fputc('\n',stdout);
}

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--){
    if(readInt()==1){
      int64 v=readInt();
      push(small,&v);
      if(getSize(small)>=k){
	pop(small,&v);
	push(large,&v);
      }
    } else if(isEmpty(large)){
      printf("-1\n");
    } else {
      int64 v;
      pop(large,&v);
      printInt(v);
    }
  }
  return;
}

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