結果

問題 No.649 ここでちょっとQK!
ユーザー bal4ubal4u
提出日時 2019-06-02 10:38:13
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 37 ms / 3,000 ms
コード長 1,652 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 30,856 KB
実行使用メモリ 4,396 KB
最終ジャッジ日時 2023-10-17 22:57:21
合計ジャッジ時間 2,376 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 5 ms
4,348 KB
testcase_04 AC 32 ms
4,348 KB
testcase_05 AC 32 ms
4,348 KB
testcase_06 AC 20 ms
4,396 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 15 ms
4,348 KB
testcase_13 AC 14 ms
4,348 KB
testcase_14 AC 13 ms
4,348 KB
testcase_15 AC 19 ms
4,348 KB
testcase_16 AC 13 ms
4,348 KB
testcase_17 AC 17 ms
4,348 KB
testcase_18 AC 20 ms
4,348 KB
testcase_19 AC 23 ms
4,348 KB
testcase_20 AC 24 ms
4,348 KB
testcase_21 AC 26 ms
4,348 KB
testcase_22 AC 29 ms
4,348 KB
testcase_23 AC 31 ms
4,348 KB
testcase_24 AC 33 ms
4,348 KB
testcase_25 AC 36 ms
4,348 KB
testcase_26 AC 37 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 12 ms
4,348 KB
testcase_31 AC 11 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:7:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    7 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:16:17: note: in expansion of macro 'gc'
   16 |         int c = gc();
      |                 ^~
main.c: In function 'out':
main.c:8:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
    8 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:26:17: note: in expansion of macro 'pc'
   26 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: 649 ここでちょっとQK
// 2019.6.2 bal4u

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
long long in()    // 非負整数の入力
{
	long long n = 0;
	int c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

void out(long long n)  // 非負整数の表示(出力)
{
	int i;
	char b[40];

	if (!n) pc('0');
	else {
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

#define MAX 200005
long long que[2][MAX]; int qsize[2];

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

void min_heapify(int id, int i)
{
	int l, r, min;

	l = LEFT(i), r = RIGHT(i);
	if (l < qsize[id] && que[id][l] < que[id][i]) min = l; else min = i;
	if (r < qsize[id] && que[id][r] < que[id][min]) min = r;
	if (min != i) {
		long long t = que[id][i]; que[id][i] = que[id][min]; que[id][min] = t;
		min_heapify(id, min);
	}
}

void deq(int id)
{
	que[id][0] = que[id][--qsize[id]];
	min_heapify(id, 0);
}

void enq(int id, long long t)
{
	int i, min;

	i = qsize[id]++;
	que[id][i] = t;
	while (i > 0 && que[id][min = PARENT(i)] > que[id][i]) {
		long long t = que[id][i]; que[id][i] = que[id][min]; que[id][min] = t;
		i = min;
	}
}

int main()
{
	int f, q, Q, K;
	
	Q = (int)in(), K = (int)in();
	f = 0; while (Q--) {
		q = gc(), gc();
		if (q == '1') {
			enq(0, -in());
			if (++f >= K) {
				enq(1, -que[0][0]);
				deq(0);
			}
		} else {
			if (qsize[1] == 0) pc('-'), pc('1'), pc('\n');
			else out(que[1][0]), deq(1);
		}
	}
	return 0;
}
0