Skip to content

Commit 0d09dfd

Browse files
authored
Merge pull request aerimforest#667 from aerimforest/hyeonjin
[Beakjoon] 22-12-31
2 parents e761dec + fcfab15 commit 0d09dfd

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
public static void main(String[] args) throws Exception{
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
StringTokenizer str;
8+
9+
int N = Integer.valueOf(br.readLine());
10+
int[] arr = new int[N];
11+
str = new StringTokenizer(br.readLine());
12+
for(int i = 0; i < N; i++){
13+
arr[i] = Integer.valueOf(str.nextToken());
14+
}
15+
int X = Integer.valueOf(br.readLine());
16+
17+
Arrays.sort(arr);
18+
19+
int result = 0;
20+
int left = 0;
21+
int right = N - 1;
22+
while(left < right){
23+
int num = arr[left] + arr[right];
24+
if(num == X) result++;
25+
26+
if(num < X) left++;
27+
else right--;
28+
}
29+
30+
System.out.println(result);
31+
}
32+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static List<Distance> distance;
6+
static int[] check;
7+
static int N;
8+
public static void main(String[] args) throws Exception{
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
StringTokenizer str;
12+
13+
N = Integer.valueOf(br.readLine());
14+
List<Point> star = new ArrayList<>();
15+
distance = new ArrayList<>();
16+
check = new int[N];
17+
for(int i = 0; i < N; i++){
18+
str = new StringTokenizer(br.readLine());
19+
star.add(new Point(Double.valueOf(str.nextToken()), Double.valueOf(str.nextToken()), i));
20+
check[i] = i;
21+
}
22+
23+
for(int i = 0; i < N; i++){
24+
for(int j = i + 1; j < N; j++){
25+
distance.add(new Distance(star.get(i), star.get(j)));
26+
}
27+
}
28+
29+
Collections.sort(distance);
30+
kruskal();
31+
}
32+
33+
private static void kruskal() {
34+
List<Distance> result = new ArrayList<>();
35+
for(int i = 0; i < distance.size(); i++){
36+
Distance dist = distance.get(i);
37+
38+
if(find_root(dist.p1.index) == find_root(dist.p2.index)) continue;
39+
40+
union_root(dist);
41+
result.add(dist);
42+
43+
if(result.size() == N - 1){
44+
double sum = 0;
45+
for(int j = 0; j < result.size(); j++){
46+
sum += result.get(j).distance;
47+
}
48+
49+
System.out.println(sum);
50+
return;
51+
}
52+
}
53+
}
54+
55+
public static void union_root(Distance dist){
56+
int x = find_root(dist.p1.index);
57+
int y = find_root(dist.p2.index);
58+
59+
if(x != y) check[y] = x;
60+
}
61+
62+
public static int find_root(int x){
63+
if(check[x] == x) return x;
64+
return check[x] = find_root(check[x]);
65+
}
66+
67+
68+
public static class Point{
69+
double x;
70+
double y;
71+
int index;
72+
public Point(double x, double y, int index){
73+
this.x = x;
74+
this.y = y;
75+
this.index = index;
76+
}
77+
}
78+
79+
public static class Distance implements Comparable<Distance>{
80+
Point p1;
81+
Point p2;
82+
double distance;
83+
public Distance(Point p1, Point p2){
84+
this.p1 = p1;
85+
this.p2 = p2;
86+
this.distance = Math.round(Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2)) * 100) / 100.0;
87+
}
88+
89+
@Override
90+
public int compareTo(Distance o){
91+
return Double.compare(this.distance, o.distance);
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)