結果

問題 No.318 学学学学学
ユーザー bal4ubal4u
提出日時 2019-07-24 10:21:52
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 3,006 bytes
コンパイル時間 894 ms
コンパイル使用メモリ 31,216 KB
実行使用メモリ 18,872 KB
最終ジャッジ日時 2023-09-10 04:13:19
合計ジャッジ時間 4,143 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
15,612 KB
testcase_01 AC 19 ms
17,332 KB
testcase_02 AC 19 ms
17,460 KB
testcase_03 AC 18 ms
17,116 KB
testcase_04 AC 19 ms
17,504 KB
testcase_05 AC 30 ms
18,872 KB
testcase_06 AC 32 ms
18,488 KB
testcase_07 AC 29 ms
18,180 KB
testcase_08 AC 28 ms
17,880 KB
testcase_09 AC 25 ms
16,596 KB
testcase_10 AC 12 ms
5,520 KB
testcase_11 AC 29 ms
18,772 KB
testcase_12 AC 30 ms
18,284 KB
testcase_13 AC 28 ms
18,072 KB
testcase_14 AC 27 ms
17,892 KB
testcase_15 AC 24 ms
16,656 KB
testcase_16 AC 12 ms
5,596 KB
testcase_17 AC 13 ms
4,376 KB
testcase_18 AC 9 ms
4,624 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 6 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 0 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 0 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:7:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    7 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:15:24: 備考: in expansion of macro ‘gc’
   15 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:8:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    8 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:23:21: 備考: in expansion of macro ‘pc’
   23 |         while (i--) pc(b[i]);
      |                     ^~

ソースコード

diff #

// yukicoder: No.318 学学学学学
// 2019.7.24 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

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 2000003
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 A[100005];

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

	n = in(); for (i = 0; i < n; i++) A[i] = in();
	t[0].v = A[0], t[0].a = t[0].b = 0, insert(A[0], 0);
	j = 0, w = 1;;
	for (i = 1; i < n; i++) {
		if (A[i] == A[i-1]) t[j].b++;
		else if ((j = insert(A[i], w)) < 0) j = w++, t[j].v = A[i], t[j].a = t[j].b = i;
		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++;
	else {
		if (t[++i].b < b) enq(v, b);
		v = t[i].v, a = t[i].a, b = t[i].b;
	}
	while (++i < w) {
		if (t[i].a <= b) {
next:		if (v < t[i].v) {  // もっと強いやつが重ねてきた
				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;
			} else  {          // 弱いやつが重ねてきた
				if (b < t[i].b) enq(t[i].v, t[i].b);
			}
		} else {	// (v,a,b)が終了
			outs(b-a+1, v), a = b+1;
			// queかt[i]から候補を選ぶ
			while (qsize) {
				v = que[0].v, b = que[0].b, deq();
				if (a <= b) {
					if (t[i].a <= b) goto next;
					else outs(b-a+1, v), a = b+1;
				}
			}
			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