Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11 투포인터 #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
56 changes: 56 additions & 0 deletions 10_이분탐색/10815.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int BinarySearch(int key, const vector<int> &arr, int n) {
int left = 0;
int right = arr.size() - 1;
int mid ;

while(left <= right) {
mid = (left+right)/2;

if(arr[mid] == key) {
return 1;
} else if(arr[mid] < key) { //오른쪽으로
left = mid + 1;
} else { //왼쪽으로
right = mid -1;
}
}

return 0;
}

int main()
{

cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(NULL);

int n,m,t;
vector <int> arr;
cin >> n;

while(n--) {
cin >> t ;
arr.push_back(t);
}


//이분탐색은 우선, 정렬 필수 !!
sort(arr.begin(), arr.end());


cin >> m ;

while(m--) {

cin >> t;
cout << BinarySearch(t, arr, n) << ' ';
}

return 0;
}
80 changes: 80 additions & 0 deletions 10_이분탐색/14500.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import java.util.Scanner;

public class Main {
static int N, M;
static int[][] board;
static int max_val;
static int[][] d = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
static boolean[][] visited;
static int answer = 0;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
N = scanner.nextInt();
M = scanner.nextInt();
board = new int[N][M];

for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
board[i][j] = scanner.nextInt();
}
}

max_val = findMaxValue(board);

d = new int[][] { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
visited = new boolean[N][M];

for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
visited[i][j] = true;
dfs(i, j, 1, board[i][j]);
visited[i][j] = false;
}
}

System.out.println(answer);
}

static void dfs(int x, int y, int step, int total) {
if (total + max_val * (4 - step) <= answer) {
return;
}

if (step == 4) {
answer = Math.max(answer, total);
return;
}

for (int[] direction : d) {
int nx = x + direction[0];
int ny = y + direction[1];

if (isValid(nx, ny) && !visited[nx][ny]) {
if (step == 2) {
visited[nx][ny] = true;
dfs(x, y, step + 1, total + board[nx][ny]);
visited[nx][ny] = false;
}

visited[nx][ny] = true;
dfs(nx, ny, step + 1, total + board[nx][ny]);
visited[nx][ny] = false;
}
}
}

static boolean isValid(int x, int y) {
return x >= 0 && x < N && y >= 0 && y < M;
}

static int findMaxValue(int[][] arr) {
int max = Integer.MIN_VALUE;
for (int[] row : arr) {
for (int val : row) {
max = Math.max(max, val);
}
}
return max;
}
}
62 changes: 62 additions & 0 deletions 10_이분탐색/16401.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <vector>
#include <algorithm> //max 함수 헤더파일


using namespace std;

//이분 탐색. 반복문으로 구현.
int binarySearch(int max_snack, const vector<int> &snack_length, int m) {
int cnt = 0; //나눠줄 수 있는 총 과자의 수
int res = 0;

int left = 1;
int right = max_snack;
int mid=0;

while(left <= right) {
cnt = 0;
mid = (left+right) / 2;

for(int i=0; i<snack_length.size(); i++) {
cnt += snack_length[i]/mid;
}

//나눠줄 수 있는 과자가 조카 수보다 많으면, 현재 길이로 잘라 나눠줄 수 있음 !
if(cnt >= m) {
left = mid+1; //오른쪽 이동 (더 큰 과자 사이즈로)
res = mid ;


}

//나눠줄 수 있는 과자가 조카 수보다 작으면, 현재 길이로는 잘라줄 수 없음.
else {
right = mid-1; //왼쪽 이동 (더 작은 과자 사이즈로 자르기)
}

}

return res;
}

int main()
{
int m, n, t;
vector <int> snack_length; //과자 길이들 저장할 벡터

cin >> m >> n;

int max_snack = 1; //과자의 최대 길이 저장할 변수

//과자 길이 입력받기
while(n--) {
cin >> t ;
snack_length.push_back(t);
max_snack = max(t, max_snack);
}

cout << binarySearch(max_snack, snack_length, m);

return 0;
}
85 changes: 85 additions & 0 deletions 10_이분탐색/2343.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <iostream>
#include <vector>
#include <algorithm> //min 함수 헤더파일


using namespace std;

//블루레이 크기의 범위로 이분 탐색. 반복문으로 구현.
int binarySearch(int left, int right, const vector<int> &lessons, int m) {
int cnt = 0; //현재 사용한 블루레이 개수
int res = 0;
int sum = 0; //블루레이 하나에 들어간 강의들 총 길이합
int mid=0;

while(left <= right) {
cnt = 0;
sum=0;
mid = (left+right) /2; //현재 블루레이의 크기



//cnt 계산
for(int i=0; i<lessons.size(); i++) {
sum += lessons[i];

if(sum>mid) { //블루레이 하나가 현재 지정한 크기(mid)를 초과했으면,
cnt++;
sum=lessons[i]; //현재 강의부터 다음 블루레이에 담기
} else if(sum==mid) { //블루레이 하나가 딱 맞게 찼으면,
cnt++;
sum=0; //다시 빈 블루레이에 채우기 시작 !
}

}

if(sum>0) {
cnt++; //마지막 블루레이 포함!
}



if(cnt <= m) { //사용 가능한 블루레이 수 이하로 사용함
res = mid; //현재 크기 답안으로 저장 !

//여기서 블루레이 개수를 더 줄일 수 있을까? 그럴려면 각 블루레이에 강의를 덜 담아야함. 현재 블루레이 크기보다 줄여야해 !
right = mid-1;

}

else { //사용 가능한 블루레이수보다 많이 사용함
//각 블루레이에 강의를 더 담아야함. 현재 블루레이 크기보다 늘여야해 !
left = mid+1;
}


}

return res;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

int m, n, t;
vector <int> lessons; //강의 길이 벡터

cin >> n >> m;

int all_lessons = 0; //모든 레슨의 길이 총 합
int max_lessons = 0; //가장 긴 레슨 길이

//강의 길이 입력받기
while(n--) {
cin >> t ;
lessons.push_back(t);
all_lessons += t;
max_lessons = max(max_lessons, t);
}

cout << binarySearch(max_lessons, all_lessons, lessons, m);

return 0;
}
55 changes: 55 additions & 0 deletions 10_이분탐색/3079.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//이분 탐색
unsigned long long binarySearch(const vector<unsigned long long>& t, unsigned long long m, unsigned long long left, unsigned long long right) {
unsigned long long res = 0;
unsigned long long mid = 0;
unsigned long long q = 0;

while (left <= right) {
mid = (left + right) / 2;
q=0;

for (int t : t) {
q += mid / t;
}

if (q >= m) { //m명 다 심사함
right = mid - 1;
res = mid;
} else {
left = mid + 1;
}
}

return res;
}

int main() {

ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

unsigned long long n,m,temp; //자료형 주의! 아니면 시간 초과..


vector <unsigned long long> t;

//입력
cin >> n >> m;
while(n--) {
cin >> temp;
t.push_back(temp);
}

//이분탐색 전 정렬 필수 !
sort(t.begin(), t.end());

cout << binarySearch(t, m, 0, t[0] * m);

return 0;
}
Loading