๋ฌธ์ : https://www.acmicpc.net/problem/12851
12851๋ฒ: ์จ๋ฐ๊ผญ์ง 2
์๋น์ด๋ ๋์๊ณผ ์จ๋ฐ๊ผญ์ง์ ํ๊ณ ์๋ค. ์๋น์ด๋ ํ์ฌ ์ N(0 ≤ N ≤ 100,000)์ ์๊ณ , ๋์์ ์ K(0 ≤ K ≤ 100,000)์ ์๋ค. ์๋น์ด๋ ๊ฑท๊ฑฐ๋ ์๊ฐ์ด๋์ ํ ์ ์๋ค. ๋ง์ฝ, ์๋น์ด์ ์์น๊ฐ X์ผ ๋
www.acmicpc.net
๊ธฐ์กด์ ์จ๋ฐ๊ผญ์ง ๋ฌธ์ ์์ ์ต๋จ๊ฒฝ๋ก์ ๊ฐฏ์๊น์ง ๊ตฌํด์ผ ํ๋ค.
1. ๋ชฉ์ ์ง์ ๋๋ฌํ๋ฉด ํจ์๋ฅผ ์ข ๋ฃํ๋ ๊ฒ์ด ์๋ ๊ณ์ ํ์์ ์ด์ด๋๊ฐ๋๋ก ํจ์๋ฅผ ๊ตฌ์ฑํ๋ค.
while(!pq.isEmpty()){
Node cur = pq.poll();
if(cur.idx==K && cur.w==dist[K]){ // ๋ชฉํ์ง์ ์ ๋๋ฌํ ๊ฒฝ์ฐ
count+=1;
}
...
}
2. ๋ํ ๋ชจ๋ ๊ฐ๋ฅํ ๊ฒฝ๋ก๋ฅผ ํ์ํ๊ธฐ ์ํด ๊ธฐ์กด ๋ค์ต์คํธ๋ผ ์ฝ๋๋ฅผ ์์ ํ๋ค.
while(!pq.isEmpty()){
Node cur = pq.poll();
...
// ๊ธฐ์กด ๋ค์ต์คํธ๋ผ๋ < ์ด์ง๋ง, ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๋ณด๊ธฐ ์ํด์ <=
if(cur.idx-1 >= 0 && cur.w + 1 <= dist[cur.idx-1]){
dist[cur.idx-1] = cur.w + 1;
pq.offer(new Node(cur.idx-1,cur.w+1));
}
if(cur.idx+1 <= 100000 && cur.w + 1 <= dist[cur.idx+1]){
dist[cur.idx+1] = cur.w + 1;
pq.offer(new Node(cur.idx+1,cur.w+1));
}
if(cur.idx*2 <= 100000 && cur.w + 1 <= dist[cur.idx*2]){
dist[cur.idx*2] = cur.w + 1;
pq.offer(new Node(cur.idx*2,cur.w+1));
}
}
๊ธฐ์กด ๋ค์ต์คํธ๋ผ ์ฝ๋์์ cur.w + 1 < dist[cur.idx - 1] ๊ณผ ๊ฐ์ด < ๋ถ๋ฑํธ๋ฅผ ์ฌ์ฉํ๋ค.
ํ์ง๋ง ๋ชจ๋ ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ฅผ ํ์ํด์ผ ํ๊ธฐ ๋๋ฌธ์ <= ๋ถ๋ฑํธ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
3. ๋ถํ์ํ ํ์์ ์ค์งํ์ฌ ์๊ฐ์ ์ค์ผ ์ ์์๋ค.
while(!pq.isEmpty()){
Node cur = pq.poll();
...
if(cur.w > dist[K]){ // ๋ชฉ์ ์ง ๋ณด๋ค ๋จผ ๊ฒฝ์ฐ๋ฅผ ํ์ํ๊ธฐ ์์ํ๋ฉด ํจ์๋ฅผ ์ข
๋ฃํด๋ ๋จ.
return;
}
...
}
์ ์ฒด์ฝ๋
package com.mincheolsong;
import java.io.*;
import java.util.*;
public class Main {
// BOJ 12851
// ๋ค์ต์คํธ๋ผ
static final int INF = (int)1e9;
static int N,K;
static int[] dist;
static PriorityQueue<Node> pq;
static int count;
static void solve(){
while(!pq.isEmpty()){
Node cur = pq.poll();
if(cur.idx==K && cur.w==dist[K]){ // ๋ชฉํ์ง์ ์ ๋๋ฌํ ๊ฒฝ์ฐ
count+=1;
}
if(dist[cur.idx] < cur.w) continue; // ๋ฐฉ๋ฌธ์ฒดํฌ
if(cur.w > dist[K]){ // ๋ชฉ์ ์ง ๋ณด๋ค ๋จผ ๊ฒฝ์ฐ๋ฅผ ํ์ํ๊ธฐ ์์ํ๋ฉด ํจ์๋ฅผ ์ข
๋ฃํด๋ ๋จ.
return;
}
// ๊ธฐ์กด ๋ค์ต์คํธ๋ผ๋ < ์ด์ง๋ง, ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๋ณด๊ธฐ ์ํด์ <=
if(cur.idx-1 >= 0 && cur.w + 1 <= dist[cur.idx-1]){
dist[cur.idx-1] = cur.w + 1;
pq.offer(new Node(cur.idx-1,cur.w+1));
}
if(cur.idx+1 <= 100000 && cur.w + 1 <= dist[cur.idx+1]){
dist[cur.idx+1] = cur.w + 1;
pq.offer(new Node(cur.idx+1,cur.w+1));
}
if(cur.idx*2 <= 100000 && cur.w + 1 <= dist[cur.idx*2]){
dist[cur.idx*2] = cur.w + 1;
pq.offer(new Node(cur.idx*2,cur.w+1));
}
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
count = 0;
if(N>=K){
System.out.println(N-K);
System.out.println(1);
return;
}
dist = new int[100001];
Arrays.fill(dist,INF);
pq = new PriorityQueue<>();
pq.offer(new Node(N,0));
dist[N]=0;
solve();
System.out.println(dist[K]);
System.out.println(count);
}
static class Node implements Comparable<Node>{
int idx,w;
public Node(int idx, int w){
this.idx = idx;
this.w = w;
}
@Override
public int compareTo(Node o){
return this.w - o .w;
}
}
}
'๐์ฝ๋ฉํ ์คํธ:CodingTest' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฒจ๋ง ํฌ๋ ์๊ณ ๋ฆฌ์ฆ (0) | 2024.01.12 |
---|---|
์๋ก์ ์งํฉ ์๋ฃ๊ตฌ์กฐ (Union Find) (๋ฐฑ์ค 1043๋ฒ) (0) | 2024.01.03 |
์ด์ง ๊ฒ์ ํธ๋ฆฌ (๋ฐฑ์ค 5639๋ฒ) (0) | 2023.12.28 |
๋ค์ต์คํธ๋ผ (๋ฐฑ์ค 13549 ์จ๋ฐ๊ผญ์ง 3) (1) | 2023.12.26 |
ํธ๋ฆฌ์ ์ง๋ฆ (๋ฐฑ์ค 1167๋ฒ) (2) | 2023.12.26 |