Skip to content

Commit

Permalink
[이분탐색] 5월 27일
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-naa committed May 27, 2024
1 parent 99ec383 commit 47ef74c
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 6 deletions.
12 changes: 6 additions & 6 deletions 10_이분탐색/10815.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ using namespace std;
#define endl '\n'

int binarySearch(int num, vector<int>& card, int n) {
int low = 0;
int high = n-1;
int left = 0;
int right = n-1;

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

//원하는 값 찾았으면 1 반환
if(num == card[mid]) return 1;
//원하는 값이 mid보다 작을 경우
else if(num < card[mid]) {
high = mid - 1;
right = mid - 1;
}
//원하는 값이 mid보다 클 경우
else {
low = mid + 1;
left = mid + 1;
}
}

Expand Down
67 changes: 67 additions & 0 deletions 10_이분탐색/16401.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define endl '\n';

//m=조카수, n=과자수
bool check(int len, int m, int n, vector<int> &snack) {
int cnt = 0;

//과자 수가 조카 수보다 많으면
if (m < n) {
for(int i = n-1; i >= n-m-1; i--) {
cnt += snack[i] / len;
}
}
//조카 수가 과자 수보다 많으면
else {
for(int i = 0; i < n; i++) {
cnt += snack[i] / len;
}
}
//조카 모두에게 줄수있는지 확인
if(cnt >= m) return true;
else return false;
}

int binarySearch(int m, int n, vector<int> &snack) {

int max = 0;
int left = 1; //과자 최소길이
int right = snack[n-1]; //과자 최대길이
int mid;

while(left <= right) {
int mid = (left + right) / 2;
if(check(mid, m, n, snack)) {
max = mid;
left = mid + 1;
}
else {
right = mid - 1;
}
}
return max;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

//m = 조카 수, n = 과자 수
int m, n;
cin >> m >> n;

vector<int> snack(n);
for(int i=0; i<n; i++) {
cin >> snack[i];
}
sort(snack.begin(), snack.end());

cout << binarySearch(m, n, snack);

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

using namespace std;

#define endl '\n';

//굴다리 모두 비추는지 확인
//n = 굴다리 길이, m = 가로등 개수
bool check(int height, int n, int m, vector<int> location) {
//첫번째 가로등 위치 확인
if(location[0] > height) {
return false;
}
//중간 가로등 위치 확인
//이전 가로등에서 hegith*2보다 멀리있으면 false
for(int i=1; i<m; i++) {
if(location[i] > location[i-1] + 2*height) {
return false;
}
}
//마지막 가로등 위치 확인
if(n - location[m-1] > height) {
return false;
}

return true;
}

int binarySearch(int n, int m, vector<int> location) {
int left = 0;
int right = n;

int height, mid;

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

//가로등 길이 조건 부합할 경우 줄여서 탐색
if(check(mid, n, m, location)) {
height = mid;
right = mid - 1;
}
//조건 부합 아닐 경우 늘려서 탐색
else {
left = mid + 1;
}
}

return height;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);

//n=굴다리 길이, m=가로등 개수
int n, m;
cin >> n >> m;
vector<int> location(m);

for(int i=0; i<m; i++) {
cin >> location[i];
}

cout << binarySearch(n, m, location);

return 0;
}

0 comments on commit 47ef74c

Please sign in to comment.