[λ°±μ€(BOJ)] 2887_νμ± ν°λ JAVA
λ¬Έμ : BOJ_2887λ² νμ± ν°λ
λ¬Έμ μ€λͺ
3κ°μ μ’ν (x,y,z)λ‘ λ N κ°μ νμ±μ΄ μμ΅λλ€.
λ νμ±μ μ°κ²°ν μ μλ λΉμ©μ νμ±μ κ° μ’νμ μ°¨ μ€ μ΅μκ°μ΄λΌκ³ ν©λλ€.
μ΄λ λͺ¨λ νμ±μ ν°λλ‘ μ°κ²°ν λ μ΅μ λΉμ©μ ꡬνλ λ¬Έμ μ
λλ€.
Solution
ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦
λͺ¨λ λ
Έλλ₯Ό μ°κ²°νλ μ΅μ λΉμ©μ ν°λμ ꡬν΄μΌ νκΈ° λλ¬Έμ ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦μ μ¬μ©ν΄μΌ ν©λλ€.
μ£Όμ΄μ§ ν°λμ΄ μκΈ° λλ¬Έμ ν°λλ€μ λΉκ΅νλ©΄μ μ΅μκ°μ ν°λμ μ°ΎμμΌ ν©λλ€.
μ΄λ νμ±μ κ°μλ μ΅λ 100,000μ΄ μ£Όμ΄μ§λ―λ‘, λͺ¨λ νμ±μ μλ‘ λΉκ΅ν΄ μ€λ€λ©΄ μκ° μ΄κ³Όκ° λ°μν©λλ€.
μ μκ°ν΄ 보면 νμ± κ°μ μ΅μκ°μ νμ x, y, z κ°μ μ°¨μ μ΅μκ° μ€ νλμ΄κΈ° λλ¬Έμ, x κ°μ μ°¨κ° μ΅μμΈ νμ± ν°λ, y κ°μ μ°¨κ° μ΅μμΈ νμ± ν°λ, z κ°μ μ°¨κ° μ΅μμΈ νμ± ν°λλ€μ λ§λ€μ΄μ λΉκ΅ν΄ μ€λ€λ©΄, 3*N κ°μ ν°λλ‘ ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦μ μνν΄ λ¬Έμ λ₯Ό ν΄κ²°ν μ μμ΅λλ€.
Description
- ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦μ μκ° λ³΅μ‘λλ E*log(E)μ λλ€.
- κ° μ’νμ μΈλ±μ€μ μ’νκ°μ μ μ₯ν΄ μ£Όλ Pair, ν νμ±μ μ’νλ₯Ό λΉκ΅ν΄ μ£Όλ Node ν΄λμ€λ₯Ό λ§λ€μμ΅λλ€.
- ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦ μ¬μ©μ μν΄ union-find μκ³ λ¦¬μ¦μ ꡬννμ΅λλ€.
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
class Node{
private int x;
private int y;
private int z;
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public Node(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}
class Pair implements Comparable<Pair>{
private int idx;
private int value;
public Pair(int idx, int value){
this.idx = idx;
this.value = value;
}
public int getIdx() {
return idx;
}
public int getValue() {
return value;
}
@Override
public int compareTo(Pair other) {
return Integer.compare(this.value,other.value);
}
}
class ConnectSpace implements Comparable<ConnectSpace>{
private int go;
private int ed;
private int value;
public ConnectSpace(int go, int ed, int value){
this.go = go;
this.ed = ed;
this.value = value;
}
public int getGo() {
return go;
}
public int getEd() {
return ed;
}
public int getValue() {
return value;
}
@Override
public int compareTo(ConnectSpace other) {
return Integer.compare(this.value,other.value);
}
}
public class Main {
public static ArrayList<Node> nodeArr = new ArrayList<>();
public static ArrayList<Pair> xArr = new ArrayList<>();
public static ArrayList<Pair> yArr = new ArrayList<>();
public static ArrayList<Pair> zArr = new ArrayList<>();
public static ArrayList<ConnectSpace> searchArr = new ArrayList<>();
public static int calcDist(Node a, Node b){
return Math.min(Math.abs(a.getX()-b.getX()) , Math.min(Math.abs(a.getY()-b.getY()) , Math.abs(a.getZ() - b.getZ())));
}
public static int n;
public static int[] parent;
public static int sum;
public static int find(int idx){
if(idx==parent[idx]) return idx;
else return parent[idx] = find(parent[idx]);
}
public static void merge(int x ,int y){
x = find(x);
y = find(y);
if(x==y) return;
if(x<y) parent[y] = x;
else parent[x] = y;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
parent = new int[n];
for(int i=0; i<n; i++) parent[i] = i;
for(int i=0;i<n;i++){
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int z = Integer.parseInt(st.nextToken());
nodeArr.add(new Node(x,y,z));
xArr.add(new Pair(i,x));
yArr.add(new Pair(i,y));
zArr.add(new Pair(i,z));
}
Collections.sort(xArr);
Collections.sort(yArr);
Collections.sort(zArr);
for(int i=0; i<n-1; i++){
searchArr.add(new ConnectSpace(xArr.get(i).getIdx(), xArr.get(i+1).getIdx() , calcDist(nodeArr.get(xArr.get(i).getIdx()),nodeArr.get(xArr.get(i+1).getIdx()))));
searchArr.add(new ConnectSpace(yArr.get(i).getIdx(), yArr.get(i+1).getIdx() , calcDist(nodeArr.get(yArr.get(i).getIdx()),nodeArr.get(yArr.get(i+1).getIdx()))));
searchArr.add(new ConnectSpace(zArr.get(i).getIdx(), zArr.get(i+1).getIdx() , calcDist(nodeArr.get(zArr.get(i).getIdx()),nodeArr.get(zArr.get(i+1).getIdx()))));
}
Collections.sort(searchArr);
for(int i=0; i<searchArr.size();i++){
int go = searchArr.get(i).getGo();
int ed = searchArr.get(i).getEd();
int value = searchArr.get(i).getValue();
if(find(go)==find(ed)) continue;
else {
merge(go,ed);
sum += value;
}
}
bw.write(String.valueOf(sum)+"\n");
bw.flush();
br.close();
bw.close();
}
}
'Algorithm > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] μΉν¨ λ°°λ¬(15686) (0) | 2022.03.16 |
---|---|
[BOJ] μκΈ° μμ΄(16236) (0) | 2022.03.15 |
[Programmers] μλ¬Όμ μ μ΄μ (2020 KAKAO BLIND RECRUITMENT) (0) | 2022.03.15 |
[BOJ] λ±(3190) (0) | 2022.03.15 |
[BOJ] μμ μ΄λ 2μ κ±°λ μ κ³±μ μ’μν΄! μμ μ΄λ 2μ κ±°λ μ κ³±μ μ’μν΄!(20153) (0) | 2022.03.15 |