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

12-suhyun113 #42

Merged
merged 4 commits into from
Sep 9, 2024
Merged
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
4 changes: 3 additions & 1 deletion suhyun113/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
| 7์ฐจ์‹œ | 2024.05.09 | ํŠธ๋ฆฌ | [์›์ˆญ์ด ๋งค๋‹ฌ๊ธฐ](https://www.acmicpc.net/problem/2716) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/31) |
| 8์ฐจ์‹œ | 2024.05.14 | ์ˆ˜ํ•™ | [์–ด๋ฆฐ ์™•์ž](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) |
| 9์ฐจ์‹œ | 2024.05.28 | ๋‹ค์ต์ŠคํŠธ๋ผ | [์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ](https://www.acmicpc.net/problem/1916) | [#33](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/33) |
| 10์ฐจ์‹œ | 2024.07.13 | ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ | [ํ”Œ๋กœ์ด๋“œ](https://www.acmicpc.net/problem/11404) | [#36](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/36) |
| 10์ฐจ์‹œ | 2024.07.13 | ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ | [ํ”Œ๋กœ์ด๋“œ](https://www.acmicpc.net/problem/11404) | [#36](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/36) |
| 11์ฐจ์‹œ | 2024.07.16 | BFS | [์—ฐ๊ตฌ์†Œ](https://www.acmicpc.net/problem/14502) | [#39](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/39) |
| 12์ฐจ์‹œ | 2024.07.24 | ์ด๋ถ„ ํƒ์ƒ‰ | [๋‚˜๋ฌด ์ž๋ฅด๊ธฐ](https://www.acmicpc.net/problem/2805) | [#42](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/42) |
106 changes: 106 additions & 0 deletions suhyun113/๊ตฌํ˜„/11-suhyun113.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//14502 : ์—ฐ๊ตฌ์†Œ

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

// ๋ฉ”ํฌ๋กœ ์„ค์ •
#define BLANK 0
#define WALL 1
#define VIRUS 2

int N, M; // ์ง€๋„์˜ ์„ธ๋กœ ํฌ๊ธฐ, ๊ฐ€๋กœ ํฌ๊ธฐ
vector<vector<int>> initial_map; // ์ดˆ๊ธฐ ์ง€๋„
vector<pair<int, int>> blank_map; // ๋นˆ ์นธ
int directions[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // ๋ฐฉํ–ฅ

// ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ฒฝ ์„ธ์šฐ๊ธฐ ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ
vector<vector<pair<int, int>>> building_walls() {
vector<vector<pair<int, int>>> combinations;
int size = blank_map.size(); // ๋นˆ ์นธ ํฌ๊ธฐ
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
for (int k = j + 1; k < size; k++) {
// ๋นˆ ์นธ์— ๋ฒฝ 3๊ฐœ ์„ธ์šฐ๊ธฐ
combinations.push_back({blank_map[i], blank_map[j], blank_map[k]});
}
}
}
return combinations;
}

// ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(๋ฐ”์ด๋Ÿฌ์Šค์ธ ์นธ ํ์— ์ €์žฅ ํ›„,
// ๋ฐ”์ด๋Ÿฌ์Šค ์นธ ์ฃผ๋ณ€ ์นธ์ด ๋ฐ”์ด๋Ÿฌ์Šค ์•„๋‹Œ ๋นˆ ์นธ์ด๋ผ๋ฉด,
// ์ฃผ๋ณ€ ์นธ์„ ๋ฐ”์ด๋Ÿฌ์Šค๋กœ ๋ฐ”๊พธ๊ธฐ)
void bfs(vector<vector<int>>& map_copy) {
queue<pair<int, int>> q;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map_copy[i][j] == VIRUS) {
q.push({i, j});
}
}
}
// ๋ฐ”์ด๋Ÿฌ์Šค์ธ ์นธ์ด ์•„๋‹ˆ๊ณ  ๋นˆ ์นธ์ด๋ผ๋ฉด,
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int d = 0; d < 4; d++) { // ๋ฐฉํ–ฅ ํƒ์ƒ‰
int nx = x + directions[d][0];
int ny = y + directions[d][1];
if (nx >= 0 && nx < N && ny >= 0 && ny < M && map_copy[nx][ny] == 0) {
map_copy[nx][ny] = VIRUS; // ๋นˆ ์นธ์„ ๋ฐ”์ด๋Ÿฌ์Šค๋กœ ๋ฐ”๊พธ๊ธฐ
q.push({nx, ny});
}
}
}
}

// ์•ˆ์ „ ์˜์—ญ ํฌ๊ธฐ ๊ณ„์‚ฐ ํ•จ์ˆ˜
int calculate_safe_area(vector<vector<int>>& map_copy) {
int safe_area = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map_copy[i][j] == BLANK) { // ๋นˆ ์นธ์˜ ๊ฐœ์ˆ˜
safe_area++;
}
}
}
return safe_area;
}

int main() {
cin >> N >> M;
initial_map.resize(N, vector<int>(M));

for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> initial_map[i][j];
if (initial_map[i][j] == BLANK) {
blank_map.push_back({ i, j }); // ๋นˆ ์นธ ์ง€๋„ ์ƒ์„ฑ
}
}
}

vector<vector<pair<int, int>>> wall_combinations = building_walls(); // ๋นˆ ์นธ์— ๋ฒฝ ์„ธ์šฐ๊ธฐ
int max_safe_area = 0; // ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ

for (const auto& walls : wall_combinations) { // ๋ฒฝ 3๊ฐœ๋ฅผ ์„ธ์šด ์—ฌ๋Ÿฌ ์กฐํ•ฉ ๋ฐ˜๋ณต
// ์ดˆ๊ธฐ ๋งต ๋ณต์‚ฌํ•ด์„œ ๊ทธ ๋งต์— ๋ฒฝ 3๊ฐœ ์„ธ์šฐ๋Š” ์—ฌ๋Ÿฌ ์กฐํ•ฉ ์ค‘ ํ•œ ๊ฒฝ์šฐ ์ ์šฉ
vector<vector<int>> map_copy = initial_map;
for (const auto& wall : walls) { // ํ•œ ๊ฒฝ์šฐ์—์„œ ์„ธ์šด ๋ฒฝ 3๊ฐœ์˜ ๊ฐ๊ฐ ์œ„์น˜์— ๋ฒฝ ์„ธ์šฐ๊ธฐ
map_copy[wall.first][wall.second] = WALL; // ๋ฒฝ ์„ธ์šฐ๊ธฐ
}

bfs(map_copy); // ๋ฐ”์ด๋Ÿฌ์Šค ์ฃผ๋ณ€ ์นธ ๋นˆ ์นธ์ด๋ผ๋ฉด ๋ฐ”์ด๋Ÿฌ์Šค ๋ฒˆ์ง

int safe_area = calculate_safe_area(map_copy); // ์•ˆ์ „์˜์—ญ ํฌ๊ธฐ ๊ณ„์‚ฐ
max_safe_area = max(max_safe_area, safe_area);
}

cout << max_safe_area << endl;
return 0;
}
51 changes: 51 additions & 0 deletions suhyun113/์ด๋ถ„ ํƒ์ƒ‰/12-suhyun113.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 2805 : ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ

#include <iostream>
#include <vector>
using namespace std;

// ์ ˆ๋‹จ๊ธฐ์— ์ง€์ •ํ•œ ๋‚˜๋ฌด์˜ ๋†’์ด H๋กœ ๋‚˜๋ฌด๋ฅผ ์ž๋ฆ„
// H๋ณด๋‹ค ํฌ๋ฉด ์ž๋ฅด๊ธฐ(์ž‘์œผ๋ฉด, ๊ฐ’์ด 0 ๋˜๋Š” ์Œ์ˆ˜๋กœ ๋‚˜์˜ด)
bool can_cut(vector<int>& trees, int H, int M) {
long long total = 0; // ์ž๋ฅธ ๋‚˜๋ฌด๋“ค์˜ ์ด ํ•ฉ
for (int tree : trees) {
if (tree > H) {
total += (tree - H);
}
}
return total >= M; // ์ด ํ•ฉ์ด M ๋ณด๋‹ค๋Š” ์ปค์•ผ ํ•จ
}

int binary_search(vector<int>& trees, int M) {
int lo = 0;
int hi = 1000000000;

while (lo + 1 < hi) {
int mid = (lo + hi) / 2; // ์ค‘์•™๊ฐ’ ์ €์žฅ

if (can_cut(trees, mid, M)) { // ์ ˆ๋‹จ๊ธฐ ๋†’์ด ๋ณ€๊ฒฝ
lo = mid; // ํ˜„์žฌ์˜ ์ ˆ๋‹จ๊ธฐ ๋†’์ด๋ฅผ ๊ฐ€๋Šฅํ•œ ์ตœ์†Œ ๋†’์ด๋กœ ์ €์žฅ(์ค‘์•™๊ฐ’ ๋” ์ปค์ง)
}
else {
hi = mid; // ํ˜„์žฌ ์ ˆ๋‹จ๊ธฐ ๋†’์ด ์•ˆ๋˜๋ฉด, ๋” ๋‚ฎ์€ ๋†’์ด๋กœ ํ™•์ธ
}
}

return lo; // canCut์„ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋ฏ€๋กœ
}

int main() {
int N, M;
cin >> N >> M;

// N๊ฐœ์˜ ๋‚˜๋ฌด๋“ค์˜ ๊ธธ์ด ๋ชฉ๋ก ๋งŒ๋“ค๊ธฐ
vector<int> trees(N);
for (int i = 0; i < N; i++) {
cin >> trees[i];
}

int result = binary_search(trees, M);
cout << result << endl;

return 0;
}