結果

問題 No.3 ビットすごろく
ユーザー TaK7907TaK7907
提出日時 2017-11-16 23:55:08
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,573 bytes
コンパイル時間 3,757 ms
コンパイル使用メモリ 101,616 KB
実行使用メモリ 22,820 KB
最終ジャッジ日時 2023-08-16 17:23:45
合計ジャッジ時間 6,499 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
20,632 KB
testcase_01 AC 47 ms
22,772 KB
testcase_02 AC 48 ms
20,736 KB
testcase_03 AC 51 ms
20,652 KB
testcase_04 AC 51 ms
20,632 KB
testcase_05 AC 55 ms
22,648 KB
testcase_06 AC 47 ms
20,712 KB
testcase_07 AC 48 ms
22,740 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 50 ms
20,644 KB
testcase_13 AC 49 ms
18,588 KB
testcase_14 AC 51 ms
22,708 KB
testcase_15 AC 48 ms
20,648 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 47 ms
18,676 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 46 ms
20,740 KB
testcase_22 AC 49 ms
20,716 KB
testcase_23 AC 54 ms
20,672 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 49 ms
22,716 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 49 ms
22,712 KB
testcase_32 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;


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) {
                    answer = cost[ cur - 1 ];
                    break;
                }
                backward = cur - CountHighBits( cur );
                if (backward >= 1 && cost[backward-1]==0) {
                    stack.Push( backward );
                    cost[ backward - 1 ] = cost[ cur - 1 ] + 1;
                }
                forward = cur + CountHighBits( cur );
                if (forward <= n && cost[ forward - 1 ] == 0) {
                    stack.Push( forward );
                    cost[ forward - 1 ] = cost[ cur - 1 ] + 1;
                }
            }
            return answer;
        }

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