Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions algorithms/binary_search/moomin/G1477.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저랑 비슷하게 푸셨군요~! 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package binary_search.moomin;

import java.io.*;
import java.util.*;

public class G1477 {
static int N, M, L;
static int ans;
static int[] arr;
public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
L = Integer.parseInt(st.nextToken());

// 현재 휴게소
arr = new int[N+2];
arr[0] = 0;
arr[1] = L;
st = new StringTokenizer(br.readLine());
for(int i = 2; i<arr.length; i++){
arr[i] = Integer.parseInt(st.nextToken());
}

// 휴게소 정렬
Arrays.sort(arr);

// new 휴게소 위치 탐색
int left = 1;
int right = L;
while(left<=right){

// mid 구하기
int mid = (left+right) / 2;

int count = 0;
// mid 보다 작은 구간이 M개가 나오는지 확인
for(int i = 1; i<arr.length; i++){
int interval = arr[i] - arr[i-1];
// 구간이 mid 보다 작거나 같으면 pass
if(interval <= mid) continue;

// 구간이 mid 보다 크면
// mid가 즉 휴게소가 몇개가 들어갈 수 있는지 확인
count += ((interval - 1) / mid);
//System.out.println(interval/mid);
}

// count 가 M보다 크면
if(count > M) {
left = mid + 1;
continue;
}
// count 가 M보다 작거나 같으면
right = mid - 1;
ans = mid;
}
System.out.println(left);
}
}
54 changes: 54 additions & 0 deletions algorithms/binary_search/moomin/G20444.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저랑 비슷하게 푸셨네요~! 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package binary_search.moomin;

import java.io.*;
import java.util.*;

/**
* 4번의 가위질로 9개의 조각 만들기
* 1. 색종이는 직사각형이며, 색종이를 자를 때는 한 변에 평행하게 자른다.
* 2. 자르기 시작했으면, 경로 상의 모든 색종이를 자를 때까지 멈추지 않는다.
* 3. 이미 자른 곳을 또 자를 수 없다.
*
* 이분탐색: 가로 * 세로 = 색종이 조각
*/

public class G20444 {
static long N, K;
public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

// N, K 입력받기
N = Long.parseLong(st.nextToken());
K = Long.parseLong(st.nextToken());

// 가로 * 세로
long left = 0;
long right = N;
long count;

while(left <= right){

long mid = (left + right) / 2;

// 색종이 만들 수 있는지 탐색
count = (mid + 1) * ((N - mid) + 1);

if(count == K) {
System.out.println("YES");
return;
}

if(count > K) {
right = mid - 1;
continue;
}
left = mid + 1;
}
System.out.println("NO");
}
}



63 changes: 63 additions & 0 deletions algorithms/binary_search/moomin/G2470.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package binary_search.moomin;

import java.io.*;
import java.util.*;

public class G2470 {
static int N;
static int[] arr;
static int ans = Integer.MAX_VALUE;
static int[] ansSolution = new int[2];

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

// N 입력받기
N = Integer.parseInt(st.nextToken());

// 용액 입력받기
arr = new int[N];
st = new StringTokenizer(br.readLine());
for(int i = 0; i<N; i++){
arr[i] = Integer.parseInt(st.nextToken());
}

// 정렬 ex. -99 -2 -1 4 98
Arrays.sort(arr);

// 탐색 시작
// 가장 작은 수와 가장 큰수를 더한값의 절대값을 비교
int left = 0;
int right = arr.length-1;

while(left < right){

int mix = Math.abs(arr[left] + arr[right]);
if(mix < ans) {
ans = mix;
ansSolution[0] = arr[left];
ansSolution[1] = arr[right];
}

// 포인터 이동
// 같으면 왼,오 이동
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

같으면 왜 이동하는지 궁금합니다..!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

절대값이 같으면 이라고 표현한다는걸 저렇게 썼네요 !!
그런데 다시보니까 없어도 되는 코드 같습니다😂

if(arr[left] == arr[right]){
left++;
right--;
continue;
}

// 오른쪽 감소 vs 왼쪽 증가
if(Math.abs(arr[left] + arr[right-1]) < Math.abs(arr[left+1] + arr[right])){
right--;
continue;
}

left++;
}

System.out.println(ansSolution[0] + " " + ansSolution[1]);
}
}