結果

問題 No.649 ここでちょっとQK!
ユーザー akakimidoriakakimidori
提出日時 2018-09-10 01:34:03
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 86 ms / 3,000 ms
コード長 2,556 bytes
コンパイル時間 652 ms
コンパイル使用メモリ 25,188 KB
実行使用メモリ 6,600 KB
最終ジャッジ日時 2023-08-26 16:14:57
合計ジャッジ時間 4,507 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 24 ms
6,384 KB
testcase_04 AC 61 ms
6,568 KB
testcase_05 AC 59 ms
6,568 KB
testcase_06 AC 59 ms
6,600 KB
testcase_07 AC 0 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 0 ms
4,380 KB
testcase_11 AC 0 ms
4,376 KB
testcase_12 AC 32 ms
4,376 KB
testcase_13 AC 32 ms
4,376 KB
testcase_14 AC 30 ms
4,380 KB
testcase_15 AC 34 ms
4,376 KB
testcase_16 AC 33 ms
4,380 KB
testcase_17 AC 45 ms
4,408 KB
testcase_18 AC 48 ms
4,740 KB
testcase_19 AC 53 ms
4,892 KB
testcase_20 AC 58 ms
5,108 KB
testcase_21 AC 64 ms
5,352 KB
testcase_22 AC 69 ms
5,548 KB
testcase_23 AC 72 ms
5,712 KB
testcase_24 AC 76 ms
6,036 KB
testcase_25 AC 83 ms
6,368 KB
testcase_26 AC 86 ms
6,596 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 23 ms
4,380 KB
testcase_31 AC 32 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long int int64;
typedef int64 node;

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

typedef struct pairingHeap{
  heapNode *root;
  int heapSize;
  heapNode *freeList;
  int (*cmp)(const node *,const node *);
} heap;

heapNode* newHeapNode(heap *h,node *v){
  heapNode *res=h->freeList;
  h->freeList=h->freeList->next;
  res->val=*v;
  res->next=NULL;
  res->child=NULL;
  return res;
}

void freeHeapNode(heap *h,heapNode *r){
  r->next=h->freeList;
  h->freeList=r;
  return;
}

heap* newHeap(int maxSize,int (*cmp)(const node *,const node *)){
  heap *res=(heap *)malloc(sizeof(heap));
  res->root=NULL;
  res->heapSize=0;
  res->freeList=(heapNode *)malloc(sizeof(heapNode)*maxSize);
  int i;
  for(i=0;i+1<maxSize;i++) res->freeList[i].next=res->freeList+i+1;
  res->freeList[maxSize-1].next=NULL;
  res->cmp=cmp;
  return res;
}

heapNode* merge(heapNode *a,heapNode *b,int (*cmp)(const node *a,const node *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,node *v){
  heapNode *p=newHeapNode(h,v);
  h->heapSize++;
  h->root=merge(h->root,p,h->cmp);
  return;
}

void pop(heap *h,node *v){
  *v=h->root->val;
  heapNode *lst=h->root->child;
  int (*cmp)(const node *a,const node *b)=h->cmp;
  freeHeapNode(h,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 node *a,const node *b){
  return *a<=*b;
}

int cmpMax(const node *a,const node *b){
  return !cmpMin(a,b);
}

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

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