結果

問題 No.953 席
ユーザー bal4ubal4u
提出日時 2020-01-13 13:57:47
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 3,210 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 31,196 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-08-22 17:10:32
合計ジャッジ時間 10,914 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,712 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 990 ms
4,384 KB
testcase_03 AC 243 ms
4,376 KB
testcase_04 AC 719 ms
4,380 KB
testcase_05 AC 22 ms
4,376 KB
testcase_06 AC 1,017 ms
4,380 KB
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.13 bal4u

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

typedef long long ll;

#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 { ll a; int 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, ll 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] > 0) {
		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;
	}
//	if (top == end) { printf("!Panic\n"); exit(1); }
	ans = sq[top++];
OK:	while (top < end) enq(SEAT, 0, sq[top++]);
//printf("next_seat %d\n", ans);
	return ans;
}

int main()
{
	N = in(), K1 = in(), K2 = in();
	
	int n = 0;
	seat[K1].n = ++n, seat[K2].n = ++n;
	int i, j = 2; for (i = 1; j < N; ++i) {
		if (K2 < K1) {
			if (K1+i <= N) seat[K1+i].n = ++n, ++j;
			if (K2-i >= 1) seat[K2-i].n = ++n, ++j;
		} else {
			if (K1-i >= 1) seat[K1-i].n = ++n, ++j;
			if (K2+i <= N) seat[K2+i].n = ++n, ++j;
		}
	}
	for (i = 1; i <= N; ++i) enq(SEAT, 0, seat[i].n);
	
	int Q = in();
	ll pre = -1;
	while (Q--) {
		ll a = in(); int b = in();
//printf("a=%lld, b=%d, pre=%lld\n", a, b, pre);		
		if (a < pre) a = pre;
		if (qsize[SEAT] == 0 && a < que[MAN][0].a) a = que[MAN][0].a;
		pre = a;
//printf("qsize(MAN)=%d, top=%lld,%d\n", qsize[MAN], que[MAN][0].a, que[MAN][0].n);
		while (qsize[MAN] > 0 && que[MAN][0].a <= a) {
			n = que[MAN][0].n;
			deq(MAN), seat[n].f = 0;
			enq(SEAT, 0, n);
		}
//printf("qsize(SEAT)=%d, top=%d\n", qsize[SEAT], que[SEAT][0].n);
		n = next_seat();
//printf("next_seat=%d\n", n);
		seat[n].f = 1, enq(MAN, a+b, n);
		out(n);
	}
	return 0;
}
0