結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-10-06 18:30:21
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 130 ms / 3,000 ms
コード長 2,601 bytes
コンパイル時間 436 ms
コンパイル使用メモリ 32,600 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-04-20 17:55:33
合計ジャッジ時間 3,755 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 20 ms
5,376 KB
testcase_04 AC 81 ms
18,816 KB
testcase_05 AC 80 ms
18,944 KB
testcase_06 AC 81 ms
17,408 KB
testcase_07 AC 0 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 0 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 43 ms
9,984 KB
testcase_13 AC 42 ms
9,856 KB
testcase_14 AC 42 ms
9,984 KB
testcase_15 AC 45 ms
9,856 KB
testcase_16 AC 38 ms
7,808 KB
testcase_17 AC 53 ms
9,216 KB
testcase_18 AC 61 ms
9,984 KB
testcase_19 AC 66 ms
10,880 KB
testcase_20 AC 70 ms
11,648 KB
testcase_21 AC 79 ms
12,544 KB
testcase_22 AC 83 ms
13,184 KB
testcase_23 AC 88 ms
14,080 KB
testcase_24 AC 95 ms
14,976 KB
testcase_25 AC 100 ms
15,744 KB
testcase_26 AC 130 ms
16,640 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 35 ms
9,984 KB
testcase_31 AC 41 ms
8,448 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 0 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 0 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long int int64;

typedef struct pairingHeapNode{
  void *val;
  struct pairingHeapNode *next;
  struct pairingHeapNode *child;
} heapNode;

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

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

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

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

heap* newHeap(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(heap *h){
  return h->heapSize;
}

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

heapNode* merge(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)){
    b->next=a->child;
    a->child=b;
    return a;
  } else {
    a->next=b->child;
    b->child=a;
    return b;
  }
}

void push(heap *h,void *v){
  heapNode *p=newHeapNode(v,h->size);
  h->heapSize++;
  h->root=merge(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;
  free(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=merge(r,merge(a,b,cmp),cmp);
    lst=next;
  }
  if(lst!=NULL){
    r=merge(r,lst,cmp);
  }
  h->root=r;
  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(getSize(small)>=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