結果

問題 No.1888 Odd Insertion
ユーザー 👑 ygussanyygussany
提出日時 2022-03-06 19:18:36
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,824 bytes
コンパイル時間 1,561 ms
コンパイル使用メモリ 32,352 KB
実行使用メモリ 29,240 KB
最終ジャッジ日時 2023-09-28 10:30:48
合計ジャッジ時間 26,570 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,656 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 28 ms
5,696 KB
testcase_07 AC 27 ms
5,736 KB
testcase_08 AC 97 ms
21,228 KB
testcase_09 AC 95 ms
24,344 KB
testcase_10 AC 97 ms
19,196 KB
testcase_11 AC 96 ms
24,360 KB
testcase_12 AC 98 ms
19,112 KB
testcase_13 AC 97 ms
21,332 KB
testcase_14 AC 95 ms
24,264 KB
testcase_15 AC 99 ms
18,620 KB
testcase_16 AC 109 ms
19,140 KB
testcase_17 AC 98 ms
19,164 KB
testcase_18 AC 111 ms
19,512 KB
testcase_19 AC 105 ms
22,536 KB
testcase_20 AC 125 ms
18,532 KB
testcase_21 AC 110 ms
22,684 KB
testcase_22 AC 121 ms
17,904 KB
testcase_23 AC 116 ms
20,408 KB
testcase_24 AC 117 ms
20,368 KB
testcase_25 AC 117 ms
20,364 KB
testcase_26 AC 93 ms
18,304 KB
testcase_27 AC 89 ms
29,240 KB
testcase_28 AC 102 ms
7,308 KB
testcase_29 AC 52 ms
12,616 KB
testcase_30 AC 1,957 ms
22,936 KB
testcase_31 AC 1,962 ms
29,184 KB
testcase_32 AC 83 ms
18,184 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <sys/time.h>

void add_BIT(int N, int BIT[], int i, int k)
{
	while (i <= N) {
		BIT[i] += k;
		i += (i & -i);
	}
}

int sum_BIT(int BIT[], int r)
{
	int sum = 0;
	while (r > 0) {
		sum += BIT[r];
		r -= (r & -r);
	}
	return sum;
}

void add_xor_BIT(int N, int BIT[], int i)
{
	while (i <= N) {
		BIT[i] ^= 1;
		i += (i & -i);
	}
}

int sum_xor_BIT(int BIT[], int r)
{
	int sum = 0;
	while (r > 0) {
		sum ^= BIT[r];
		r -= (r & -r);
	}
	return sum;
}

struct timeval t;
double beg, cur;

int memo[200001], r;

int recursion(int N, int l, int P_inv[], int BIT[], int ans[])
{
	if (l > N) return 1;
	else if (memo[l] != 0) return 0;
	
	gettimeofday(&t, NULL);
	cur = t.tv_sec + (double)t.tv_usec / 1000000;
	if (cur - beg > 1.95) return 0;
	
	int i;
	if (sum_xor_BIT(BIT, P_inv[l] - 1) == 0) {
		add_xor_BIT(N, BIT, P_inv[l]);
		ans[0] = l;
		if (recursion(N, l + 1, P_inv, BIT, &(ans[1])) != 0) return 1;
		add_xor_BIT(N, BIT, P_inv[l]);
	}
	if (P_inv[l] % 2 == 0) {
		for (i = l + 1; i < r; i++) {
        	gettimeofday(&t, NULL);
        	cur = t.tv_sec + (double)t.tv_usec / 1000000;
        	if (cur - beg > 1.95) return 0;
			if (sum_xor_BIT(BIT, P_inv[i] - 1) != 0) break;
			add_xor_BIT(N, BIT, P_inv[i]);
			ans[i-l-1] = i;
		
			if (sum_xor_BIT(BIT, P_inv[l] - 1) == 0) {
				add_xor_BIT(N, BIT, P_inv[l]);
				ans[i-l] = l;
				if (recursion(N, i + 1, P_inv, BIT, &(ans[i-l+1])) != 0) return 1;
				add_xor_BIT(N, BIT, P_inv[l]);
			}
		}
	} else {
		for (i = l + 1; i <= N; i++) {
        	gettimeofday(&t, NULL);
        	cur = t.tv_sec + (double)t.tv_usec / 1000000;
        	if (cur - beg > 1.95) return 0;
			if (sum_xor_BIT(BIT, P_inv[i] - 1) != 0) break;
			add_xor_BIT(N, BIT, P_inv[i]);
			ans[i-l-1] = i;
		
			if (sum_xor_BIT(BIT, P_inv[l] - 1) == 0) {
				add_xor_BIT(N, BIT, P_inv[l]);
				ans[i-l] = l;
				if (recursion(N, i + 1, P_inv, BIT, &(ans[i-l+1])) != 0) return 1;
				add_xor_BIT(N, BIT, P_inv[l]);
			}
		}
	}
	for (i--; i > l; i--) add_xor_BIT(N, BIT, P_inv[i]);
	
	memo[l] = -1;
	while (memo[r] != 0) r--;
	return 0;
}

int solve(int N, int P[], int ans[])
{
	static int i, P_inv[200001], BIT[200001];
	for (i = 1, memo[0] = 0; i <= N; i++) {
		P_inv[P[i]] = i;
		BIT[i] = 0;
		memo[i] = 0;
	}
	r = N;
	return recursion(N, 1, P_inv, BIT, &(ans[1]));
}

int main()
{
	gettimeofday(&t, NULL);
	beg = t.tv_sec + (double)t.tv_usec / 1000000;
	
	int i, N, P[200001], P_inv[200001], ans[200001], BIT[200001] = {};
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d", &(P[i]));
	if (solve(N, P, ans) == 0) printf("No\n");
	else {
		printf("Yes\n");
		for (i = 1; i <= N; i++) P_inv[P[i]] = i;
		for (i = 1; i <= N; i++) {
			printf("%d %d\n", ans[i], sum_BIT(BIT, P_inv[ans[i]] - 1) + 1);
			add_BIT(N, BIT, P_inv[ans[i]], 1);
		}
	}
	fflush(stdout);
	return 0;
}
0