結果

問題 No.642 Two Operations No.1
ユーザー GrenacheGrenache
提出日時 2018-02-02 21:41:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 964 bytes
コンパイル時間 3,803 ms
コンパイル使用メモリ 76,600 KB
実行使用メモリ 56,232 KB
最終ジャッジ日時 2023-08-30 02:05:00
合計ジャッジ時間 6,721 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,512 KB
testcase_01 AC 128 ms
55,712 KB
testcase_02 AC 128 ms
55,516 KB
testcase_03 AC 129 ms
55,564 KB
testcase_04 AC 131 ms
55,524 KB
testcase_05 AC 134 ms
53,532 KB
testcase_06 AC 132 ms
55,840 KB
testcase_07 AC 133 ms
56,232 KB
testcase_08 AC 134 ms
56,004 KB
testcase_09 AC 131 ms
55,968 KB
testcase_10 AC 133 ms
55,600 KB
testcase_11 AC 132 ms
55,736 KB
testcase_12 AC 138 ms
55,776 KB
testcase_13 AC 134 ms
55,524 KB
testcase_14 AC 135 ms
55,416 KB
権限があれば一括ダウンロードができます

ソースコード

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