結果
| 問題 |
No.873 バイナリ、ヤバいなり!w
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-08-31 11:17:16 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,046 bytes |
| コンパイル時間 | 797 ms |
| コンパイル使用メモリ | 31,872 KB |
| 実行使用メモリ | 8,664 KB |
| 最終ジャッジ日時 | 2024-06-24 06:35:07 |
| 合計ジャッジ時間 | 3,613 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 WA * 2 |
コンパイルメッセージ
main.c: In function 'in':
main.c:8:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
8 | #define gc() getchar_unlocked()
| ^~~~~~~~~~~~~~~~
main.c:16:24: note: in expansion of macro 'gc'
16 | int n = 0, c = gc();
| ^~
main.c: In function 'outs':
main.c:9:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
9 | #define pc(c) putchar_unlocked(c)
| ^~~~~~~~~~~~~~~~
main.c:21:33: note: in expansion of macro 'pc'
21 | void outs(char *s) { while (*s) pc(*s++); }
| ^~
ソースコード
// 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;
}
bal4u