-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreePointAngle_009.java
More file actions
91 lines (74 loc) · 2.04 KB
/
ThreePointAngle_009.java
File metadata and controls
91 lines (74 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package java;
import java.util.*;
/**
* 009 - Three Point Angle(★6)
*
* 二分探索法
* 偏角ソート
* 真ん中決め打ち
*/
public class ThreePointAngle_009 {
private static int N;
private static Pos[] posList;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
posList = new Pos[N];
for (int i = 0; i < N; i++) {
posList[i] = new Pos(sc.nextInt(), sc.nextInt());
}
List<Double> ans = new ArrayList<>();
for (int i = 0; i < N; i++) ans.add(0.0);
for (int i = 0; i < N; i++) {
List<Double> angles = new ArrayList<>();
for (int j = 0; j < N; j++) {
if (i == j) continue;
angles.add(computeAngle(posList[j].minus(posList[i])));
}
Collections.sort(angles);
for (Double angle : angles) {
Double target = (angle + 180) % 360;
int left = 0;
int right = angles.size() - 1;
while (right - left > 1) {
int mid = (int) (left + right) / 2;
if (angles.get(mid) <= target) left = mid;
else right = mid;
}
Double temp = Math.max(computeAmplitude(angles.get(left), angle), computeAmplitude(angles.get(right), angle));
ans.set(i, Math.max(ans.get(i), temp));
}
}
System.out.println(Collections.max(ans));
sc.close();
}
public static Double computeAngle(Pos pos) {
Double posAngleRad = Math.atan2(pos.y, pos.x);
posAngleRad += (posAngleRad >= 0 ? 0 : 2 * Math.PI);
return posAngleRad * 180 / Math.PI;
}
public static Double computeAmplitude(Double angle1, Double angle2) {
Double amplitude = Math.abs(angle1 - angle2);
return (amplitude <= 180 ? amplitude : 360 - amplitude);
}
}
/**
* Struct(DTO)
*/
class Pos {
public int x;
public int y;
public Pos(int x, int y) {
this.x = x;
this.y = y;
}
/**
* 引き算
*
* @param pos Posインスタンス
* @return Posインスタンス
*/
public Pos minus(Pos pos) {
return new Pos(this.x - pos.x, this.y - pos.y);
}
}