https://www.acmicpc.net/problem/2110
2110번: 공유기 설치
첫째 줄에 집의 개수 N (2 ≤ N ≤ 200,000)과 공유기의 개수 C (2 ≤ C ≤ N)이 하나 이상의 빈 칸을 사이에 두고 주어진다. 둘째 줄부터 N개의 줄에는 집의 좌표를 나타내는 xi (0 ≤ xi ≤ 1,000,000,000)가
www.acmicpc.net
import java.util.*;
import java.io.*;
public class Main
{
public static int n;
public static int m;
public static Integer[] houses;
public static boolean check(int dist){
int count =1;
int cur = houses[0];
for(int i =1; i<n; i++){
if(cur+dist > houses[i]) {
continue;
}else{
count++;
cur = houses[i];
}
}
if(count>=m) {
return true;
}else{
return false;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input1 = br.readLine().split(" ");
n = Integer.valueOf(input1[0]);
m = Integer.valueOf(input1[1]);
houses = new Integer[n];
for(int i=0; i<n;i++){
houses[i] = Integer.valueOf(br.readLine());
}
Arrays.sort(houses);
int l = 0;
int r = 1_000_000_000;
int ans = 0;
while(l<=r){
int mid = (l+r)/2;
if(check(mid)){
l = mid + 1;
ans = mid;
}else{
r = mid - 1;
}
}
System.out.println(ans);
}
}
import java.util.Arrays;
import java.util.Scanner;
class Main
{
static int calculateCount(int[] xs, int distance) {
int pastX = xs[0];
int cnt = 1;
for (int i = 1; i < xs.length; i++)
if (xs[i] - pastX >= distance) {
pastX = xs[i];
cnt++;
}
return cnt;
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int C = sc.nextInt();
int[] xs = new int[N];
for (int i = 0; i < N; i++)
xs[i] = sc.nextInt();
Arrays.sort(xs);
int l = 1, r = xs[N - 1] - xs[0], ans = -1;
while (l <= r) {
int m = (l + r) / 2;
if (calculateCount(xs, m) >= C) {
ans = m;
l = m + 1;
}
else r = m - 1;
}
System.out.println(ans);
}
}
'Problem Solving > 이진 탐색' 카테고리의 다른 글
BOJ2343 기타레슨 (2) | 2024.02.28 |
---|---|
BOJ2512 예산 (0) | 2024.02.28 |
BOJ6236 용돈관리 (매개변수 탐색) (1) | 2024.02.27 |
BOJ1654 랜선 자르기 (매개변수 탐색) (0) | 2024.02.27 |
BOJ2805 나무자르기 (매개변수탐색) (1) | 2024.02.27 |
https://www.acmicpc.net/problem/2110
2110번: 공유기 설치
첫째 줄에 집의 개수 N (2 ≤ N ≤ 200,000)과 공유기의 개수 C (2 ≤ C ≤ N)이 하나 이상의 빈 칸을 사이에 두고 주어진다. 둘째 줄부터 N개의 줄에는 집의 좌표를 나타내는 xi (0 ≤ xi ≤ 1,000,000,000)가
www.acmicpc.net
import java.util.*;
import java.io.*;
public class Main
{
public static int n;
public static int m;
public static Integer[] houses;
public static boolean check(int dist){
int count =1;
int cur = houses[0];
for(int i =1; i<n; i++){
if(cur+dist > houses[i]) {
continue;
}else{
count++;
cur = houses[i];
}
}
if(count>=m) {
return true;
}else{
return false;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input1 = br.readLine().split(" ");
n = Integer.valueOf(input1[0]);
m = Integer.valueOf(input1[1]);
houses = new Integer[n];
for(int i=0; i<n;i++){
houses[i] = Integer.valueOf(br.readLine());
}
Arrays.sort(houses);
int l = 0;
int r = 1_000_000_000;
int ans = 0;
while(l<=r){
int mid = (l+r)/2;
if(check(mid)){
l = mid + 1;
ans = mid;
}else{
r = mid - 1;
}
}
System.out.println(ans);
}
}
import java.util.Arrays;
import java.util.Scanner;
class Main
{
static int calculateCount(int[] xs, int distance) {
int pastX = xs[0];
int cnt = 1;
for (int i = 1; i < xs.length; i++)
if (xs[i] - pastX >= distance) {
pastX = xs[i];
cnt++;
}
return cnt;
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int C = sc.nextInt();
int[] xs = new int[N];
for (int i = 0; i < N; i++)
xs[i] = sc.nextInt();
Arrays.sort(xs);
int l = 1, r = xs[N - 1] - xs[0], ans = -1;
while (l <= r) {
int m = (l + r) / 2;
if (calculateCount(xs, m) >= C) {
ans = m;
l = m + 1;
}
else r = m - 1;
}
System.out.println(ans);
}
}
'Problem Solving > 이진 탐색' 카테고리의 다른 글
BOJ2343 기타레슨 (2) | 2024.02.28 |
---|---|
BOJ2512 예산 (0) | 2024.02.28 |
BOJ6236 용돈관리 (매개변수 탐색) (1) | 2024.02.27 |
BOJ1654 랜선 자르기 (매개변수 탐색) (0) | 2024.02.27 |
BOJ2805 나무자르기 (매개변수탐색) (1) | 2024.02.27 |