結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  WagiHOH | 
| 提出日時 | 2016-07-08 10:58:37 | 
| 言語 | D (dmd 2.109.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 55 ms / 5,000 ms | 
| コード長 | 1,157 bytes | 
| コンパイル時間 | 780 ms | 
| コンパイル使用メモリ | 98,200 KB | 
| 実行使用メモリ | 7,324 KB | 
| 最終ジャッジ日時 | 2024-06-12 03:37:23 | 
| 合計ジャッジ時間 | 2,126 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import core.bitop;
struct Block
{
    uint bits;
    bool forward, backward;
    uint optimum = uint.max;
}
void main()
{
    immutable n = readln.chomp.to!uint;
    auto game = new Block[n + 1];
    foreach (i, ref x; game) x.bits = popcnt(cast(uint)i);
    uint[] route = [1];
    do {
        auto point = route.back;
        if (point < 1 || point > n) {
            route.popBack;
            continue;
        }
        with (game[point]) {
            if (optimum > route.length) {
                optimum = cast(uint)route.length;
                forward = backward = false;
            }
            if (optimum < route.length) {
                route.popBack;
                continue;
            }
            if (!forward) {
                route ~= point + bits;
                forward = true;
            } else if (!backward) {
                route ~= point - bits;
                backward = true;
            } else {
                route.popBack;
            }
        }
    } while (!route.empty);
    writeln(cast(int)game[n].optimum);
}
            
            
            
        