結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
guricerin
|
| 提出日時 | 2020-02-04 12:43:32 |
| 言語 | F# (F# 4.0) |
| 結果 |
AC
|
| 実行時間 | 62 ms / 5,000 ms |
| コード長 | 1,387 bytes |
| コンパイル時間 | 13,141 ms |
| コンパイル使用メモリ | 193,512 KB |
| 実行使用メモリ | 31,360 KB |
| 最終ジャッジ日時 | 2024-07-01 09:38:50 |
| 合計ジャッジ時間 | 13,089 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.fsproj を復元しました (349 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
open System
open System.Collections.Generic
[<AutoOpen>]
module Cin =
let read f = stdin.ReadLine() |> f
let reada f = stdin.ReadLine().Split() |> Array.map f
let readChars() = read string |> Seq.toArray
let readInts() = readChars() |> Array.map (fun x -> Convert.ToInt32(x.ToString()))
[<AutoOpen>]
module Cout =
let writer = new IO.StreamWriter(new IO.BufferedStream(Console.OpenStandardOutput()))
let print (s: string) = writer.Write s
let println (s: string) = writer.WriteLine s
let inline puts (s: ^a) = string s |> println
let bitcount (a: int) =
a
|> fun x -> Convert.ToString(a, 2)
|> fun x -> x.Replace("0", "")
|> fun x -> x.Length
let main() =
let n = read int
let inf = Int32.MaxValue
let dist = Array.init (n + 1) (fun _ -> inf)
let len = dist |> Array.length
dist.[1] <- 1
let que = Queue<int>()
que.Enqueue(1)
let inner a = 1 <= a && a < len
while que.Count > 0 do
let i = que.Dequeue()
let b = bitcount i
let l = i - b
let r = i + b
let next = dist.[i] + 1
if inner l && dist.[l] > next then
dist.[l] <- next
que.Enqueue(l)
if inner r && dist.[r] > next then
dist.[r] <- next
que.Enqueue(r)
if dist.[n] = inf then -1 else dist.[n]
|> puts
()
main()
writer.Close()
guricerin