結果

問題 No.3 ビットすごろく
ユーザー TaK7907TaK7907
提出日時 2017-11-17 00:42:37
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 63 ms / 5,000 ms
コード長 1,617 bytes
コンパイル時間 2,157 ms
コンパイル使用メモリ 102,980 KB
実行使用メモリ 22,860 KB
最終ジャッジ日時 2023-09-14 00:50:14
合計ジャッジ時間 5,442 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,760 KB
testcase_01 AC 56 ms
20,692 KB
testcase_02 AC 57 ms
20,664 KB
testcase_03 AC 60 ms
22,788 KB
testcase_04 AC 59 ms
20,684 KB
testcase_05 AC 62 ms
22,740 KB
testcase_06 AC 58 ms
22,736 KB
testcase_07 AC 57 ms
22,744 KB
testcase_08 AC 58 ms
20,628 KB
testcase_09 AC 61 ms
22,736 KB
testcase_10 AC 63 ms
20,680 KB
testcase_11 AC 61 ms
20,744 KB
testcase_12 AC 61 ms
22,728 KB
testcase_13 AC 58 ms
22,804 KB
testcase_14 AC 61 ms
20,664 KB
testcase_15 AC 61 ms
22,716 KB
testcase_16 AC 60 ms
20,812 KB
testcase_17 AC 61 ms
20,760 KB
testcase_18 AC 60 ms
20,688 KB
testcase_19 AC 61 ms
22,808 KB
testcase_20 AC 57 ms
20,884 KB
testcase_21 AC 57 ms
22,816 KB
testcase_22 AC 61 ms
18,768 KB
testcase_23 AC 62 ms
20,616 KB
testcase_24 AC 62 ms
18,720 KB
testcase_25 AC 62 ms
22,804 KB
testcase_26 AC 59 ms
18,680 KB
testcase_27 AC 60 ms
22,704 KB
testcase_28 AC 62 ms
22,860 KB
testcase_29 AC 59 ms
22,800 KB
testcase_30 AC 57 ms
20,620 KB
testcase_31 AC 57 ms
20,748 KB
testcase_32 AC 59 ms
20,868 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;

namespace yuki0003 {
    internal static class Program {
        internal static int CountHighBits(int value) {
            return value == 0 ? 0 : ( value % 2 + CountHighBits( value >> 1 ) );
        }

        public static int Solve(System.IO.TextReader reader) {
            var n = int.Parse( reader.ReadLine() );
            var cost = new int[ n ];
            var stack = new Stack<int>();
            var answer = -1;
            const int FIRST = 1;
            stack.Push( FIRST );
            cost[ FIRST - 1 ] = 1;
            int cur, backward, forward;
            while (stack.Count > 0) {
                cur = stack.Pop();
                if (cur == n) {
                    if (answer == -1 || answer > cost[ cur - 1 ])
                        answer = cost[ cur - 1 ];
                }
                backward = cur - CountHighBits( cur );
                if (backward >= 1 && ( cost[ backward - 1 ] == 0 || cost[ backward - 1 ] > cost[ cur - 1 ] + 1 )) {
                    stack.Push( backward );
                    cost[ backward - 1 ] = cost[ cur - 1 ] + 1;
                }
                forward = cur + CountHighBits( cur );
                if (forward <= n && ( cost[ forward - 1 ] == 0 || cost[ forward - 1 ] > cost[ cur - 1 ] + 1 )) {
                    stack.Push( forward );
                    cost[ forward - 1 ] = cost[ cur - 1 ] + 1;
                }
            }
            return answer;
        }

        static void Main(string[] args) {
            Console.WriteLine( Solve( System.Console.In ) );
        }
    }
}
0