-
Notifications
You must be signed in to change notification settings - Fork 0
Week10 #10
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
base: main
Are you sure you want to change the base?
Week10 #10
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <algorithm> | ||
| using namespace std; | ||
|
|
||
| int main() { | ||
| int n, m; | ||
| cin >> n; | ||
| vector<int> cards(n); | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| cin >> cards[i]; | ||
| } | ||
|
|
||
| // 숫자 카드 배열 정렬 | ||
| sort(cards.begin(), cards.end()); | ||
|
|
||
| cin >> m; | ||
| vector<int> query(m); | ||
| for (int i = 0; i < m; i++) { | ||
| cin >> query[i]; | ||
| } | ||
|
|
||
| for (int i = 0; i < m; i++) { | ||
| // 이분 탐색을 통해 query[i]가 cards에 있는지 확인 | ||
| if (binary_search(cards.begin(), cards.end(), query[i])) { | ||
| cout << "1 "; | ||
| } else { | ||
| cout << "0 "; | ||
| } | ||
|
Comment on lines
+26
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: 이분 탐색을 사용해 특정 숫자의 존재 여부를 확인하는 로직을 잘 구현해 주셨네요! 다만 공부의 차원에서 라이브러리의 함수를 사용하지 않고 직접 이분 탐색을 구현해 보셔도 좋을 것 같습니다 😊 |
||
| } | ||
|
|
||
| cout << endl; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: 시간 효율을 위해 줄바꿈은 endl보다는 \n을 사용하는 것을 추천드립니다! |
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <algorithm> | ||
| using namespace std; | ||
|
|
||
| bool canDivideSnacks(const vector<int>& snacks, int numChildren, int length) { | ||
| int count = 0; | ||
| for (int snack : snacks) { | ||
| count += snack / length; | ||
| if (count >= numChildren) return true; // 필요한 아이 수만큼 나눠줄 수 있으면 true | ||
| } | ||
| return count >= numChildren; | ||
|
Comment on lines
+10
to
+12
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2. 앞의 for문에서 |
||
| } | ||
|
|
||
| int main() { | ||
| int numChildren, numSnacks; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3. 변수명은 스네이크 코드 형식을 따라주세요! |
||
| cin >> numChildren >> numSnacks; | ||
|
|
||
| vector<int> snacks(numSnacks); | ||
| for (int i = 0; i < numSnacks; i++) { | ||
| cin >> snacks[i]; | ||
| } | ||
|
|
||
| // 과자의 길이를 내림차순으로 정렬하여 큰 길이부터 탐색하도록 합니다. | ||
| sort(snacks.begin(), snacks.end(), greater<int>()); | ||
|
Comment on lines
+24
to
+25
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2. 맞아요! 이분탐색을 할 때는 탐색할 값들이 반드시 정렬되어 있어야 한다고 했죠. 🤗 그런데 이 문제에서는 가지고 있는 과자들의 길이 자체는 탐색하지 않으며, 이건 순서에 상관없이 주어진 길이로 과자 몇 개를 나눠줄 수 있는지 계산할 때만 사용돼요. 그래서 |
||
|
|
||
| int left = 1, right = snacks[0], answer = 0; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 매개변수 탐색을 위한 초깃값을 잘 설정해주셨네요 💯 |
||
| while (left <= right) { | ||
| int mid = (left + right) / 2; | ||
| if (canDivideSnacks(snacks, numChildren, mid)) { | ||
| answer = mid; // 가능한 길이이므로 저장 | ||
| left = mid + 1; // 더 큰 길이를 탐색 | ||
| } else { | ||
| right = mid - 1; // 길이가 부족하면 더 작은 길이를 탐색 | ||
| } | ||
| } | ||
|
Comment on lines
+27
to
+36
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2. 이분 탐색을 하는 부분은 따로 함수로 빼면 좋을 것 같아요~!
Comment on lines
+28
to
+36
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 탐색 부분, 함수화 모두 좋습니다! 💯💯💯 |
||
|
|
||
| cout << answer << endl; | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <algorithm> | ||
| using namespace std; | ||
|
|
||
| bool canLightUpTunnel(int height, const vector<int>& positions, int tunnelLength) { | ||
| int currentCoverage = 0; | ||
| for (int pos : positions) { | ||
| if (currentCoverage < pos - height) { | ||
| return false; | ||
| } | ||
| currentCoverage = pos + height; | ||
| } | ||
| return currentCoverage >= tunnelLength; | ||
| } | ||
|
|
||
| int main() { | ||
| int tunnelLength, numLights; | ||
| cin >> tunnelLength >> numLights; | ||
|
|
||
| vector<int> positions(numLights); | ||
| for (int i = 0; i < numLights; i++) { | ||
| cin >> positions[i]; | ||
| } | ||
|
|
||
| int left = 1, right = tunnelLength, answer = tunnelLength; | ||
| while (left <= right) { | ||
| int mid = (left + right) / 2; | ||
| if (canLightUpTunnel(mid, positions, tunnelLength)) { | ||
| answer = mid; | ||
| right = mid - 1; | ||
| } else { | ||
| left = mid + 1; | ||
| } | ||
| } | ||
|
|
||
| cout << answer << endl; | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <algorithm> | ||
| using namespace std; | ||
|
|
||
| bool canDivideLectures(const vector<int>& lessons, int numBluRays, int maxSize) { | ||
| int count = 1, currentSum = 0; | ||
| for (int lesson : lessons) { | ||
| if (currentSum + lesson > maxSize) { | ||
| count++; // 새로운 블루레이에 녹화 | ||
| currentSum = lesson; | ||
| if (count > numBluRays) return false; | ||
| } else { | ||
| currentSum += lesson; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| int main() { | ||
| int numLessons, numBluRays; | ||
| cin >> numLessons >> numBluRays; | ||
|
|
||
| vector<int> lessons(numLessons); | ||
| int maxLesson = 0, totalSum = 0; | ||
| for (int i = 0; i < numLessons; i++) { | ||
| cin >> lessons[i]; | ||
| maxLesson = max(maxLesson, lessons[i]); | ||
| totalSum += lessons[i]; | ||
| } | ||
|
|
||
| int left = maxLesson, right = totalSum, answer = totalSum; | ||
| while (left <= right) { | ||
| int mid = (left + right) / 2; | ||
| if (canDivideLectures(lessons, numBluRays, mid)) { | ||
| answer = mid; // 가능한 블루레이 크기이므로 저장 | ||
| right = mid - 1; // 더 작은 크기를 탐색 | ||
| } else { | ||
| left = mid + 1; // 더 큰 크기를 탐색 | ||
| } | ||
| } | ||
|
|
||
| cout << answer << endl; | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <algorithm> | ||
| using namespace std; | ||
|
|
||
| bool canProcessAll(int64_t time, const vector<int>& times, int64_t numPeople) { | ||
| int64_t totalProcessed = 0; | ||
| for (int t : times) { | ||
| totalProcessed += time / t; // 각 심사대에서 주어진 시간 내에 처리할 수 있는 사람 수 | ||
| if (totalProcessed >= numPeople) return true; // 필요한 사람 수를 모두 처리할 수 있으면 true | ||
| } | ||
| return totalProcessed >= numPeople; | ||
| } | ||
|
|
||
| int main() { | ||
| int numStations; | ||
| int64_t numPeople; | ||
| cin >> numStations >> numPeople; | ||
|
|
||
| vector<int> times(numStations); | ||
| int maxTime = 0; | ||
| for (int i = 0; i < numStations; i++) { | ||
| cin >> times[i]; | ||
| maxTime = max(maxTime, times[i]); | ||
| } | ||
|
|
||
| int64_t left = 1, right = static_cast<int64_t>(maxTime) * numPeople; | ||
| int64_t answer = right; | ||
|
|
||
| while (left <= right) { | ||
| int64_t mid = (left + right) / 2; | ||
| if (canProcessAll(mid, times, numPeople)) { | ||
| answer = mid; // 가능한 시간이면 저장 | ||
| right = mid - 1; // 더 작은 시간으로 탐색 | ||
| } else { | ||
| left = mid + 1; // 더 큰 시간으로 탐색 | ||
| } | ||
| } | ||
|
|
||
| cout << answer << endl; | ||
| return 0; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3. 모든 로직이 메인함수에 구현되어 있네요! 이 문제는 짧은 코드로도 구현이 가능하니 괜찮지만, 그래도 최대한 로직별로 함수를 분리하는 것을 추천드립니다 👍