📢 공지합니다
이 게시글은 메인 페이지에 항상 고정되어 표시됩니다.
https://www.acmicpc.net/problem/2467
package implement;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Impl2467 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int[] liquid = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
liquid[i] = Integer.parseInt(st.nextToken());
}
int[] result = new int[2];
int min = Integer.MAX_VALUE;
int left = 0;
int right = N - 1;
while (left < right) {
int sum = Math.abs(liquid[left] + liquid[right]);
if (min >= sum) {
min = sum;
result[0] = liquid[left];
result[1] = liquid[right];
}
if (liquid[left] + liquid[right] >= 0) {
right--;
} else {
left++;
}
}
System.out.print(result[0] + " " + result[1]);
}
}
이분 탐색의 대표적인 문제이다! 필자가 많이 약한 분야이므로 철저히 복습해주자.
핵심은 0보다 크면 즉 양수일때 right 줄이고 음수일때는 left를 증가시키면 된다.
아래 문제 복습하자. 근데 아래 문제가 실버인데 이게 더 어려운듯?
https://balhae.tistory.com/264
[백준 2805번] 나무 자르기
https://www.acmicpc.net/problem/2805 수정전import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.StringTokenizer;public c
balhae.tistory.com
아래 문제도 참고!!! 비슷한 둘이~~
package implement;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Impl2230 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
List<Integer> num = new ArrayList<>();
for (int i = 0; i < N; i++) {
num.add(Integer.parseInt(br.readLine()));
}
Collections.sort(num);
int start = 0;
int end = 1;
int result = Integer.MAX_VALUE;
while (end < N) {
int sum = num.get(end) - num.get(start);
if (M <= sum) {
result = Math.min(result, sum);
}
if(result == M){
break;
}
if (sum >= M) {
start++;
} else {
end++;
}
}
System.out.println(result);
}
}
https://www.acmicpc.net/problem/2230
[백준 15685번] 드래곤 커브 (0) | 2025.05.29 |
---|---|
[백준 1025번] 제곱수 찾기 (1) | 2025.05.27 |
[백준 15684번] 사다리 조작 (0) | 2025.05.24 |
[백준 15683번] 감시 (0) | 2025.05.21 |
[백준 14891번] 톱니바퀴 (0) | 2025.05.20 |