結果

問題 No.318 学学学学学
ユーザー bal4ubal4u
提出日時 2019-07-24 08:10:34
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 2,959 bytes
コンパイル時間 903 ms
コンパイル使用メモリ 31,204 KB
実行使用メモリ 41,948 KB
最終ジャッジ日時 2023-09-09 21:46:38
合計ジャッジ時間 4,288 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
24,288 KB
testcase_01 AC 35 ms
34,160 KB
testcase_02 AC 38 ms
36,304 KB
testcase_03 AC 32 ms
31,572 KB
testcase_04 AC 37 ms
34,748 KB
testcase_05 AC 58 ms
41,948 KB
testcase_06 AC 57 ms
41,360 KB
testcase_07 AC 54 ms
39,884 KB
testcase_08 AC 48 ms
36,032 KB
testcase_09 AC 36 ms
26,928 KB
testcase_10 AC 12 ms
5,468 KB
testcase_11 AC 58 ms
41,900 KB
testcase_12 AC 55 ms
41,084 KB
testcase_13 AC 51 ms
39,776 KB
testcase_14 AC 44 ms
35,836 KB
testcase_15 AC 33 ms
26,864 KB
testcase_16 AC 11 ms
5,520 KB
testcase_17 AC 11 ms
4,380 KB
testcase_18 AC 8 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 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 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 4999999
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;
	}
	
//for (i = 0; i < w; i++) printf("(%d,%d,%d) ", t[i].v, t[i].a, t[i].b);
//printf("\n");
	
	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