https://www.acmicpc.net/problem/1059
유형
수학, 배열
풀이
코드
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
int N =stoi(st.nextToken());
int arr[] = new int[N];
st = new StringTokenizer(in.readLine());
for (int i = 0; i < arr.length; i++) {
arr[i]= stoi(st.nextToken());
}
st = new StringTokenizer(in.readLine());
int M = stoi(st.nextToken());
Arrays.sort(arr);
int left=0, right=987654321;
boolean find=false;
for (int i = 0; i < arr.length; i++) {
if (arr[i]>M) {
right=Math.min(right, arr[i]);
}else if(arr[i]<M){
left=Math.max(left, arr[i]);
}else {
find=true;
break;
}
}
int L = (M-left-1);
int R = (right-M-1);
int answer = L + R +(L*R);
if(find)
System.out.println(0);
else
System.out.println(answer);
}
static int stoi(String s) {
return Integer.valueOf(s);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 13902 - (Java)개업 2 (0) | 2021.08.21 |
---|---|
백준 16988 - (Java)Baaaaaaaaaduk2 (0) | 2021.08.19 |
백준 16985 - (Java) Maaaaaaaaaze (0) | 2021.08.17 |
백준 18223 - (Java)민준이와 마산 그리고 건우 (0) | 2021.08.17 |
[devmoon]백준 14938 - 서강그라운드 (0) | 2021.07.17 |