結果

問題 No.608 God's Maze
ユーザー hirakich1048576hirakich1048576
提出日時 2017-12-07 00:48:46
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 10,056 bytes
コンパイル時間 1,116 ms
コンパイル使用メモリ 34,816 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-07 05:28:59
合計ジャッジ時間 2,147 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 0 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 0 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 0 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 0 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 0 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 0 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 3 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 3 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 1 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 1 ms
5,376 KB
testcase_53 AC 1 ms
5,376 KB
testcase_54 AC 1 ms
5,376 KB
testcase_55 AC 0 ms
5,376 KB
testcase_56 AC 1 ms
5,376 KB
testcase_57 AC 1 ms
5,376 KB
testcase_58 AC 1 ms
5,376 KB
testcase_59 AC 1 ms
5,376 KB
testcase_60 AC 1 ms
5,376 KB
testcase_61 AC 1 ms
5,376 KB
testcase_62 AC 1 ms
5,376 KB
testcase_63 AC 1 ms
5,376 KB
testcase_64 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
cat <<EOF >mistaken-paste
*/

#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>

#define BIG 2000000007

#define MOD 1000000007
typedef unsigned long long ull;
typedef   signed long long sll;

#define N_MAX 100000
#define M_MAX 200

typedef struct {
	int a;
	int b;
} hw;

typedef struct {
	sll a;
	sll b;
} hwll;


typedef struct {
	ull s;
	ull t;
	int c;
} struct_a;



const hw vector8[8] = {
	{-1, -1},
	{-1,  0},
	{-1, +1},
	{ 0, -1},
	{ 0, +1},
	{+1, -1},
	{+1,  0},
	{+1, +1}
};

ull n, m;
ull h, w;
ull k;
ull vua, vub, vuc, vud, vue, vuf;
sll vsa, vsb, vsc, vsd, vse, vsf;
long double vra, vrb, vrc;
// ull a[N_MAX];
// sll a[N_MAX];
// ull a[N_MAX][N_MAX];
// ull wall[N_MAX][M_MAX];
// ull b[N_MAX];
// sll b[N_MAX];
// ull b[N_MAX][N_MAX];
// sll b[N_MAX][N_MAX];
char s[N_MAX + 1];
// char t[N_MAX + 1];
size_t slen;
// size_t tlen;
// char s[N_MAX][M_MAX + 1];

// ull dp[N_MAX];
// sll dp[N_MAX];
bool dp[N_MAX];
// ull dq[N_MAX];
// ull dp[N_MAX + 1][N_MAX + 1];
// ull dp[N_MAX][M_MAX + 1];

// hw arr[N_MAX];
// hwll arr[N_MAX];
// hw brr[M_MAX];

// ull bitdp[1 << N_MAX];

// ull digitdp[102][   2][    2];
//          pos  less  carry


// struct_a arr[N_MAX * 2];
// struct_b brr[N_MAX * 2];

void swap_adj (ull *a, ull *b) {
	ull tmp = *b;
	*b = *a;
	*a = tmp;
	return;
}

ull divide (ull a, ull b) {
	ull x = MOD - 2;
	ull ans = 1;
	while (x) {
		if (x & 1) ans = (ans * b) % MOD;
		b = (b * b) % MOD;
		x /= 2;
	}
	return (a * ans) % MOD;
}

int digits (ull x) {
	int i = 1;
	while (x >= 10) {
		x /= 10;
		i++;
	}
	return i;
}

ull umin (ull x, ull y) {
	return (x < y) ? x : y;
}

ull umax (ull x, ull y) {
	return (x > y) ? x : y;
}

sll smin (sll x, sll y) {
	return (x < y) ? x : y;
}

sll smax (sll x, sll y) {
	return (x > y) ? x : y;
}

ull gcd (ull x, ull y) {
	if (x < y) {
		return gcd(y, x);
	} else if (y == 0) {
		return x;
	} else {
		return gcd(y, x % y);
	}
}

ull bitpow (ull a, ull x, ull modulo) {
	ull result = 1;
	while (x) {
		if (x & 1) {
			result *= a;
			result %= modulo;
		}
		x /= 2;
		a = (a * a) % modulo;
	}
	return result;
}

// int nextroute (int arr[]) {
// 	int i = n - 1;
// 	int j, x;
// 	while (arr[i - 1] > arr[i]) i--;

// 	x = n;
// 	for (j = i; j < n; j++) {
// 		if (arr[j] < arr[i - 1]) continue;
// 		if (x == n || arr[x] > arr[j]) x = j;
// 	}
// 	arr[i - 1] ^= arr[x];
// 	arr[x] ^= arr[i - 1];
// 	arr[i - 1] ^= arr[x];

// 	qsort(&arr[i], n - i, sizeof(int), comp);
// 	return 0;
// }

int targetdig (ull x, int index /* 1-indexed */) {
	// static...?
	int posmax = digits(x);
	if (posmax < index) return -1;
	while (posmax > index) {
		posmax--;
		x /= 10;
	}
	return x % 10;
}

int charcomp (const char left, const char right) {
	if (left < right) {
		return -1;
	} else if (left > right) {
		return +1;
	} else {
		return 0;
	}
}

int pcharcomp (const char *left, const char *right) {
	return charcomp(*left, *right);
}

int intcomp (const int left, const int right) {
	if (left < right) {
		return -1;
	} else if (left > right) {
		return +1;
	} else {
		return 0;
	}
}

int pintcomp (const int *left, const int *right) {
	return intcomp(*left, *right);
}

int ullcomp (const ull left, const ull right) {
	if (left < right) {
		return -1;
	} else if (left > right) {
		return +1;
	} else {
		return 0;
	}
}

int pullcomp (const ull *left, const ull *right) {
	return ullcomp(*left, *right);
}

int sllcomp (const sll left, const sll right) {
	if (left < right) {
		return -1;
	} else if (left > right) {
		return +1;
	} else {
		return 0;
	}
}

int psllcomp (const sll *left, const sll *right) {
	return sllcomp(*left, *right);
}

int phwAcomp (const hw *left, const hw *right) {
	return intcomp(left->a, right->a);
}

int phwBcomp (const hw *left, const hw *right) {
	return intcomp(left->b, right->b);
}

int phwABcomp (const hw *left, const hw *right) {
	int x = phwAcomp(left, right);
	if (x) return x;
	return phwBcomp(left, right);
}

int phwllAcomp (const hwll *left, const hwll *right) {
	return sllcomp(left->a, right->a);
}

int phwllBcomp (const hwll *left, const hwll *right) {
	return sllcomp(left->b, right->b);
}

int phwllABcomp (const hwll *left, const hwll *right) {
	int x = phwllAcomp(left, right);
	if (x) return x;
	return phwllBcomp(left, right);
}

int pstrAcomp (const struct_a *left, const struct_a *right) {
	int x;
	if (x = ullcomp(left->t, right->t)) return x;
	if (x = ullcomp(left->s, right->s)) return x;
	if (x = intcomp(left->c, right->c)) return x;
	return 0;
}

int bitlet (char c) {
	return (1 << (c - 'a'));
}

ull ullabs (ull a, ull b) {
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}

sll sllabs (sll a, sll b) {
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}

sll nibutanlobo (bool (*func)(sll arg), sll ok, sll ng) {
	while (sllabs(ok, ng) > 1) {
		sll med = (ok + ng) / 2;
		if (func(med)) {
			ok = med;
		} else {
			ng = med;
		}

		// printf("debug: [%lld %lld)\n", ok, ng);
	}

	if (!func(ok)) return ok * 2 - ng;
	return ok;
}


ull accumu[N_MAX];
ull accumu_sum (int left, int right) {
	return accumu[right] - (left ? accumu[left - 1] : 0);
}

ull solve () {
	sll i, j, ki;
	ull result = BIG;
	// sll result;
	ull sum = 0;
	// qsortの際には"p"ullcompを使う

	for (i = 0; i < slen; i++) {
		ull item = ((s[i] == '#') ? 1 : 0);
		if (i == 0) {
			accumu[i] = item;
		} else {
			accumu[i] = accumu[i - 1] + item;
		}
	}

	n--;

	if (accumu_sum(0, slen - 1) == 1 && accumu[n] == 1 && (n == 0 || accumu[n - 1] == 0)) {
		puts("3");
		return 0;
	}

	result = BIG;
	// goal stands left
	if (n > 0) {
		ull goal;
		ull repeatpath = 0;
		
		ull tilesum = (n - accumu_sum(0, n - 1)) + accumu_sum(n, slen - 1);

		// for (i = 0; i < slen; i++) {
		// 	printf("%llu ", accumu[i]);
		// }
		// puts("");
		// printf("%llu\n", tilesum);

		for (goal = 0; goal <= n; goal++) {
			if (s[goal] == '#') break;
			tilesum--;
		}
		if (tilesum % 2 == 1) {
			goal++;
		}

		// printf("LEFT GOAL: %llu\n", goal);

		if (goal <= n) {
			for (i = 0; i < slen; i++) {
				dp[i] = ((s[i] == '#') ? true : false);
				if (goal <= i && i <= n - 1) {
					dp[i] = !dp[i];
				}
				// putchar(dp[i] ? '#' : '.');
			}
			// puts("");

			sll pairleft, pairright;
			sll rightest = n;

			// for (pairright = slen - 1; pairright > rightest; pairright--) {
			// 	if (dp[pairright]) break;
			// }
			// if (pairright > rightest) {

			// 	pairleft = rightest;
			// 	while (pairleft >= 0 && !dp[pairleft]) pairleft--;

			// 	if (pairleft >= 0) {
			// 		dp[pairleft] = dp[pairright] = false;
			// 		repeatpath += (pairright - pairleft);

			// 		rightest = pairright;

			// 		// printf(":!: %lld,%lld\n", pairleft, pairright);

			// 	}
			// }

			// printf("pr(%lld) - pl(%lld): %lld\n", pairright, pairleft, repeatpath);

			pairleft = slen;
			pairright = slen + 1;
			for (i = slen - 1; i >= 0; i--) {
				if (!dp[i]) continue;

				if (pairleft < pairright) {
					if (pairleft < slen && pairleft > rightest) {
						repeatpath += (pairleft - smax(i, rightest)) * 2;
						// printf("%lld~%lld\n", smax(i, rightest), pairleft);
					}

					pairright = i;
				} else {
					repeatpath += (pairright - i);
					pairleft = i;
				}

				// printf("%lld:: %llu (pl: %lld pr: %lld)\n", i, repeatpath, pairleft, pairright);
			}
			if (pairleft < slen && pairleft > rightest) {
				repeatpath += (pairleft - rightest) * 2;
			}

			// printf("(%lld - %lld) + %lld * 2\n", n, goal, repeatpath);

			ull maybe = (n - goal) + repeatpath * 2;
			if (result > maybe) result = maybe;
		}
	}

	// goal stands right
	if (n < slen - 1) {
		ull goal;
		ull repeatpath = 0;
		
		ull tilesum = accumu_sum(0, n) + ((slen - n - 1) - accumu_sum(n + 1, slen - 1));

		for (goal = slen - 1; goal >= n; goal--) {
			if (s[goal] == '#') break;
			tilesum--;
		}
		if (tilesum % 2 == 1) {
			goal--;
		}

		if (goal >= n) {
			for (i = 0; i < slen; i++) {
				dp[i] = ((s[i] == '#') ? true : false);
				if (n + 1 <= i && i <= goal) {
					dp[i] = !dp[i];
				}
			}

			sll pairleft, pairright;
			sll leftest = n;

			// for (pairleft = 0; pairleft > leftest; pairleft++) {
			// 	if (dp[pairleft]) break;
			// }
			// if (pairleft > leftest) {

			// 	pairright = leftest;
			// 	while (pairright < slen && !dp[pairright]) pairright++;

			// 	if (pairright < slen) {
			// 		dp[pairleft] = dp[pairright] = false;
			// 		repeatpath += (pairright - pairleft);

			// 		leftest = pairleft;
			// 	}
			// }

			pairleft = -2;
			pairright = -1;
			for (i = 0; i < slen; i++) {
				if (!dp[i]) continue;

				if (pairleft < pairright) {
					if (pairright >= 0 && pairright < leftest) {
						repeatpath += (smin(i, leftest) - pairright) * 2;
						// printf("%lld~%lld\n", pairright, smax(i, leftest));
					}

					pairleft = i;
				} else {
					repeatpath += (i - pairleft);
					pairright = i;
				}

				// printf("%lld:: %llu (pl: %lld pr: %lld)\n", i, repeatpath, pairleft, pairright);

			}

			// printf("(%lld - %lld) + %lld * 2\n", n, goal, repeatpath);


			if (pairright >= 0 && pairright < leftest) {
				repeatpath += (leftest - pairright) * 2;
			}

			ull maybe = (goal - n) + repeatpath * 2;
			if (result > maybe) result = maybe;
		}
	}

	printf("%llu\n", result);
	// puts(s);

	return 0;

	success:
	puts("Yes");
	// printf("%llu\n", result);
	return 0;

	fail:
	puts("No");
	return 1;
}

int main (void) {
	int i, j;
	int x, y;

	// scanf("%llu%llu", &h, &w);
	scanf("%llu", &n, &k);
	// scanf("%llu%llu", &vua, &vub);
	scanf("%s", s);
	slen = strlen(s);
	// scanf("%s", t);
	// tlen = strlen(t);
	// scanf("%llu", &k);
	// for (i = 0; i < n; i++) {
	// 	scanf("%llu", &a[i]);
	// }
	// for (i = 0; i < m; i++) {
	// 	scanf("%lld", &b[i]);
	// }
	// for (i = 0; i < n; i++) {
	// 	scanf("%llu%llu%d", &arr[i].s, &arr[i].t, &arr[i].c);
	// 	arr[i].c--;
	// }

	solve();

	// for (i = 0; i < n; i++) {
	// 	// scanf("%llu%llu", &vua, &vub);
	// 	scanf("%s", s);
	// 	solve();
	// }

	return 0;
}
0