結果

問題 No.873 バイナリ、ヤバいなり!w
ユーザー bal4ubal4u
提出日時 2019-08-31 11:17:16
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,046 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 30,748 KB
実行使用メモリ 9,296 KB
最終ジャッジ日時 2023-09-06 11:57:38
合計ジャッジ時間 3,025 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
4,380 KB
testcase_01 AC 40 ms
4,380 KB
testcase_02 AC 173 ms
9,296 KB
testcase_03 WA -
testcase_04 AC 0 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 0 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 35 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 0 ms
4,380 KB
testcase_11 AC 0 ms
4,376 KB
testcase_12 AC 10 ms
4,500 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 0 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 0 ms
4,376 KB
testcase_17 AC 0 ms
4,376 KB
testcase_18 AC 0 ms
4,376 KB
testcase_19 AC 0 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 0 ms
4,384 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 0 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 0 ms
4,380 KB
testcase_28 AC 0 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 0 ms
4,380 KB
testcase_31 AC 0 ms
4,380 KB
testcase_32 AC 5 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 21 ms
4,380 KB
testcase_35 AC 182 ms
9,260 KB
testcase_36 AC 93 ms
6,684 KB
testcase_37 WA -
testcase_38 AC 181 ms
9,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:8:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:16:24: 備考: in expansion of macro ‘gc’
   16 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘outs’ 内:
main.c:9:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:21:33: 備考: in expansion of macro ‘pc’
   21 | void outs(char *s) { while (*s) pc(*s++); }
      |                                 ^~

ソースコード

diff #

// yukicoder: 873 バイナリ、ヤバいなり!w
// 2019.8.31 bal4u

#include <stdio.h>
#include <stdlib.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 outs(char *s) { while (*s) pc(*s++); }

#define INF 0x01010101
#define LIM 550
int N;
int a[LIM]; int w;
int dp[600005][2], pre[600005][2];
char s01[LIM];

int ok(int a, int b, int zero) {   // a は 高評価対象か
	int ea = a & 1, eb = b & 1, t = a <= b;
	int r;
	if (ea && eb) r = t;
	else if (ea) r = 1;
	else if (eb) r = 0;
	else r = !t;
	if (!zero) r = !t;
	return r;
}

void pr(int w) {
	static char *s = s01; char c;
	c = s[w], s[w] = 0;
	outs(s);
	s[w] = c;
	s = (c == '0')? s01+1: s01;
}

int cmp(const void *u, const void *v) {
	int a = (*(int *)u) & 1, b = (*(int *)v) & 1, t = *(int *)u - *(int *)v;
	if (a && b) return t;
	if (a & 1) return -1;
	if (b & 1) return 1;
	return t;
}

int main()
{
	int i, j, k;
	
	N = in();
	if (N == 1) { puts("0"); return 0; }
	w = 0; while (w*w <= N) w++, a[w] = w*w;
	i = 0; while (i < LIM) s01[i++] = '0', s01[i++] = '1';
	
	for (j = 1; j <= N; j++) {
		int t, x = INF;
		for (i = 1; a[i] <= j; i++) {
			t = j - a[i];
			if (dp[t][0] + i  < x || (dp[t][0] + i == x) && ok(i, k, dp[t][1]))
				x = dp[t][0] + i, k = i;
		}
		t = j - a[k];
		dp[j][0] = dp[t][0] + k;
		dp[j][1] = (a[k] & 1)? dp[t][1]: !dp[t][1];
		pre[j][0] = t, pre[j][1] = k;
	}

	i = N; for (j = 0; i > 0; j++) {
		a[j] = pre[i][1];
		i = pre[i][0];
	}
//	printf("w=%d\n", j);
//	for (i = 0; i < j; i++) printf("%d ", a[i]); printf("\n");
	
	qsort(a, j, sizeof(int), cmp);
//	printf("w=%d\n", j);
//	for (i = 0; i < j; i++) printf("%d ", a[i]); printf("\n");
	
	for (i = 0; i < j && (a[i] & 1); i++) pr(a[i]);  // 奇数長を昇順
	while (i < j) { pr(a[--j]); if (i < j) pr(a[i++]); }  // 偶数長を長短長短順
	pc('\n');
	return 0;
}
0