結果

問題 No.318 学学学学学
ユーザー bal4ubal4u
提出日時 2019-07-23 17:27:54
言語 C
(gcc 12.3.0)
結果
OLE  
実行時間 -
コード長 2,839 bytes
コンパイル時間 1,308 ms
コンパイル使用メモリ 31,220 KB
実行使用メモリ 13,048 KB
最終ジャッジ日時 2023-09-08 11:22:51
合計ジャッジ時間 7,891 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
13,048 KB
testcase_01 AC 11 ms
8,808 KB
testcase_02 AC 11 ms
8,920 KB
testcase_03 AC 10 ms
8,832 KB
testcase_04 AC 10 ms
8,820 KB
testcase_05 AC 23 ms
9,840 KB
testcase_06 AC 24 ms
9,520 KB
testcase_07 AC 22 ms
9,228 KB
testcase_08 AC 21 ms
8,984 KB
testcase_09 AC 17 ms
8,724 KB
testcase_10 AC 11 ms
4,748 KB
testcase_11 OLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:9:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: 備考: in expansion of macro ‘gc’
   17 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:10:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:25:21: 備考: in expansion of macro ‘pc’
   25 |         while (i--) pc(b[i]);
      |                     ^~

ソースコード

diff #

// yukicoder: No.318 学学学学学
// 2019.7.23 bal4u

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

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

void out(int n) { // 非負整数の表示(出力)
	int i; char b[20];
	i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
	while (i--) pc(b[i]);
}

void outs(int k, int v) { // 非負整数の表示(出力)
	int i, ii; char b[20];
	i = 0; while (v) b[i++] = v % 10 + '0', v /= 10;
	while (k--) { pc(' '); ii = i; while (ii--) pc(b[ii]); }
}

#define HASHSIZ 900007
typedef struct { int v, id; } HASH;
HASH hash[HASHSIZ+5], *hashend = hash+HASHSIZ;

int insert(int v, int id) {
	HASH *p = hash + v % HASHSIZ;
	while (p->v) {
		if (p->v == v) return p->id;
		if (++p == hashend) p = hash;
	}
	p->v = v, p->id = id;
	return -1;
}

// 優先度付きキュー
#define QSIZE 110000
#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

typedef struct { int v, b; } QUE;
QUE que[QSIZE+5]; int qsize;

void max_heapify(int i) {
	int l, r, max;
	QUE qt;
	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && que[l].v > que[i].v) max = l; else max = i;
	if (r < qsize && que[r].v > que[max].v) max = r;
	if (max != i) {
		qt = que[i], que[i] = que[max], que[max] = qt;
		max_heapify(max);
	}
}

void deq() {
	que[0] = que[--qsize];
	max_heapify(0);
}

void enq(int v, int b) {
	int i, max;	QUE qt;
	i = qsize++;
	que[i].v = v, que[i].b = b;
	while (i > 0 && que[max = PARENT(i)].v < que[i].v) {
		qt = que[i]; que[i] = que[max], que[max] = qt;
		i = max;
	}
}

typedef struct { int v, a, b; } T;
T t[100005]; int w;

int main()
{
	int i, j, v, a, b, n;

	n = in(); for (i = 0; i < n; i++) {
		if ((j = insert(v = in(), w)) < 0) t[w].v = v, t[w].a = t[w].b = i, w++;
		else t[j].b = i;
	}
	
	i = 0, v = t[0].v, a = 0, b = t[0].b;
	out(v);
	if (b > 0 || v > t[1].v) a = 1;
	else v = t[++i].v, a = t[i].a, b = t[i].b;
	
	while (++i < w) {
		while (b+1 < t[i].a) {
			outs(b-a+1, v);
			a = b+1;
			while (qsize) {
				v = que[0].v, b = que[0].b, deq();
				if (b >= a) break;
			}
		}
		if (b+1 == t[i].a) {
			outs(b-a+1, v);
			if (qsize && que[0].v > t[i].v) {
				v = que[0].v, a = t[i].a, b = que[0].b, deq();
				if (b < t[i].b) enq(t[i].v, t[i].b);
			} else v = t[i].v, a = t[i].a, b = t[i].b;
		} else {
			if (v > t[i].v) {
				if (b < t[i].b) enq(t[i].v, t[i].b);
			} else {
				outs(t[i].a-a, v);
				if (t[i].b < b) enq(v, b);
				v = t[i].v, a = t[i].a, b = t[i].b;
			}
		}
	}
	outs(b-a+1, v), a = b+1;
	while (qsize) {
		if (a <= que[0].b) outs(que[0].b-a+1, que[0].v), a = que[0].b+1;
		deq();
	}
	pc('\n');
	return 0;
}
	
0