結果
| 問題 | No.649 ここでちょっとQK! |
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2018-10-21 06:04:05 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 85 ms / 3,000 ms |
| コード長 | 2,687 bytes |
| 記録 | |
| コンパイル時間 | 427 ms |
| コンパイル使用メモリ | 31,844 KB |
| 実行使用メモリ | 8,064 KB |
| 最終ジャッジ日時 | 2024-11-19 03:13:33 |
| 合計ジャッジ時間 | 3,484 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 32 |
ソースコード
#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;
}
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;
}
akakimidori