結果

問題 No.953 席
ユーザー bal4ubal4u
提出日時 2020-01-13 11:18:07
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,962 bytes
コンパイル時間 1,088 ms
コンパイル使用メモリ 31,068 KB
実行使用メモリ 8,884 KB
最終ジャッジ日時 2023-08-22 11:16:13
合計ジャッジ時間 11,096 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,884 KB
testcase_01 AC 0 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// yuki 953 席
// 2019.1.12 bal4u

#include <stdio.h>

#if 1
int getchar_unlocked(void);
int putchar_unlocked(int c);
#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[30];

	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 100010
#define MAN 	0
#define SEAT	1
typedef struct { int a, n; } QUE;
QUE que[2][MAX]; int qsize[2];

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

int pqcmp(int id, int u, int v);

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

	l = LEFT(i), r = RIGHT(i);
	if (l < qsize[id] && pqcmp(id, l, i)) mi = l; else mi = i;
	if (r < qsize[id] && pqcmp(id, r, mi)) mi = r;
	if (mi != i) {
		QUE qt = que[id][i];
		que[id][i] = que[id][mi]; que[id][mi] = qt;
		min_heapify(id, mi);
	}
}

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

void enq(int id, int a, int n) {
	int i, mi;

	i = qsize[id]++;
	que[id][i].a = a, que[id][i].n = n;
	while (i > 0 && pqcmp(id, i, mi = PARENT(i))) {
		QUE qt = que[id][i];
		que[id][i] = que[id][mi]; que[id][mi] = qt;
		i = mi;
	}
}

typedef struct { int n; char f; } T;
T seat[MAX];
int N, K1, K2;

int pqcmp(int id, int u, int v) {
	if (id == MAN) return que[id][u].a < que[id][v].a;
	return seat[que[id][u].n].n < seat[que[id][v].n].n;
}

int sq[MAX], top, end;
int next_seat() {
	int n, ans;
	
	top = end = 0;
	while (qsize[SEAT]) {
		n = que[SEAT][0].n, deq(SEAT);
		if (n == 1) {
			if (seat[n+1].f == 0) { ans = n; goto OK; }
		} else if (n == N) {
			if (seat[n-1].f == 0) { ans = n; goto OK; }
		} else {
			if (seat[n-1].f == 0 && seat[n+1].f == 0) { ans = n; goto OK; }
		}
		sq[end++] = n;
	}
	ans = sq[top++];
OK:	while (top < end) enq(SEAT, 0, sq[top++]);
//printf("next_seat %d\n", ans);
	return ans;
}

int main()
{
	int i, j, n;
	
	N = in(), K1 = in(), K2 = in();
	
	n = 0;
	seat[K1].n = ++n, seat[K2].n = ++n;
	enq(SEAT, 0, K1), enq(SEAT, 0, K2);
	j = 2; for (i = 1; j < N; ++i) {
		if (K2 < K1) {
			if (K1 + i <= N) {
				seat[K1+i].n = ++n, ++j;
				enq(SEAT, 0, K1+i);
			}
			if (K2 - i >= 1) {
				seat[K2-i].n = ++n, ++j;
				enq(SEAT, 0, K2-i);
			}
		} else {
			if (K1 - i >= 1) {
				seat[K1-i].n = ++n, ++j;
				enq(SEAT, 0, K1-i);
			}
			if (K2 + i <= N) {
				seat[K2+i].n = ++n, ++j;
				enq(SEAT, 0, K2+i);
			}
		}
	}

	int Q = in();
	while (Q--) {
		int a = in(), b = in();
		if (qsize[SEAT] == 0 && que[MAN][0].a > a) a = que[MAN][0].a;
		while (qsize[MAN] > 0 && que[MAN][0].a <= a) {
			n = que[MAN][0].n;
			deq(MAN), seat[n].f = 0;
			enq(SEAT, 0, n);
		}
		n = next_seat();
		seat[n].f = 1, enq(MAN, a+b, n);
		out(n);
	}
	return 0;
}
0