結果
問題 | No.3 ビットすごろく |
ユーザー | Cavasiro |
提出日時 | 2020-05-02 18:58:07 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 70 ms / 5,000 ms |
コード長 | 3,829 bytes |
コンパイル時間 | 2,199 ms |
コンパイル使用メモリ | 78,240 KB |
実行使用メモリ | 52,912 KB |
最終ジャッジ日時 | 2024-07-01 09:47:27 |
合計ジャッジ時間 | 5,308 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
50,152 KB |
testcase_01 | AC | 57 ms
50,356 KB |
testcase_02 | AC | 55 ms
50,452 KB |
testcase_03 | AC | 60 ms
52,816 KB |
testcase_04 | AC | 57 ms
50,748 KB |
testcase_05 | AC | 65 ms
50,828 KB |
testcase_06 | AC | 59 ms
50,648 KB |
testcase_07 | AC | 57 ms
52,436 KB |
testcase_08 | AC | 68 ms
50,996 KB |
testcase_09 | AC | 68 ms
50,912 KB |
testcase_10 | AC | 67 ms
50,980 KB |
testcase_11 | AC | 66 ms
50,968 KB |
testcase_12 | AC | 67 ms
50,496 KB |
testcase_13 | AC | 59 ms
50,876 KB |
testcase_14 | AC | 69 ms
50,924 KB |
testcase_15 | AC | 68 ms
50,752 KB |
testcase_16 | AC | 68 ms
51,084 KB |
testcase_17 | AC | 68 ms
51,004 KB |
testcase_18 | AC | 57 ms
50,544 KB |
testcase_19 | AC | 68 ms
52,824 KB |
testcase_20 | AC | 58 ms
52,496 KB |
testcase_21 | AC | 55 ms
49,996 KB |
testcase_22 | AC | 67 ms
52,912 KB |
testcase_23 | AC | 69 ms
52,908 KB |
testcase_24 | AC | 69 ms
50,852 KB |
testcase_25 | AC | 70 ms
51,048 KB |
testcase_26 | AC | 57 ms
52,136 KB |
testcase_27 | AC | 60 ms
50,584 KB |
testcase_28 | AC | 69 ms
50,860 KB |
testcase_29 | AC | 67 ms
51,028 KB |
testcase_30 | AC | 56 ms
49,920 KB |
testcase_31 | AC | 58 ms
50,364 KB |
testcase_32 | AC | 67 ms
50,836 KB |
ソースコード
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.NoSuchElementException; import java.util.Queue; class Main implements Runnable { private final FastScanner sc; private final PrintWriter out; //private final long MOD = 1000000007; //private final int[] d8x = {-1,0,1,1,1,0,-1,-1}; //private final int[] d8y = {-1,-1,-1,0,1,1,1,0}; //private final int[] d4x = {0,1,0,-1}; //private final int[] d4y = {-1,0,1,0}; int n; public static void main(String[] args) { new Thread(null, new Main(), "", 16L * 1024 * 1024).start(); } Main() { sc = new FastScanner(); out = new PrintWriter(System.out); } private void init() throws Exception { n = sc.nextInt(); } private void solve() throws Exception{ boolean[] front = new boolean[n]; boolean[] back = new boolean[n]; int min = Integer.MAX_VALUE; Queue<Data> q = new ArrayDeque<Data>(); q.offer(new Data(1,1)); while(q.size()>0){ Data d = q.poll(); if(d.getAdress() == n) { min = Math.min(min, d.getPath()); continue; } if(!back[d.getAdress()-1]){ if(d.getAdress()-Integer.bitCount(d.getAdress())>=1){ q.offer(new Data(d.getAdress()-Integer.bitCount(d.getAdress()),d.getPath()+1)); } back[d.getAdress()-1] = true; } if(!front[d.getAdress()-1]){ if(d.getAdress()+Integer.bitCount(d.getAdress())<=n){ q.offer(new Data(d.getAdress()+Integer.bitCount(d.getAdress()),d.getPath()+1)); } front[d.getAdress()-1] = true; } } out.println(min==Integer.MAX_VALUE?-1:min); } public void run() { try { init(); sc.close(); solve(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } out.flush(); out.close(); } } class Data { private int adress; private int path; public Data(int adress,int path){ this.adress = adress; this.path = path; } public int getAdress() { return adress; } public int getPath() { return path; } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean read() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } return true; } } private boolean hasNext() { while (read() && !isVisibleChar(buffer[ptr])) ptr++; return read(); } private int getNextChar() { if (read()) { ptr++; return buffer[ptr - 1]; } else { return -1; } } private static boolean isVisibleChar(int code) { return code >= 33 && code <= 126; } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = getNextChar(); while (isVisibleChar(b)) { sb.appendCodePoint(b); b = getNextChar(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean sign = true; int b = getNextChar(); if (b == '-') { sign = false; b = getNextChar(); } if (b < '0' || b > '9') { throw new NumberFormatException(); } while (true) { if (b >= '0' && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isVisibleChar(b)) { return sign ? n : -n; } else { throw new NumberFormatException(); } b = getNextChar(); } } public int nextInt() { long n = nextLong(); if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) n; } public double nextDouble() { return Double.parseDouble(next()); } public void close() { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }