結果

問題 No.873 バイナリ、ヤバいなり!w
コンテスト
ユーザー easy new
提出日時 2026-10-08 00:27:25
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 108 ms / 2,000 ms
+ 992µs
コード長 1,886 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,539 ms
コンパイル使用メモリ 354,584 KB
実行使用メモリ 9,876 KB
最終ジャッジ日時 2026-10-08 00:27:32
合計ジャッジ時間 6,274 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    // dp[score] = minimum total characters needed for this score.
    const int INF = 1e9;
    vector<int> dp(n + 1, INF);
    dp[0] = 0;

    for (int score = 1; score <= n; score++) {
        for (int len = 1; len * len <= score; len++) {
            dp[score] = min(
                dp[score],
                len + dp[score - len * len]
            );
        }
    }

    string answer;
    int remaining = n;
    int start = 0;

    while (remaining > 0) {
        int shortestZero = -1;
        int longest = -1;

        // Try lengths in increasing order.
        for (int len = 1; len * len <= remaining; len++) {

            // Only consider choices preserving minimum total length.
            if (len + dp[remaining - len * len] != dp[remaining]) {
                continue;
            }

            longest = len;

            int last;
            if (len % 2 == 1) {
                last = start;      // Odd: same ending bit.
            } else {
                last = 1 - start;  // Even: opposite ending bit.
            }

            // First zero-ending candidate is the shortest one.
            if (last == 0 && shortestZero == -1) {
                shortestZero = len;
            }
        }

        int chosen;
        if (shortestZero != -1) {
            chosen = shortestZero;
        } else {
            chosen = longest;
        }

        // Append the chosen alternating block.
        int bit = start;
        for (int i = 0; i < chosen; i++) {
            answer.push_back('0' + bit);
            bit = 1 - bit;
        }

        remaining -= chosen * chosen;

        // Repeat the last bit to create the next block boundary.
        start = answer.back() - '0';
    }

    cout << answer << '\n';
}
0