結果
| 問題 | No.873 バイナリ、ヤバいなり!w | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-08-30 22:22:00 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 207 ms / 2,000 ms | 
| コード長 | 1,921 bytes | 
| コンパイル時間 | 2,313 ms | 
| コンパイル使用メモリ | 182,732 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-06-11 13:59:37 | 
| 合計ジャッジ時間 | 4,566 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 36 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
string calc(int num, int bg) {
    string res = "";
    for (int i = 0; i < num; i++) {
        res += (char)('0' + bg);
        bg = 1 - bg;
    }
    return res;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    
    vector<int> dp(n + 1);
    //vector<int> dp2(n + 1, 1);
    for (int i = 0; i <= n; i++) dp[i] = i;
    //dp2[0] = 0;
    
    for (int i = 2; i * i <= n; i++) {
        for (int j = i * i; j <= n; j++) {
            int cost = dp[j - i * i] + i;
            if (dp[j] > cost) {
                dp[j] = cost;
                //dp2[j] = i;
            }
        }
    }
    
    vector<int> odd;
    deque<int> even;
    int cur = n;
    while (cur > 0) {
        bool f = false;
        for (int i = 1; i * i <= cur; i += 2) {
            if (dp[cur] == dp[cur - i * i] + i) {
                odd.push_back(i);
                cur -= i * i;
                f = true;
                break;
            }
        }
        if (f) continue;
        else break;
    }
    int hoge = 0;
    while (cur > 0) {
        int nex = 0;
        for (int i = 2; i * i <= cur; i += 2) {
            if (dp[cur] == dp[cur - i * i] + i) {
                nex = i;
                if (hoge == 1) break;
            }
        }
        assert(nex > 0);
        even.push_back(nex);
        cur -= nex * nex;
    }
    sort(odd.begin(), odd.end());
    sort(even.begin(), even.end());
    string ans = "";
    for (int x : odd) {
        ans += calc(x, 0);
    }
    int bg = 0;
    while (!even.empty()) {
        if (bg == 0) {
            ans += calc(even.back(), bg);
            even.pop_back();
            bg = 1;
        } else {
            ans += calc(even.front(), bg);
            even.pop_front();            
            bg = 0;
        }
    }
    cout << ans << endl;
    return 0;
}
            
            
            
        