結果

問題 No.3 ビットすごろく
ユーザー tetora1011tetora1011
提出日時 2019-11-16 19:33:14
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 58 ms / 5,000 ms
コード長 7,681 bytes
コンパイル時間 4,075 ms
コンパイル使用メモリ 111,044 KB
実行使用メモリ 23,932 KB
最終ジャッジ日時 2023-09-14 01:30:20
合計ジャッジ時間 6,976 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
21,700 KB
testcase_01 AC 56 ms
23,752 KB
testcase_02 AC 55 ms
21,876 KB
testcase_03 AC 55 ms
21,772 KB
testcase_04 AC 57 ms
21,908 KB
testcase_05 AC 55 ms
23,756 KB
testcase_06 AC 53 ms
21,776 KB
testcase_07 AC 54 ms
21,816 KB
testcase_08 AC 54 ms
23,820 KB
testcase_09 AC 53 ms
23,816 KB
testcase_10 AC 54 ms
21,844 KB
testcase_11 AC 54 ms
21,876 KB
testcase_12 AC 56 ms
21,880 KB
testcase_13 AC 55 ms
23,932 KB
testcase_14 AC 56 ms
23,772 KB
testcase_15 AC 58 ms
23,804 KB
testcase_16 AC 55 ms
21,740 KB
testcase_17 AC 54 ms
23,688 KB
testcase_18 AC 57 ms
21,876 KB
testcase_19 AC 57 ms
21,748 KB
testcase_20 AC 53 ms
19,732 KB
testcase_21 AC 53 ms
21,632 KB
testcase_22 AC 54 ms
21,716 KB
testcase_23 AC 55 ms
23,696 KB
testcase_24 AC 54 ms
23,780 KB
testcase_25 AC 53 ms
21,840 KB
testcase_26 AC 55 ms
21,644 KB
testcase_27 AC 57 ms
21,720 KB
testcase_28 AC 57 ms
19,720 KB
testcase_29 AC 57 ms
21,768 KB
testcase_30 AC 54 ms
21,712 KB
testcase_31 AC 53 ms
21,892 KB
testcase_32 AC 53 ms
21,692 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace No003
{
    class Program
    {
        public static void Solve(Input input)
        {
            var n = input.NextInt();
            var dp = Enumerable.Repeat(int.MaxValue, n + 10).ToArray();

            dp[0] = 0;
            dp[1] = 1;

            var q = new Queue<int>();
            q.Enqueue(1);
            while (q.Any())
            {
                var r = q.Dequeue();
                var b = BitCount(r);

                if (r - b >= 0)
                {
                    if (dp[r - b] > dp[r] + 1)
                    {
                        dp[r - b] = dp[r] + 1;
                        q.Enqueue(r - b);
                    }
                }
                if (r + b <= n)
                {
                    if (dp[r + b] > dp[r] + 1)
                    {
                        dp[r + b] = dp[r] + 1;
                        q.Enqueue(r + b);
                    }
                }
            }

            if (dp[n] == int.MaxValue)
            {
                Console.WriteLine(-1);
            }
            else
            {
                Console.WriteLine(dp[n]);
            }
        }

        static int BitCount(int n)
        {
            int count = 0;
            while (n != 0)
            {
                if ((n & 1) != 0)
                {
                    count++;
                }

                n >>= 1;
            }

            return count;
        }



        #region Main
        public static void Main(string[] args)
        {
            // 出力が少ないときはこれをセットする方が時間かかるけれど
            // そのときにTLEするのはアルゴリズムが悪いせいだし、まあ良しとする
            var needsFlushOutput = true;
            if (needsFlushOutput)
            {
                // 細かく出力しないようにする
                var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }

            // 通常は引数無しで、ファイルから読み出したい場合はpath指定する
            var input = new Input();

            // 仮想的に標準入力をセットする
            // テスト入力したまま提出することがままあるので……
#if DEBUG
            input.SetText("");
#endif

            Solve(input);

            Console.Out.Flush();
        }
        #endregion

        #region Competitive Template
#pragma warning disable CS0414
        static readonly int MOD = (int)1e9 + 7;
        static readonly int[] dx = { 1, 0, -1, 0 };
        static readonly int[] dy = { 0, 1, 0, -1 };
        /// <summary> 左上が原点 </summary>
        static readonly char[] dir = { 'R', 'U', 'L', 'D' };
#pragma warning restore CS0414

        public class Input
        {
            // 変な改行コードがたまに混じっているので、一応セパレート指定する
            // スペース単独指定の方がもちろん早い
            static readonly char[] separator = { ' ', '\r', '\n' };
            readonly StreamReader sr;
            readonly Queue<string> queue;

            /// <summary>
            /// 特定のファイルから読み出したい場合はpath指定する
            /// </summary>
            public Input(string path = "")
            {
                queue = new Queue<string>();

                if (string.IsNullOrEmpty(path))
                { sr = new StreamReader(Console.OpenStandardInput()); }
                else
                { sr = new StreamReader(path); }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public void SetText(IEnumerable<string> items)
            {
                foreach (var item in items)
                    SetText(item);
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public bool SetText(string s)
            {
                if (string.IsNullOrEmpty(s))
                    return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                    queue.Enqueue(elem);

                return true;
            }

            /// <summary>
            /// 要素が存在するか
            /// </summary>
            public bool Any() => queue.Any() || Read();

            /// <summary>
            /// 内部queueに入力からの値をsplitして格納する
            /// </summary>
            bool Read()
            {
                if (!SetText(sr.ReadLine()))
                    return false;

                if (!queue.Any())
                    return Read();

                return queue.Any();
            }

            /// <summary>
            /// 次のstringを一つ読み込む
            /// </summary>
            public string Next()
            {
                if (!queue.Any() && !Read())
                    return "";

                return queue.Dequeue();
            }

            /// <summary>
            /// 指定個数queueにたまるまでenqueueし続ける
            /// </summary>
            bool Accumulate(int n)
            {
                while (queue.Count() < n)
                    if (!Read())
                        return false;

                return true;
            }

            public int NextInt() => int.Parse(Next());
            public long NextLong() => long.Parse(Next());
            public double NextDouble() => double.Parse(Next());

            /// <summary>
            /// n個の要素をparseして、それぞれにoffsetをaddした配列を返す
            /// </summary>
            T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add)
            {
                if (!Accumulate(n))
                    return null;

                var a = new T[n];

                for (int i = 0; i < n; i++)
                    a[i] = add(parse(queue.Dequeue()), offset);

                return a;
            }

            public string[] Next(int n) => NextT(n, "", x => x, (x, y) => x);
            public int[] NextInt(int n, int offset = 0) => NextT(n, offset, int.Parse, (x, y) => x + y);
            public long[] NextLong(int n, long offset = 0) => NextT(n, offset, long.Parse, (x, y) => x + y);
            public double[] NextDouble(int n, double offset = 0.0) => NextT(n, offset, double.Parse, (x, y) => x + y);
        }

        static class Utils
        {
            public static T Max<T>(params T[] objs) => objs.Max();
            public static T Min<T>(params T[] objs) => objs.Min();

            /// <summary>
            /// vでfillされたT[d1][d2]配列を作成する
            /// </summary>
            public static T[][] Create2DArray<T>(int d1, int d2, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(v, d2).ToArray()).ToArray();
            }

            /// <summary>
            /// vでfillされたT[d1][d2][d3]配列を作成する
            /// </summary>
            public static T[][][] Create3DArray<T>(int d1, int d2, int d3, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(0, d2).Select(__ =>
                    Enumerable.Repeat(v, d3).ToArray()).ToArray()).ToArray();
            }
        }
        #endregion
    }
}
0