-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1253.java
More file actions
115 lines (106 loc) · 3.19 KB
/
Copy path1253.java
File metadata and controls
115 lines (106 loc) · 3.19 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// https://www.acmicpc.net/problem/1253
// 좋다
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
static int n, arr[], answer = 0;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(reader.readLine());
arr = new int[n];
StringTokenizer st = new StringTokenizer(reader.readLine());
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
for(int i=0; i<n; i++){
if(isGood(i))
answer++;
}
System.out.println(answer);
}
public static boolean isGood(int index){
for(int i=0; i<n; i++){
if(i == index) continue;
if(contains(arr[index] - arr[i], Math.min(index, i), Math.max(index, i))){
//System.out.println(arr[index]+" is good: "+arr[i]);
return true;
}
}
return false;
}
public static boolean contains(int value, int ex1, int ex2){
int start = -1, middle = -1, end = -1;
if(value == arr[ex1]){
int i = ex1;
while(true){
i++;
if(i == ex2) continue;
if(i >= n) break;
if(arr[i] == value) return true;
else break;
}
i = ex1;
while(true){
i--;
if(i == ex2) continue;
if(i < 0) break;
if(arr[i] == value) return true;
else break;
}
return false;
}
else if(value == arr[ex2]){
int i = ex2;
while(true){
i++;
if(i == ex1) continue;
if(i >= n) break;
if(arr[i] == value) return true;
else break;
}
i = ex2;
while(true){
i--;
if(i == ex1) continue;
if(i < 0) break;
if(arr[i] == value) return true;
else break;
}
return false;
}
else if(value < arr[ex1]){
start = 0;
end = ex1 - 1;
if(end < 0) return false;
}
else if(arr[ex1] < value && value < arr[ex2]){
start = ex1 + 1;
end = ex2 - 1;
if(start >= n || end < 0) return false;
}
else if(arr[ex2] < value){
start = ex2 + 1;
end = n - 1;
if(start >= n) return false;
}
while(start <= end){
middle = start + (end - start) / 2;
if(arr[middle] > value){
end = middle - 1;
}
else if(arr[middle] < value){
start = middle + 1;
}
else return true;
}
return false;
}
public static void print(int[] arr){
StringBuilder sb = new StringBuilder();
for(int i=0; i<arr.length; i++)
sb.append(arr[i]).append(' ');
System.out.println(sb);
}
}