結果

問題 No.3 ビットすごろく
ユーザー ぴろずぴろず
提出日時 2014-12-21 01:02:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 144 ms / 5,000 ms
コード長 1,583 bytes
コンパイル時間 4,374 ms
コンパイル使用メモリ 76,176 KB
実行使用メモリ 59,648 KB
最終ジャッジ日時 2023-09-13 22:58:12
合計ジャッジ時間 9,544 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,052 KB
testcase_01 AC 124 ms
55,632 KB
testcase_02 AC 122 ms
56,012 KB
testcase_03 AC 128 ms
55,412 KB
testcase_04 AC 128 ms
55,612 KB
testcase_05 AC 136 ms
55,400 KB
testcase_06 AC 128 ms
55,916 KB
testcase_07 AC 126 ms
55,612 KB
testcase_08 AC 137 ms
56,008 KB
testcase_09 AC 136 ms
57,888 KB
testcase_10 AC 139 ms
57,620 KB
testcase_11 AC 133 ms
58,372 KB
testcase_12 AC 133 ms
55,740 KB
testcase_13 AC 121 ms
55,952 KB
testcase_14 AC 137 ms
57,768 KB
testcase_15 AC 137 ms
57,668 KB
testcase_16 AC 142 ms
59,648 KB
testcase_17 AC 140 ms
57,708 KB
testcase_18 AC 125 ms
55,980 KB
testcase_19 AC 138 ms
58,088 KB
testcase_20 AC 125 ms
56,268 KB
testcase_21 AC 127 ms
55,600 KB
testcase_22 AC 142 ms
57,772 KB
testcase_23 AC 137 ms
55,860 KB
testcase_24 AC 140 ms
58,352 KB
testcase_25 AC 144 ms
58,132 KB
testcase_26 AC 124 ms
55,684 KB
testcase_27 AC 128 ms
55,704 KB
testcase_28 AC 140 ms
58,064 KB
testcase_29 AC 137 ms
57,828 KB
testcase_30 AC 121 ms
55,652 KB
testcase_31 AC 123 ms
55,936 KB
testcase_32 AC 143 ms
55,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no003;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Graph g = new Graph(n);
		for(int i=0;i<n;i++) {
			int bc = Integer.bitCount(i+1);
			if (i - bc >= 0) {
				g.addEdge(i, i-bc, 1);
			}
			if (i + bc < n) {
				g.addEdge(i, i+bc, 1);
			}
		}
		int dist = g.minDistQueue(0)[n-1];
		System.out.println(dist == Graph.INF ? -1 : dist + 1);
	}
}
class Graph {
	public static final int INF = 1<<29;
	int n;
	ArrayList<Edge>[] graph;

	@SuppressWarnings("unchecked")
	public Graph(int n) {
		this.n = n;
		this.graph = new ArrayList[n];
		for(int i=0;i<n;i++) {
			graph[i] = new ArrayList<Edge>();
		}
	}

	public void addBidirectionalEdge(int from,int to,int cost) {
		addEdge(from,to,cost);
		addEdge(to,from,cost);
	}
	public void addEdge(int from,int to,int cost) {
		graph[from].add(new Edge(to, cost));
	}

	//O(E) all cost is 0 or 1
	public int[] minDistQueue(int s) {
		int[] d = new int[n];
		Arrays.fill(d, INF);
		ArrayDeque<Integer> q = new ArrayDeque<Integer>();
		q.add(s);
		d[s] = 0;
		while(!q.isEmpty()) {
			int v = q.pollFirst();
			for(Edge e:graph[v]) {
				int u = e.to;
				if (d[v] + e.cost < d[u]) {
					d[u] = d[v] + e.cost;
					if (e.cost == 0) {
						q.addFirst(u);
					}else{
						q.addLast(u);
					}
				}
			}
		}
		return d;
	}

	class Edge {
		int to;
		int cost;
		public Edge(int to,int cost) {
			this.to = to;
			this.cost = cost;
		}
	}
}
0