結果
| 問題 | No.649 ここでちょっとQK! |
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2018-10-05 21:55:43 |
| 言語 | C90 (gcc 12.3.0) |
| 結果 |
AC
|
| 実行時間 | 127 ms / 3,000 ms |
| コード長 | 2,376 bytes |
| 記録 | |
| コンパイル時間 | 812 ms |
| コンパイル使用メモリ | 23,040 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-12 13:07:13 |
| 合計ジャッジ時間 | 5,669 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 32 |
コンパイルメッセージ
main.c: In function ‘run’:
main.c:91:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
91 | scanf("%d%d",&q,&k);
| ^~~~~~~~~~~~~~~~~~~
main.c:96:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
96 | scanf("%d",&c);
| ^~~~~~~~~~~~~~
main.c:99:7: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
99 | scanf("%lld",&v);
| ^~~~~~~~~~~~~~~~
ソースコード
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef long long int int64;
typedef struct binaryHeap{
void *h;
int heapSize;
int maxSize;
size_t size;
int (*cmp)(const void *,const void *);
} heap;
heap* newHeap(size_t size,int (*cmpF)(const void *,const void *)){
const int maxSize=1;
heap *res=(heap *)malloc(sizeof(heap));
res->h=malloc(size*(maxSize+1));
res->heapSize=0;
res->maxSize=maxSize;
res->size=size;
res->cmp=cmpF;
return res;
}
int isEmpty(heap *h){
return h->heapSize==0;
}
void freeHeap(heap *h){
free(h->h);
free(h);
return;
}
inline void heapFunc_swap(void * restrict a,void * restrict b,void * restrict tmp,size_t size){
memcpy(tmp,a,size);
memcpy(a,b,size);
memcpy(b,tmp,size);
return;
}
void push(heap *q,void *p){
if(q->heapSize==q->maxSize){
q->h=realloc(q->h,q->size*(2*q->maxSize+1));
q->maxSize*=2;
}
q->heapSize+=1;
void *h=q->h;
int k=q->heapSize;
size_t size=q->size;
int (*cmp)(const void *,const void *)=q->cmp;
memcpy(h+k*size,p,size);
while(k>1){
if(cmp(h+(k/2)*size,h+k*size)) return;
heapFunc_swap(h+(k/2)*size,h+k*size,h,size);
k/=2;
}
return;
}
void pop(heap *q,void *res){
void *h=q->h;
size_t size=q->size;
memcpy(res,h+size,size);
memcpy(h+size,h+size*q->heapSize,size);
q->heapSize-=1;
int (*cmp)(const void *,const void *)=q->cmp;
int n=q->heapSize;
int k=1;
while(2*k+1<=n){
int next=cmp(h+2*k*size,h+(2*k+1)*size)?2*k:2*k+1;
if(cmp(h+k*size,h+next*size)) return;
heapFunc_swap(h+k*size,h+next*size,h,size);
k=next;
}
if(2*k<=n && cmp(h+2*k*size,h+k*size)) heapFunc_swap(h+k*size,h+2*k*size,h,size);
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(small->heapSize>=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