結果
問題 | No.3 ビットすごろく |
ユーザー | tetsu |
提出日時 | 2018-03-04 15:00:22 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 98 ms / 5,000 ms |
コード長 | 2,836 bytes |
コンパイル時間 | 4,074 ms |
コンパイル使用メモリ | 81,392 KB |
実行使用メモリ | 40,212 KB |
最終ジャッジ日時 | 2024-07-01 08:56:32 |
合計ジャッジ時間 | 7,576 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
37,088 KB |
testcase_01 | AC | 56 ms
37,160 KB |
testcase_02 | AC | 56 ms
37,192 KB |
testcase_03 | AC | 65 ms
37,572 KB |
testcase_04 | AC | 56 ms
37,444 KB |
testcase_05 | AC | 79 ms
38,800 KB |
testcase_06 | AC | 66 ms
37,704 KB |
testcase_07 | AC | 58 ms
37,228 KB |
testcase_08 | AC | 73 ms
38,244 KB |
testcase_09 | AC | 89 ms
39,516 KB |
testcase_10 | AC | 83 ms
39,596 KB |
testcase_11 | AC | 89 ms
39,412 KB |
testcase_12 | AC | 84 ms
38,384 KB |
testcase_13 | AC | 62 ms
37,504 KB |
testcase_14 | AC | 93 ms
39,524 KB |
testcase_15 | AC | 96 ms
40,136 KB |
testcase_16 | AC | 84 ms
39,900 KB |
testcase_17 | AC | 97 ms
40,212 KB |
testcase_18 | AC | 60 ms
37,516 KB |
testcase_19 | AC | 97 ms
39,968 KB |
testcase_20 | AC | 56 ms
37,284 KB |
testcase_21 | AC | 54 ms
36,980 KB |
testcase_22 | AC | 92 ms
39,528 KB |
testcase_23 | AC | 90 ms
40,016 KB |
testcase_24 | AC | 98 ms
40,016 KB |
testcase_25 | AC | 97 ms
40,132 KB |
testcase_26 | AC | 53 ms
36,768 KB |
testcase_27 | AC | 63 ms
37,788 KB |
testcase_28 | AC | 84 ms
39,748 KB |
testcase_29 | AC | 90 ms
39,412 KB |
testcase_30 | AC | 53 ms
37,112 KB |
testcase_31 | AC | 53 ms
36,952 KB |
testcase_32 | AC | 89 ms
39,424 KB |
ソースコード
package yukicoder;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Arrays;import java.util.Comparator;import java.util.PriorityQueue;import java.util.StringTokenizer;public class P3 {static long[] dist;static long INF = 100000000000000000L;static ArrayList<ArrayList<Edge>> adj;static class Edge {int to;long w;Edge(int _to, long _w) {this.to = _to;this.w = _w;}@Overridepublic String toString() {return "(to=" + to + ", w=" + w + ")";}}static class NextNode {int id;long cost;NextNode(int _id, long _cost) {id = _id; cost = _cost;}}static void initG(int V) { // V: the number of vertexesfor(int i=0; i<V; i++) {adj.add(new ArrayList<Edge>());}}static void dijkstra(int s) {Arrays.fill(dist, INF);dist[s] = 0;PriorityQueue<NextNode> pq = new PriorityQueue<NextNode>(new Comparator<NextNode>() {public int compare(NextNode n1, NextNode n2) {if(n1.cost > n2.cost) return 1;else if(n1.cost==n2.cost) return 0;else return -1;}});pq.add(new NextNode(s, 0));while(!pq.isEmpty()) {NextNode cn = pq.poll();if(cn.cost > dist[cn.id]) continue;for(Edge e : adj.get(cn.id)) {if(cn.cost + e.w < dist[e.to]) {dist[e.to] = cn.cost + e.w;pq.add(new NextNode(e.to, dist[e.to]));}}}}public static void main(String[] args) throws IOException {MyScanner sc = new MyScanner(System.in);int n = sc.nextInt();dist = new long[n+1];adj = new ArrayList<ArrayList<Edge>>(n+1);initG(n+1);for(int i=1; i<=n; i++) {int d = digit(i);if(0<=i+d && i+d<=n) adj.get(i).add(new Edge(i+d, 1));if(0<=i-d && i-d<=n) adj.get(i).add(new Edge(i-d, 1));}dijkstra(1);if(dist[n]>=INF) {System.out.println(-1);} else {System.out.println(dist[n]+1);}}public static int digit(int n) {int ret = 0;while(n>0) {if(n%2==1) ret++;n/=2;}return ret;}static class MyScanner{BufferedReader br;StringTokenizer st;public MyScanner(InputStream s){br=new BufferedReader(new InputStreamReader(s));}public String nextLine() throws IOException{return br.readLine();}public String next() throws IOException{while(st==null || !st.hasMoreTokens())st=new StringTokenizer(br.readLine());return st.nextToken();}public int nextInt() throws IOException{return Integer.parseInt(next());}public double nextDouble() throws IOException{return Double.parseDouble(next());}public boolean ready() throws IOException{return br.ready();}public long nextLong() throws IOException{return Long.parseLong(next());}}}