結果

問題 No.642 Two Operations No.1
ユーザー Grenache
提出日時 2018-02-02 21:41:31
言語 Java
(openjdk 23)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 964 bytes
コンパイル時間 4,592 ms
コンパイル使用メモリ 79,532 KB
実行使用メモリ 56,340 KB
最終ジャッジ日時 2024-12-31 07:19:32
合計ジャッジ時間 8,151 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder642_1 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();

		Map<Long, Integer> dp = new HashMap<>();
		dp.put((long)n, 0);
		Queue<Long> q = new ArrayDeque<>();
		q.add((long)n);
		while (!q.isEmpty()) {
			long e = q.remove();

			if (e == 1) {
				pr.println(dp.get(e));
				return;
			}

			if (e > 1) {
				if (e % 2 == 0 && !dp.containsKey(e / 2)) {
					q.add(e / 2);
					dp.put(e / 2, dp.get(e) + 1);
				}
				if (!dp.containsKey(e + 1)) {
					q.add(e + 1);
					dp.put(e + 1, dp.get(e) + 1);
				}
			}
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0