diff --git "a/10_\354\235\264\353\266\204\355\203\220\354\203\211/10815.cpp" "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/10815.cpp" new file mode 100644 index 0000000..6e68d06 --- /dev/null +++ "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/10815.cpp" @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace std; + +int BinarySearch(int key, const vector &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 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; +} \ No newline at end of file diff --git "a/10_\354\235\264\353\266\204\355\203\220\354\203\211/14500.java" "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/14500.java" new file mode 100644 index 0000000..ea4fc67 --- /dev/null +++ "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/14500.java" @@ -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; + } +} diff --git "a/10_\354\235\264\353\266\204\355\203\220\354\203\211/16401.cpp" "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/16401.cpp" new file mode 100644 index 0000000..b48ddc1 --- /dev/null +++ "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/16401.cpp" @@ -0,0 +1,62 @@ +#include +#include +#include //max 함수 헤더파일 + + +using namespace std; + +//이분 탐색. 반복문으로 구현. +int binarySearch(int max_snack, const vector &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= m) { + left = mid+1; //오른쪽 이동 (더 큰 과자 사이즈로) + res = mid ; + + + } + + //나눠줄 수 있는 과자가 조카 수보다 작으면, 현재 길이로는 잘라줄 수 없음. + else { + right = mid-1; //왼쪽 이동 (더 작은 과자 사이즈로 자르기) + } + + } + + return res; +} + +int main() +{ + int m, n, t; + vector 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; +} \ No newline at end of file diff --git "a/10_\354\235\264\353\266\204\355\203\220\354\203\211/2343.cpp" "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/2343.cpp" new file mode 100644 index 0000000..f5be96b --- /dev/null +++ "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/2343.cpp" @@ -0,0 +1,85 @@ +#include +#include +#include //min 함수 헤더파일 + + +using namespace std; + +//블루레이 크기의 범위로 이분 탐색. 반복문으로 구현. +int binarySearch(int left, int right, const vector &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; imid) { //블루레이 하나가 현재 지정한 크기(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 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; +} \ No newline at end of file diff --git "a/10_\354\235\264\353\266\204\355\203\220\354\203\211/3079.cpp" "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/3079.cpp" new file mode 100644 index 0000000..f89f73d --- /dev/null +++ "b/10_\354\235\264\353\266\204\355\203\220\354\203\211/3079.cpp" @@ -0,0 +1,55 @@ +#include +#include +#include + +using namespace std; + +//이분 탐색 +unsigned long long binarySearch(const vector& 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 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; +}