結果

問題 No.318 学学学学学
ユーザー bal4ubal4u
提出日時 2019-07-24 08:26:15
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 2,867 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 31,124 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2023-09-09 21:59:30
合計ジャッジ時間 2,978 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
8,684 KB
testcase_01 AC 10 ms
8,984 KB
testcase_02 AC 11 ms
9,020 KB
testcase_03 AC 9 ms
8,788 KB
testcase_04 AC 11 ms
8,900 KB
testcase_05 AC 22 ms
9,808 KB
testcase_06 AC 23 ms
9,468 KB
testcase_07 AC 20 ms
9,232 KB
testcase_08 AC 19 ms
9,084 KB
testcase_09 AC 16 ms
8,752 KB
testcase_10 AC 11 ms
4,780 KB
testcase_11 AC 22 ms
9,856 KB
testcase_12 AC 23 ms
9,380 KB
testcase_13 AC 19 ms
9,160 KB
testcase_14 AC 17 ms
8,944 KB
testcase_15 AC 15 ms
8,796 KB
testcase_16 AC 10 ms
4,732 KB
testcase_17 AC 12 ms
4,380 KB
testcase_18 AC 9 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,500 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 0 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 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++;
	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) {
			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) { i--; break; }
					else outs(b-a+1, v), a = b+1;
				}
			}
			if (b < a) 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