結果
| 問題 | No.649 ここでちょっとQK! |
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2018-09-10 01:19:33 |
| 言語 | C90 (gcc 12.3.0) |
| 結果 |
AC
|
| 実行時間 | 87 ms / 3,000 ms |
| コード長 | 2,216 bytes |
| 記録 | |
| コンパイル時間 | 334 ms |
| コンパイル使用メモリ | 22,656 KB |
| 実行使用メモリ | 7,808 KB |
| 最終ジャッジ日時 | 2024-12-25 10:51:58 |
| 合計ジャッジ時間 | 4,373 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 32 |
コンパイルメッセージ
main.c: In function ‘run’:
main.c:88:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
88 | scanf("%d%d",&q,&k);
| ^~~~~~~~~~~~~~~~~~~
main.c:93:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
93 | scanf("%d",&c);
| ^~~~~~~~~~~~~~
main.c:96:7: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
96 | scanf("%lld",&v);
| ^~~~~~~~~~~~~~~~
ソースコード
#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;
int (*cmp)(const node *,const node *);
} heap;
heapNode* newHeapNode(node *v){
heapNode *res=(heapNode *)malloc(sizeof(heapNode));
res->val=*v;
res->next=NULL;
res->child=NULL;
return res;
}
heap* newHeap(int (*cmp)(const node *,const node *)){
heap *res=(heap *)malloc(sizeof(heap));
res->root=NULL;
res->heapSize=0;
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(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;
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 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(cmpMax);
heap *minHeap=newHeap(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;
}
akakimidori