-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |