From 3ae0faad2bd951748143fb00be9e68bb122f0d60 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 25 Jan 2021 14:00:51 +0900 Subject: [PATCH 001/203] =?UTF-8?q?=EA=B0=95=EC=9D=98=EC=8B=A4=20=EB=B0=B0?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../11000.cpp" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/11000.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/11000.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/11000.cpp" new file mode 100644 index 0000000..7f1e158 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/11000.cpp" @@ -0,0 +1,64 @@ +#include +#include +#include + +using namespace std; + +int N; + +typedef struct Node +{ + int start, end; + + inline bool operator<(const Node &a) const + { + if (a.end < end) + return true; + return false; + } +} Node; + +vector > classes; +priority_queue pq; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + int ans = 0, i, x, y; + cin >> N; + for (i = 0; i < N; i++) + { + cin >> x >> y; + classes.push_back({x, y}); + } + + sort(classes.begin(), classes.end()); + + for (i = 0; i < N; i++) + { + if (pq.empty()) + { + pq.push({classes[i].first, classes[i].second}); + ans++; + } + else if (pq.top().end <= classes[i].first) + { + Node c = pq.top(); + pq.pop(); + c.end = classes[i].second; + pq.push(c); + } + else + { + Node c; + c.start = classes[i].first; + c.end = classes[i].second; + pq.push(c); + ans++; + } + } + + cout << ans << "\n"; + return 0; +} From f45cd598376d9ad640136fd519495230fedb5e7a Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 25 Jan 2021 14:20:16 +0900 Subject: [PATCH 002/203] =?UTF-8?q?=ED=9A=8C=EC=9D=98=EC=8B=A4=20=EB=B0=B0?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1931.cpp" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/1931.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1931.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1931.cpp" new file mode 100644 index 0000000..cc8c9dc --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1931.cpp" @@ -0,0 +1,50 @@ +#include +#include +#include + +using namespace std; + +int N; +long long endtime = 0, ans = 0; + +struct Node +{ + long long start, end; + + inline bool operator<(const Node &a) const + { + if (end == a.end) + return start < a.start; + return end < a.end; + } +}; + +vector meetings; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int i; + long long x, y; + for (i = 0; i < N; i++) + { + cin >> x >> y; + meetings.push_back({x, y}); + } + + sort(meetings.begin(), meetings.end()); + + for (i = 0; i < N; i++) + { + if (meetings[i].start >= endtime) + { + ans++; + endtime = meetings[i].end; + } + } + + cout << ans << "\n"; + return 0; +} From b0b5ad4398d77fe89216d6ddcb085f015e0beccd Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 25 Jan 2021 14:40:32 +0900 Subject: [PATCH 003/203] =?UTF-8?q?=EC=88=98=20=EB=AC=B6=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1744.cpp" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/1744.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1744.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1744.cpp" new file mode 100644 index 0000000..bb3b068 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1744.cpp" @@ -0,0 +1,58 @@ +#include +#include +#include + +using namespace std; + +int N; +vector A; +vector B; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int i, x; + long long ans = 0; + cin >> N; + for (i = 0; i < N; i++) + { + cin >> x; + if (x > 0) + A.push_back(x); + else if (x <= 0) + B.push_back(x); + } + sort(A.begin(), A.end()); + sort(B.begin(), B.end()); + + for (i = A.size() - 1; i >= 0; i--) + { + if (i > 0 && ((long long)A[i] * A[i - 1]) > ((long long)A[i] + A[i - 1])) + { + ans += (long long)A[i] * A[i - 1]; + i--; + } + else + { + ans += A[i]; + } + } + for (i = 0; i < B.size(); i++) + { + if (i + 1 < B.size()) + { + ans += (long long)B[i] * B[i + 1]; + i++; + } + else + { + ans += B[i]; + } + } + + cout << ans << "\n"; + + return 0; +} From 58eb7265b264637f8811eb3667e39f42cb7b34a6 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 25 Jan 2021 14:58:28 +0900 Subject: [PATCH 004/203] =?UTF-8?q?=EB=B9=B5=EC=A7=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../3109.cpp" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/3109.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/3109.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/3109.cpp" new file mode 100644 index 0000000..05857d3 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/3109.cpp" @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int N, M; +string board[10000]; +bool visited[10000][500]; +int dir[3][2] = {{-1, 1}, {0, 1}, {1, 1}}; + +bool dfs(int i, int j) +{ + visited[i][j] = true; + if (j == M - 1) + return true; + + for (int d = 0; d < 3; d++) + { + int nx = i + dir[d][0]; + int ny = j + dir[d][1]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && board[nx][ny] == '.' && !visited[nx][ny]) + { + if (dfs(nx, ny)) + return true; + } + } + return false; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + int i, j; + for (i = 0; i < N; i++) + { + cin >> board[i]; + } + int ans = 0; + for (i = 0; i < N; i++) + { + if (board[i][0] == '.' && !visited[i][0]) + { + if (dfs(i, 0)) + ans++; + } + } + cout << ans << "\n"; + return 0; +} From 73d67639e5a0e6ff15cdfe36ec8bdb71f992872a Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 25 Jan 2021 16:05:03 +0900 Subject: [PATCH 005/203] =?UTF-8?q?=EB=A9=80=ED=8B=B0=ED=83=AD=20=EC=8A=A4?= =?UTF-8?q?=EC=BC=80=EC=A5=B4=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1700.cpp" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/1700.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1700.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1700.cpp" new file mode 100644 index 0000000..54c553d --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1700.cpp" @@ -0,0 +1,80 @@ +#include +#include + +using namespace std; + +int N, K; +vector elec; +int use[101]; +int uselength = 0; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> K; + int i, j, x, u; + int ans = 0; + for (i = 0; i < K; i++) + { + cin >> x; + elec.push_back(x); + } + + for (i = 0; i < K; i++) + { + int now = elec[i]; + + bool flag = false; + for (u = 0; u < uselength; u++) + { + if (use[u] == now) + { + flag = true; + break; + } + } + if (flag) + continue; + + if (uselength < N) + { + use[uselength] = now; + uselength++; + continue; + } + + int next = 0; + int lastIndex = -1; + + for (u = 0; u < uselength; u++) + { + flag = false; + for (j = i + 1; j < K; j++) + { + if (use[u] == elec[j]) + { + flag = true; + if (lastIndex < j) + { + lastIndex = j; + next = u; + } + break; + } + } + if (!flag) + { + next = u; + break; + } + } + + use[next] = now; + ans++; + } + + cout << ans << "\n"; + return 0; +} From b12d7d0c86462a801f11abc943d57ca317a3f417 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 25 Jan 2021 16:54:01 +0900 Subject: [PATCH 006/203] =?UTF-8?q?=EC=BB=B5=EB=9D=BC=EB=A9=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1781.cpp" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/1781.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1781.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1781.cpp" new file mode 100644 index 0000000..8e42c32 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1781.cpp" @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; +int N; + +struct Work +{ + long long deadline, ramen; + + bool operator<(const Work &a) const + { + if (ramen == a.ramen) + { + return deadline > a.deadline; + } + return ramen > a.ramen; + } +}; + +struct PushedWork +{ + long long deadline, ramen; + + bool operator<(const PushedWork &a) const + { + if (deadline == a.deadline) + { + return ramen < a.ramen; + } + return deadline > a.deadline; + } +}; + +priority_queue pq; +priority_queue pq2; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int i; + long long x, y; + long long ans = 0; + for (i = 0; i < N; i++) + { + cin >> x >> y; + pq.push({x, y}); + } + + int workdeadline = 1; + for (i = 1; i <= N; i++) + { + long long deadline = pq.top().deadline; + long long ramen = pq.top().ramen; + pq.pop(); + + //cout << i << " " << deadline << " " << ramen << "\n"; + + if (deadline >= workdeadline) + { + pq2.push({deadline, ramen}); + ans += ramen; + workdeadline++; + } + else if (!pq2.empty() && pq2.top().ramen < ramen) + { + pq2.push({deadline, ramen}); + ans += ramen - pq2.top().ramen; + pq2.pop(); + } + //cout << ans << "\n"; + } + + cout << ans << "\n"; + return 0; +} From 75226d823b999902ace59a5a1174aa2a6a20b02d Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 26 Jan 2021 19:02:20 +0900 Subject: [PATCH 007/203] =?UTF-8?q?=ED=81=AC=EA=B2=8C=20=EB=A7=8C=EB=93=A4?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2812.cpp" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/2812.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2812.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2812.cpp" new file mode 100644 index 0000000..7cf6cc7 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2812.cpp" @@ -0,0 +1,44 @@ +#include +#include + +using namespace std; + +int N, K; +string s; +vector stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> K >> s; + int i; + for (i = 0; i < N; i++) + { + if (stack.empty() || stack.back() >= s[i] - '0') + { + stack.push_back(s[i] - '0'); + } + else if (K > 0) + { + stack.pop_back(); + K--; + i--; + } + else + { + stack.push_back(s[i] - '0'); + } + } + while (K > 0) + { + stack.pop_back(); + K--; + } + for (int s : stack) + { + cout << s; + } + cout << "\n"; + return 0; +} From fc1819962203da484fb5b6cb58e14e27cc5cd8e6 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 26 Jan 2021 19:50:40 +0900 Subject: [PATCH 008/203] =?UTF-8?q?=EC=A4=84=EC=84=B8=EC=9A=B0=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/7570.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 baekjoon/DP/7570.cpp diff --git a/baekjoon/DP/7570.cpp b/baekjoon/DP/7570.cpp new file mode 100644 index 0000000..99956d2 --- /dev/null +++ b/baekjoon/DP/7570.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +int N; +int D[1000001]; +int ans = 0; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int i, x; + for (i = 1; i <= N; i++) + { + cin >> x; + D[x] = D[x - 1] + 1; + ans = max(ans, D[x]); + } + cout << N - ans << "\n"; + + return 0; +} From 953c4c7ec3f0cb694b4d23c62a7f6204c90e4d22 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 26 Jan 2021 20:15:15 +0900 Subject: [PATCH 009/203] =?UTF-8?q?=ED=95=A9=EB=B6=84=ED=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2225.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 baekjoon/DP/2225.cpp diff --git a/baekjoon/DP/2225.cpp b/baekjoon/DP/2225.cpp new file mode 100644 index 0000000..8e23af9 --- /dev/null +++ b/baekjoon/DP/2225.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int N, K; +long long D[201][201]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> K; + int i, j, k; + for (i = 1; i <= K; i++) + { + D[0][i] = 1; + } + for (i = 1; i <= N; i++) + { + D[i][1] = 1; + } + + for (i = 1; i <= N; i++) + { + for (j = 2; j <= K; j++) + { + D[i][j] = (D[i - 1][j] + D[i][j - 1]) % 1000000000; + } + } + + cout << D[N][K] << "\n"; + return 0; +} From 6f773dd2dbfbfd4d8be1fe6033ec5a8e0ee2d22a Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 26 Jan 2021 20:38:29 +0900 Subject: [PATCH 010/203] =?UTF-8?q?=EB=82=B4=EB=A6=AC=EB=A7=89=EA=B8=B8=20?= =?UTF-8?q?(DFS&DP)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/1520.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 baekjoon/DP/1520.cpp diff --git a/baekjoon/DP/1520.cpp b/baekjoon/DP/1520.cpp new file mode 100644 index 0000000..8cb4643 --- /dev/null +++ b/baekjoon/DP/1520.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; +int N, M; +int board[500][500]; +bool visited[500][500]; +int dp[500][500]; + +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int dfs(int i, int j) +{ + int res = dp[i][j]; + visited[i][j] = true; + + for (int x = 0; x < 4; x++) + { + int tx = i + dx[x]; + int ty = j + dy[x]; + if (tx >= 0 && ty >= 0 && tx < N && ty < M && board[tx][ty] < board[i][j]) + { + if (!visited[tx][ty]) + res += dfs(tx, ty); + else + res += dp[tx][ty]; + } + } + + dp[i][j] = res; + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + cin >> board[i][j]; + } + } + + dp[N - 1][M - 1] = 1; + cout << dfs(0, 0) << "\n"; + + return 0; +} From c4fe8e6d92fd5f8a25af132809866b2e3d8b310c Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 27 Jan 2021 13:52:08 +0900 Subject: [PATCH 011/203] =?UTF-8?q?=EB=8B=AC=EC=9D=B4=20=EC=B0=A8=EC=98=A4?= =?UTF-8?q?=EB=A5=B8=EB=8B=A4=20=EA=B0=80=EC=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1194.cpp" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 "baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1194.cpp" diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1194.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1194.cpp" new file mode 100644 index 0000000..538675e --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1194.cpp" @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; + +struct Move +{ + int x, y, turn; + unsigned int key; +}; + +bool visited[50][50][128]; +string board[50]; +int N, M; +queue bfsQueue; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + int i, j; + for (i = 0; i < N; i++) + { + cin >> board[i]; + for (j = 0; j < M; j++) + { + if (board[i][j] == '0') + { + bfsQueue.push({i, j, 0, 0}); + visited[i][j][0] = true; + } + } + } + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int turn = bfsQueue.front().turn; + unsigned int key = bfsQueue.front().key; + bfsQueue.pop(); + + if (board[x][y] == '1') + { + cout << turn << "\n"; + return 0; + } + + if (board[x][y] >= 'a' && board[x][y] <= 'f') + { + unsigned int k = 1 << (board[x][y] - 'a'); + key = (key | k); + } + else if (board[x][y] >= 'A' && board[x][y] <= 'F') + { + unsigned int k = 1 << (board[x][y] - 'A'); + if ((key & k) != k) + continue; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && !visited[nx][ny][key] && board[nx][ny] != '#') + { + bfsQueue.push({nx, ny, turn + 1, key}); + visited[nx][ny][key] = true; + } + } + } + + cout << "-1\n"; + return 0; +} From 7f6ad1ee0c885f2badd54832ccd64082c4ae3170 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 27 Jan 2021 14:24:53 +0900 Subject: [PATCH 012/203] =?UTF-8?q?=EB=B2=BD=20=EB=B6=80=EC=88=98=EA=B3=A0?= =?UTF-8?q?=20=EC=9D=B4=EB=8F=99=ED=95=98=EA=B8=B0=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14442.cpp" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14442.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14442.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14442.cpp" new file mode 100644 index 0000000..5122102 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14442.cpp" @@ -0,0 +1,74 @@ +#include +#include + +using namespace std; + +struct Move +{ + int x, y, k, turn; +}; + +string board[1000]; +bool visited[1000][1000][10]; +int N, M, K; +queue bfsQueue; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M >> K; + int i, j; + for (i = 0; i < N; i++) + cin >> board[i]; + + if (board[0][0] == '0') + { + bfsQueue.push({0, 0, 0, 1}); + visited[0][0][0] = true; + } + else if (K > 0) + { + bfsQueue.push({0, 0, 1, 1}); + visited[0][0][1] = true; + } + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int k = bfsQueue.front().k; + int turn = bfsQueue.front().turn; + bfsQueue.pop(); + + if (x == N - 1 && y == M - 1) + { + cout << turn << "\n"; + return 0; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (board[nx][ny] == '0' && !visited[nx][ny][k]) + { + bfsQueue.push({nx, ny, k, turn + 1}); + visited[nx][ny][k] = true; + } + else if (board[nx][ny] == '1' && k < K && !visited[nx][ny][k + 1]) + { + bfsQueue.push({nx, ny, k + 1, turn + 1}); + visited[nx][ny][k + 1] = true; + } + } + } + } + + cout << "-1\n"; + return 0; +} From 2cd1b0410142df5a187cd1a7f89eaff4dd86b17c Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 27 Jan 2021 15:01:50 +0900 Subject: [PATCH 013/203] =?UTF-8?q?4*N=20=ED=83=80=EC=9D=BC=20=EC=B1=84?= =?UTF-8?q?=EC=9A=B0=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2718.cpp" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 "baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2718.cpp" diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2718.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2718.cpp" new file mode 100644 index 0000000..3fab2c0 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2718.cpp" @@ -0,0 +1,57 @@ +#include + +using namespace std; +int T, N; +int dp_table[30][6]; + +int dp(int n, int status) +{ + int res; + if (dp_table[n][status] != 0) + return dp_table[n][status]; + + if (n == 0 && status == 1) + return 1; + else if (n == 0) + return 0; + if (n == 1) + { + if (status == 4) + return 0; + return 1; + } + + switch (status) + { + case 1: + res = dp(n - 1, 1) + dp(n - 1, 2) + dp(n - 1, 3) + dp(n - 1, 5) + dp(n - 2, 1); + break; + case 2: + res = dp(n - 1, 1) + dp(n - 1, 3); + break; + case 3: + res = dp(n - 1, 1) + dp(n - 1, 2); + break; + case 4: + res = dp(n - 1, 5); + break; + case 5: + res = dp(n - 1, 1) + dp(n - 1, 4); + break; + } + return dp_table[n][status] = res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> T; + int i; + for (i = 0; i < T; i++) + { + cin >> N; + cout << dp(N, 1) << "\n"; + } + return 0; +} From 0fe117525617a8d18351e057849e77d0f9fd689b Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 27 Jan 2021 15:25:02 +0900 Subject: [PATCH 014/203] =?UTF-8?q?2*N=20=ED=83=80=EC=9D=BC=20=EC=B1=84?= =?UTF-8?q?=EC=9A=B0=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14852.cpp" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/14852.cpp" diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/14852.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/14852.cpp" new file mode 100644 index 0000000..5cd08e3 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/14852.cpp" @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int N; +int dp_table[1000001][4]; + +int dp(int n, int status) +{ + if (n == 0) + { + if (status == 1) + return 1; + else + return 0; + } + else if (n == 1) + { + if (status == 1) + return 2; + else + return 1; + } + + int res = dp_table[n][status]; + if (res != 0) + return res; + + switch (status) + { + case 1: + res = ((long long)dp(n - 1, 1) * 2 + dp(n - 1, 2) + dp(n - 1, 3) + dp(n - 2, 1)) % 1000000007; + break; + case 2: + res = ((long long)dp(n - 1, 1) + dp(n - 1, 3)) % 1000000007; + break; + case 3: + res = ((long long)dp(n - 1, 1) + dp(n - 1, 2)) % 1000000007; + break; + } + + return dp_table[n][status] = res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + + cout << dp(N, 1) << "\n"; + return 0; +} From f8814ec1dba8109ae2f520c67066b2ea5bbbf852 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 27 Jan 2021 15:43:13 +0900 Subject: [PATCH 015/203] =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EC=A0=9C=EA=B1=B0?= =?UTF-8?q?=20-=20=EB=A9=94=EB=AA=A8=EB=A6=AC=20=EC=A0=9C=ED=95=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../13701.cpp" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/13701.cpp" diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/13701.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/13701.cpp" new file mode 100644 index 0000000..89dead9 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/13701.cpp" @@ -0,0 +1,23 @@ +#include + +using namespace std; +int a[1 << 20], x; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + while (cin >> x) + { + int q = x / 32; + int r = x % 32; + + if ((a[q] & (1 << r)) != (1 << r)) + { + cout << x << " "; + a[q] |= (1 << r); + } + } + return 0; +} From b1cdcd9f4161d3adc5ea74fa5998d6b8f54c6794 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 27 Jan 2021 16:07:36 +0900 Subject: [PATCH 016/203] =?UTF-8?q?=EB=8B=A8=EC=96=B4=20=EC=95=94=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../18119.cpp" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/18119.cpp" diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/18119.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/18119.cpp" new file mode 100644 index 0000000..25550f6 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/18119.cpp" @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int N, M; +unsigned int alpha[10000]; +unsigned int know = 0; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + cin >> N >> M; + + int i, j, x; + unsigned char c; + for (i = 'a' - 'a'; i <= 'z' - 'a'; i++) + { + know |= (1 << i); + } + + for (i = 0; i < N; i++) + { + string s; + cin >> s; + for (j = 0; j < s.length(); j++) + { + alpha[i] |= (1 << (s[j] - 'a')); + } + } + + for (i = 0; i < M; i++) + { + cin >> x >> c; + if (x == 1 && (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u')) + { + know &= ~(1 << (c - 'a')); + } + else if (x == 2) + { + know |= (1 << (c - 'a')); + } + + int count = 0; + for (j = 0; j < N; j++) + { + if ((know & alpha[j]) == alpha[j]) + count++; + } + cout << count << "\n"; + } + + return 0; +} From 78d52f721089206ad8ea371c718f2c5203b87f40 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 27 Jan 2021 17:31:05 +0900 Subject: [PATCH 017/203] =?UTF-8?q?=EB=8F=99=EC=A0=84=20=EB=92=A4=EC=A7=91?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1285.cpp" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1285.cpp" diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1285.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1285.cpp" new file mode 100644 index 0000000..3a22595 --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/1285.cpp" @@ -0,0 +1,61 @@ +#include +#include +#include + +using namespace std; + +int N; +unsigned int board[21]; +int ans = 400; + +void brute(int n) +{ + if (n == N) + { + int c = 0; + for (int i = 0; i < N; i++) + { + int t = 0; + int h = 0; + for (int j = 0; j < N; j++) + { + if ((board[j] & (1 << i)) == (1 << i)) + t++; + else + h++; + } + t = min(h, t); + c += t; + } + ans = min(ans, c); + return; + } + + board[n] = ~board[n]; + brute(n + 1); + board[n] = ~board[n]; + brute(n + 1); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + string s; + int i, j; + for (i = 0; i < N; i++) + { + cin >> s; + for (j = 0; j < N; j++) + { + if (s[j] == 'T') + board[i] |= (1 << j); + } + } + + brute(0); + cout << ans << "\n"; + + return 0; +} From 31f428abbe6ef52d5f0fcf647d2ea16b9a26f890 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 29 Jan 2021 13:49:20 +0900 Subject: [PATCH 018/203] =?UTF-8?q?=EC=84=B1=EA=B3=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2234.cpp" | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 "baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2234.cpp" diff --git "a/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2234.cpp" "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2234.cpp" new file mode 100644 index 0000000..47129aa --- /dev/null +++ "b/baekjoon/\353\271\204\355\212\270\353\247\210\354\212\244\355\202\271/2234.cpp" @@ -0,0 +1,90 @@ +#include +#include + +using namespace std; +int N, M; +int board[50][50]; +int room[50][50]; +int c[2500]; +bool visited[50][50]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int w[4] = {4, 1, 8, 2}; +int ans2 = 0; + +int dfs(int i, int j, int n) +{ + visited[i][j] = true; + room[i][j] = n; + + int res = 1; + + for (int x = 0; x < 4; x++) + { + int ni = i + dx[x]; + int nj = j + dy[x]; + if ((board[i][j] & w[x]) == 0) + { + if (ni >= 0 && nj >= 0 && ni < N && nj < M && !visited[ni][nj]) + { + res += dfs(ni, nj, n); + } + } + } + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> M >> N; + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + cin >> board[i][j]; + } + } + + int ans1 = 0; + + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + if (!visited[i][j]) + { + c[ans1] = dfs(i, j, ans1); + ans2 = max(c[ans1], ans2); + ans1++; + } + } + } + + int ans3 = c[0]; + + for (int dir = 0; dir < 4; dir++) + { + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + int nx = i + dx[dir]; + int ny = j + dy[dir]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && (board[i][j] & w[dir]) == w[dir] && room[i][j] != room[nx][ny]) + { + ans3 = max(ans3, c[room[i][j]] + c[room[nx][ny]]); + } + } + } + } + + cout << ans1 << "\n" + << ans2 << "\n" + << ans3 << "\n"; + + return 0; +} From f8b86e9fa4f50d2b5bc4387c2a9ea881c874e8b5 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 29 Jan 2021 15:30:55 +0900 Subject: [PATCH 019/203] =?UTF-8?q?=EB=A7=90=EC=9D=B4=20=EB=90=98=EA=B3=A0?= =?UTF-8?q?=20=EC=8B=B6=EC=9D=80=20=EC=9B=90=EC=88=AD=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1600.cpp" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1600.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1600.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1600.cpp" new file mode 100644 index 0000000..a77707f --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1600.cpp" @@ -0,0 +1,80 @@ +#include +#include + +using namespace std; + +struct Node +{ + int x, y, k, turn; +}; + +int K, W, H; +int board[200][200]; +bool visited[200][200][31]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int mx[8] = {-1, 1, -2, 2, -2, 2, -1, 1}; +int my[8] = {-2, -2, -1, -1, 1, 1, 2, 2}; + +queue bfsQueue; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> K >> H >> W; + int i, j; + for (i = 0; i < W; i++) + { + for (j = 0; j < H; j++) + { + cin >> board[i][j]; + } + } + + bfsQueue.push({0, 0, 0, 0}); + visited[0][0][0] = true; + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int k = bfsQueue.front().k; + int turn = bfsQueue.front().turn; + bfsQueue.pop(); + + if (x == W - 1 && y == H - 1) + { + cout << turn << "\n"; + return 0; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < W && ny < H && board[nx][ny] == 0 && !visited[nx][ny][k]) + { + bfsQueue.push({nx, ny, k, turn + 1}); + visited[nx][ny][k] = true; + } + } + if (k < K) + { + for (i = 0; i < 8; i++) + { + int nx = x + mx[i]; + int ny = y + my[i]; + if (nx >= 0 && ny >= 0 && nx < W && ny < H && board[nx][ny] == 0 && !visited[nx][ny][k + 1]) + { + bfsQueue.push({nx, ny, k + 1, turn + 1}); + visited[nx][ny][k + 1] = true; + } + } + } + } + + cout << "-1\n"; + + return 0; +} From 8aff5a3edb604b0abf23af64254f9c3632f49212 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 30 Jan 2021 14:24:24 +0900 Subject: [PATCH 020/203] =?UTF-8?q?=ED=83=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\354\212\244\355\203\235/2493.cpp" | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "baekjoon/\354\212\244\355\203\235/2493.cpp" diff --git "a/baekjoon/\354\212\244\355\203\235/2493.cpp" "b/baekjoon/\354\212\244\355\203\235/2493.cpp" new file mode 100644 index 0000000..0cfcd62 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/2493.cpp" @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; + +int N; +vector > stack; +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int i, x; + for (i = 0; i < N; i++) + { + cin >> x; + if (stack.empty()) + { + cout << "0 "; + stack.push_back({i + 1, x}); + } + else if (stack.back().second < x) + { + while (!stack.empty() && stack.back().second < x) + { + stack.pop_back(); + } + if (stack.empty()) + { + cout << "0 "; + } + else + { + cout << stack.back().first << " "; + } + stack.push_back({i + 1, x}); + } + else + { + cout << stack.back().first << " "; + stack.push_back({i + 1, x}); + } + } + return 0; +} From 95b048dc8b10fc099ce27b2a2409186bd50cc0e1 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 30 Jan 2021 14:37:54 +0900 Subject: [PATCH 021/203] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=ED=8F=AD?= =?UTF-8?q?=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\354\212\244\355\203\235/9935.cpp" | 60 ++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 "baekjoon/\354\212\244\355\203\235/9935.cpp" diff --git "a/baekjoon/\354\212\244\355\203\235/9935.cpp" "b/baekjoon/\354\212\244\355\203\235/9935.cpp" new file mode 100644 index 0000000..c4d1b7b --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/9935.cpp" @@ -0,0 +1,60 @@ +#include +#include + +using namespace std; +vector > stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + string s; + string target; + + cin >> s >> target; + + int i; + for (i = 0; i < s.length(); i++) + { + if (stack.empty()) + { + int tmp = target[0] == s[i] ? 0 : -1; + stack.push_back({s[i], tmp}); + } + else + { + int checkIndex = stack.back().second + 1; + int tmp; + if (target[checkIndex] == s[i]) + tmp = checkIndex; + else if (target[0] == s[i]) + tmp = 0; + else + tmp = -1; + + stack.push_back({s[i], tmp}); + } + + if (stack.back().second == target.length() - 1) + { + int count = 0; + while (count < target.length()) + { + stack.pop_back(); + count++; + } + } + } + + if(stack.empty()){ + cout << "FRULA\n"; + } + else{ + for(pair a : stack){ + cout << a.first; + } + cout <<"\n"; + } + return 0; +} From 9550d0e4abfbe280588f739953cb1f04d0894a08 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 30 Jan 2021 14:50:02 +0900 Subject: [PATCH 022/203] =?UTF-8?q?=EC=98=A4=ED=81=B0=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\354\212\244\355\203\235/17298.cpp" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "baekjoon/\354\212\244\355\203\235/17298.cpp" diff --git "a/baekjoon/\354\212\244\355\203\235/17298.cpp" "b/baekjoon/\354\212\244\355\203\235/17298.cpp" new file mode 100644 index 0000000..9bd2259 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/17298.cpp" @@ -0,0 +1,48 @@ +#include +#include + +using namespace std; + +int N; +int A[1000000]; +int ans[1000000]; +vector stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int i; + for (i = 0; i < N; i++) + { + cin >> A[i]; + } + for (i = N - 1; i >= 0; i--) + { + while (!stack.empty() && stack.back() <= A[i]) + { + stack.pop_back(); + } + + if (stack.empty()) + { + ans[i] = -1; + stack.push_back(A[i]); + } + else + { + ans[i] = stack.back(); + stack.push_back(A[i]); + } + } + + for (i = 0; i < N; i++) + { + cout << ans[i] << " "; + } + cout << "\n"; + + return 0; +} From b0fc8b9d8540923fd191001d76bb90e704574a5e Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 30 Jan 2021 15:36:00 +0900 Subject: [PATCH 023/203] =?UTF-8?q?=EC=98=A4=EC=95=84=EC=8B=9C=EC=8A=A4=20?= =?UTF-8?q?=EC=9E=AC=EA=B2=B0=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\354\212\244\355\203\235/3015.cpp" | 66 ++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 "baekjoon/\354\212\244\355\203\235/3015.cpp" diff --git "a/baekjoon/\354\212\244\355\203\235/3015.cpp" "b/baekjoon/\354\212\244\355\203\235/3015.cpp" new file mode 100644 index 0000000..0e0dc6a --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/3015.cpp" @@ -0,0 +1,66 @@ +#include +#include +#include + +using namespace std; +typedef long long ll; +vector stack; +int N; + +int getlb(int left, int right, ll key) +{ + if (left >= right) + return left; + + int middle = (left + right) / 2 + 1; + if (stack[middle] <= key) + { + return getlb(left, middle - 1, key); + } + else + { + return getlb(middle, right, key); + } +} + +int getSize(ll x) +{ + int index = getlb(0, stack.size() - 1, x); + return stack.size() - index; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int i; + ll x, ans = 0; + + for (i = 0; i < N; i++) + { + cin >> x; + if (stack.empty()) + { + stack.push_back(x); + } + else if (stack.back() > x) + { + ans += 1; + stack.push_back(x); + } + else + { + ans += getSize(x); + while (!stack.empty() && stack.back() < x) + { + stack.pop_back(); + } + stack.push_back(x); + } + } + + cout << ans << "\n"; + return 0; +} From 2aa93c71078bcc0609cb39ceafc682dd5b3ad197 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 30 Jan 2021 15:51:20 +0900 Subject: [PATCH 024/203] =?UTF-8?q?=EC=98=A4=EB=93=B1=ED=81=B0=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\354\212\244\355\203\235/17299.cpp" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "baekjoon/\354\212\244\355\203\235/17299.cpp" diff --git "a/baekjoon/\354\212\244\355\203\235/17299.cpp" "b/baekjoon/\354\212\244\355\203\235/17299.cpp" new file mode 100644 index 0000000..3ac8313 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/17299.cpp" @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +int N; +int A[1000000]; +int F[1000001]; +int ans[1000000]; +vector stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int i; + for (i = 0; i < N; i++) + { + cin >> A[i]; + F[A[i]]++; + } + + for (i = N - 1; i >= 0; i--) + { + + while (!stack.empty() && F[stack.back()] <= F[A[i]]) + { + stack.pop_back(); + } + + if (stack.empty()) + { + ans[i] = -1; + stack.push_back(A[i]); + } + else + { + ans[i] = stack.back(); + stack.push_back(A[i]); + } + } + + for (i = 0; i < N; i++) + { + cout << ans[i] << " "; + } + cout << "\n"; + + return 0; +} From 77191127e792c8ecfd8f2cc2bea2231d2940936d Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 2 Feb 2021 19:37:30 +0900 Subject: [PATCH 025/203] Update README.md --- README.md | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7cdbfb4..83bcea6 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,4 @@ ### Algorithm Study -#### 특이사항 - * 5557 DP심화 이차원으루다가 - * 1937 DFS&DP 욕심쟁이판다 - * 2583 뭐가 문제지 ...? -> sort할때 vector.begin() vector.end() - **list sort하는 게 더 쉽구나 list.sort()하면 그냥 끝 !** - * 2206 벽부수고이동 아직 푸는중 - * 2198 블랙잭 시간초과 이유 : 브루트 포스할 때 3번 뽑은 이후에도 계속 뽑아서 체크하기 때문이었다. - * 1339 아직 c++ 문법에 익숙하지 않은 나,, comp함수 직접 만들어서 적용하기, 내림차순 정렬 방법, include 해야한다 - * 3055 탈출에는 두개의 BFS가 있다. 물번지기와 고슴도치 이동, 두 가지 BFS를 하나의 Queue에 넣으면 더 쉽다! 클래스에다가 type설정하여 type별로 다르게 행동하도록 설정한다. - * 풀어야 할 것 : 1753 (다익스트라사용) , 3860 할로윈 묘지 (벨만포드 사용) / 그래프 이론 공부 : 단절점 찾기 - * 4195 map 자료구조 사용하는 방법 - -#### 일기와 메모 - * 2020.07.25 Codeforces 가입하고 (제일 쉬워보이는) round 656에서 문제를 쇼쇼쇽 풀어보았다. - * 2020.08.10 - 2020.08.21 SDS알고특강 - * 9, 10월 목표 : 쉬운문제에 머물지 말기 어려운 문제 도전 https://www.acmicpc.net/workbook/view/4349 문제집 사용해서 다양한 알고리즘 적용해보기 + * https://solved.ac/profile/cbr5964 + * https://www.notion.so/575a370490a1459d955c0ef9b8f38974 From 67df832ab4bb1eeae3dccf3d7b4c9c3688b539c3 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 2 Feb 2021 20:11:30 +0900 Subject: [PATCH 026/203] =?UTF-8?q?N=EB=B2=88=EC=A7=B8=20=ED=81=B0=20?= =?UTF-8?q?=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2075.cpp" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2075.cpp" diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2075.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2075.cpp" new file mode 100644 index 0000000..a9e02e3 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2075.cpp" @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +priority_queue pq; +int N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int i, j, x; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + cin >> x; + if (pq.size() < N) + pq.push(-x); + else if (-pq.top() < x) + { + pq.pop(); + pq.push(-x); + } + } + } + + cout << -pq.top() << "\n"; + + return 0; +} From 74b97f7a5e431a8c609d3a1868ed153c41583049 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 2 Feb 2021 21:21:10 +0900 Subject: [PATCH 027/203] =?UTF-8?q?=EC=86=8C=EC=88=98=EC=9D=98=20=EA=B3=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2014.cpp" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2014.cpp" diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2014.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2014.cpp" new file mode 100644 index 0000000..c254962 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2014.cpp" @@ -0,0 +1,54 @@ +#include +#include +#include + +using namespace std; + +typedef long long ll; +priority_queue pq; +map visited; +int K, N; +ll prime[100]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> K >> N; + + int i, j; + for (i = 0; i < K; i++) + { + cin >> prime[i]; + } + + pq.push(-1); + + ll maxValue = 1; + + while (N > 0) + { + ll cur = -pq.top(); + pq.pop(); + + for (i = 0; i < K; i++) + { + ll element = cur * prime[i]; + + if (pq.size() > N && maxValue < element) + continue; + + if (!visited.count(element)) + { + maxValue = max(element, maxValue); + pq.push(-element); + visited[element] = true; + } + } + N--; + } + + cout << -pq.top() << "\n"; + + return 0; +} From fc57b2799ed7cd1c8aa364850a1624dab6f1e55f Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 2 Feb 2021 22:45:27 +0900 Subject: [PATCH 028/203] PPAP --- .../16120.cpp" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/16120.cpp" diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/16120.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/16120.cpp" new file mode 100644 index 0000000..67cbcb2 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/16120.cpp" @@ -0,0 +1,83 @@ +#include +#include + +using namespace std; + +vector stack; + +char target[4] = {'P', 'P', 'A', 'P'}; + +void print() +{ + for (int s : stack) + { + cout << s << " "; + } + cout << "\n"; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + string s; + cin >> s; + + for (int i = 0; i < s.length(); i++) + { + + if (s[i] == 'P') + { + if (!stack.empty() && stack.back() == 1) + { + stack.push_back(2); + } + else + stack.push_back(0); + } + else + { + stack.push_back(1); + } + + while (stack.back() == 2) + { + stack.pop_back(); + stack.pop_back(); + if (!stack.empty() && stack.back() != 1) + { + stack.pop_back(); + if (!stack.empty() && stack.back() != 1) + { + stack.pop_back(); + } + else + { + cout << "NP\n"; + return 0; + } + } + else + { + cout << "NP\n"; + return 0; + } + + if (!stack.empty() && stack.back() == 1) + stack.push_back(2); + else + stack.push_back(0); + } + } + + if (!stack.empty() && (stack.back() == 1 || stack.size() > 1)) + { + cout << "NP\n"; + } + else + cout << "PPAP\n"; + + return 0; +} From 096be1d134e860d405ba158ac2ecbc4dc5c44e80 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 2 Feb 2021 23:01:27 +0900 Subject: [PATCH 029/203] =?UTF-8?q?=EC=95=95=EC=B6=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\354\212\244\355\203\235/1662.cpp" | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "baekjoon/\354\212\244\355\203\235/1662.cpp" diff --git "a/baekjoon/\354\212\244\355\203\235/1662.cpp" "b/baekjoon/\354\212\244\355\203\235/1662.cpp" new file mode 100644 index 0000000..ae4f479 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/1662.cpp" @@ -0,0 +1,49 @@ +#include +#include + +using namespace std; + +vector > stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + string s; + cin >> s; + + for (int i = 0; i < s.length(); i++) + { + if (s[i] == '(') + { + stack.push_back({-1, 0}); + } + else if (s[i] == ')') + { + + int tmp = 0; + while (stack.back().first != -1) + { + tmp += stack.back().first; + stack.pop_back(); + } + stack.pop_back(); + tmp *= stack.back().second; + stack.pop_back(); + stack.push_back({tmp, tmp}); + } + else + { + stack.push_back({1, s[i] - '0'}); + } + } + + int ans = 0; + while (!stack.empty()) + { + ans += stack.back().first; + stack.pop_back(); + } + cout << ans << "\n"; + return 0; +} From fadb617fe9b551f408876220adfa61b20f95a8ca Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 3 Feb 2021 12:18:44 +0900 Subject: [PATCH 030/203] =?UTF-8?q?=EC=A0=84=EC=83=9D=ED=96=88=EB=8D=94?= =?UTF-8?q?=EB=8B=88=20=EC=8A=AC=EB=9D=BC=EC=9E=84=20=EC=97=B0=EA=B5=AC?= =?UTF-8?q?=EC=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14698.cpp" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/14698.cpp" diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/14698.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/14698.cpp" new file mode 100644 index 0000000..7e6c3b9 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/14698.cpp" @@ -0,0 +1,39 @@ +#include +#include + +using namespace std; + +typedef long long ll; + +int T, N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> T; + int i, j; + for (i = 0; i < T; i++) + { + ll x, ans = 1; + cin >> N; + priority_queue pq; + for (j = 0; j < N; j++) + { + cin >> x; + pq.push(-x); + } + while (pq.size() > 1) + { + ll tmp = pq.top(); + pq.pop(); + tmp *= pq.top(); + pq.pop(); + //cout << tmp << " "; + pq.push(-tmp); + ans = (ans * (tmp % 1000000007)) % 1000000007; + } + cout << ans << "\n"; + } + return 0; +} From 1dfd595367189028568a318ec408325c6633ec8b Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 3 Feb 2021 12:48:32 +0900 Subject: [PATCH 031/203] =?UTF-8?q?=EC=9D=BC=EC=9A=94=EC=9D=BC=20=EC=95=84?= =?UTF-8?q?=EC=B9=A8=EC=9D=98=20=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1445.cpp" | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 "baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1445.cpp" diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1445.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1445.cpp" new file mode 100644 index 0000000..6d596df --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1445.cpp" @@ -0,0 +1,102 @@ +#include +#include + +using namespace std; + +int N, M; + +struct node +{ + int x, y, through, by; + + bool operator<(const node &a) const + { + if (through == a.through) + return by > a.by; + return through > a.through; + } +}; + +priority_queue pq; +string board[50]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +bool visited[50][50]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + int i, j; + for (i = 0; i < N; i++) + { + cin >> board[i]; + for (j = 0; j < M; j++) + { + if (board[i][j] == 'S') + { + pq.push({i, j, 0, 0}); + } + } + } + + while (!pq.empty()) + { + int x = pq.top().x; + int y = pq.top().y; + int through = pq.top().through; + int by = pq.top().by; + pq.pop(); + + if (board[x][y] == 'F') + { + cout << through << " " << by << "\n"; + break; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && !visited[nx][ny]) + { + if (board[nx][ny] == 'g') + { + pq.push({nx, ny, through + 1, by}); + visited[nx][ny] = true; + } + else if (board[nx][ny] == '.') + { + bool trash = false; + for (j = 0; j < 4; j++) + { + int cx = nx + dx[j]; + int cy = ny + dy[j]; + if (cx >= 0 && cy >= 0 && cx < N && cy < M && board[cx][cy] == 'g') + { + trash = true; + break; + } + } + if (trash) + { + pq.push({nx, ny, through, by + 1}); + } + else + { + pq.push({nx, ny, through, by}); + } + visited[nx][ny] = true; + } + else + { + pq.push({nx, ny, through, by}); + visited[nx][ny] = true; + } + } + } + } + return 0; +} From ce9dd34248c4abd180faa22dfb1a867b3677e703 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 3 Feb 2021 13:36:17 +0900 Subject: [PATCH 032/203] =?UTF-8?q?=EC=87=BC=ED=95=91=EB=AA=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../17612.cpp" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/17612.cpp" diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/17612.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/17612.cpp" new file mode 100644 index 0000000..b6086e4 --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/17612.cpp" @@ -0,0 +1,86 @@ +#include +#include + +using namespace std; + +struct out +{ + int n, number, time; + + bool operator<(const out &a) const + { + if (time == a.time) + return n < a.n; + return time > a.time; + } +}; + +struct in +{ + int n, number, time; + + bool operator<(const in &a) const + { + if (time == a.time) + return n > a.n; + return time > a.time; + } +}; + +priority_queue O; +priority_queue I; +int N, K; +long long ans = 0; +int totaltime = 0; +int cus[100000][2]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> K; + + int i, j, x, y; + for (i = 1; i <= K; i++) + { + I.push({i, 0, 0}); + } + + for (i = 0; i < N; i++) + { + cin >> cus[i][0] >> cus[i][1]; + } + + int current = 0; + int out = 1; + while (current < N) + { + if (I.top().time <= totaltime) + { + struct in t = I.top(); + I.pop(); + t.number = cus[current][0]; + t.time += cus[current][1]; + I.push(t); + O.push({t.n, t.number, t.time}); + cout << "in " << t.n << " " << current << " " << totaltime << "\n"; + current++; + } + else + { + totaltime++; + } + } + + while (!O.empty()) + { + ans += (long long)O.top().number * out; + O.pop(); + out++; + } + + cout << ans << "\n"; + + return 0; +} From f5e4974af97f74364eefabae9a3dc8fa16492353 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 3 Feb 2021 14:06:09 +0900 Subject: [PATCH 033/203] =?UTF-8?q?=EB=B3=B4=EC=84=9D=20=EB=8F=84=EB=91=91?= =?UTF-8?q?=20(multiset=20=EC=82=AC=EC=9A=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1202.cpp" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 "baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1202.cpp" diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1202.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1202.cpp" new file mode 100644 index 0000000..35475aa --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/1202.cpp" @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +using namespace std; + +struct Jew +{ + long long m, v; + + bool operator<(const Jew &a) const + { + if (v == a.v) + return m > a.m; + return v < a.v; + } +}; + +priority_queue jews; +long long ans; +multiset back; +int N, K; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> K; + long long x, y, i; + for (i = 0; i < N; i++) + { + cin >> x >> y; + jews.push({x, y}); + } + for (i = 0; i < K; i++) + { + cin >> x; + back.insert(x); + } + + while (!jews.empty()) + { + long long c = jews.top().v; + long long w = jews.top().m; + jews.pop(); + + multiset::iterator it = back.lower_bound(w); + //cout << *it << " "; + if (it != back.end()) + { + back.erase(it); + ans += c; + } + } + + cout << ans << "\n"; + return 0; +} From 1086483a22b7ca4b26f4fe209dadf2ed3ff95215 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 5 Feb 2021 15:05:08 +0900 Subject: [PATCH 034/203] =?UTF-8?q?=EB=8B=A4=EC=9D=B4=EC=96=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1484.cpp" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/1484.cpp" diff --git "a/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/1484.cpp" "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/1484.cpp" new file mode 100644 index 0000000..31f2305 --- /dev/null +++ "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/1484.cpp" @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int G; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> G; + + int left = 1, right = 1; + bool flag = false; + + while (right <= 100000) + { + long long now = (long long)right * right - (long long)left * left; + if (now == G) + { + flag = true; + cout << right << "\n"; + left++; + } + else if (now < G) + { + right++; + } + else + { + left++; + } + } + + if (!flag) + { + cout << "-1\n"; + } + + return 0; +} From 4f9fa9cd2372f80f5a03193ab0483ec87ba00751 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 5 Feb 2021 18:38:01 +0900 Subject: [PATCH 035/203] =?UTF-8?q?=ED=9A=8C=EC=A0=84=EC=B4=88=EB=B0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../15961.cpp" | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 "baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/15961.cpp" diff --git "a/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/15961.cpp" "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/15961.cpp" new file mode 100644 index 0000000..b7bdc07 --- /dev/null +++ "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/15961.cpp" @@ -0,0 +1,69 @@ +#include + +using namespace std; + +int N, D, K, C; +int include[3001]; +int A[3000000]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + int i, x; + int count = 0; + int ans = 0; + int kind = 0; + + cin >> N >> D >> K >> C; + include[C]++; + kind = 1; + + for (i = 0; i < N; i++) + { + cin >> A[i]; + } + + int left = 0, right = 0; + if (include[A[0]] == 0) + { + kind++; + } + include[A[0]]++; + count = 1; + ans = kind; + + while (left < N) + { + if (count <= K) + { + right++; + count++; + if (right == N) + right = 0; + + if (include[A[right]] == 0) + { + kind++; + } + include[A[right]]++; + } + else + { + include[A[left]]--; + if (include[A[left]] == 0) + { + kind--; + } + + left++; + count--; + } + + if (count == K) + ans = max(ans, kind); + } + + cout << ans << "\n"; + return 0; +} From 4fec4ad0a2941e58912939392fedd74501b6b60b Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 5 Feb 2021 19:55:59 +0900 Subject: [PATCH 036/203] 17136.cpp --- ...0\261\355\212\270\353\236\230\355\202\271" | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271" diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271" new file mode 100644 index 0000000..3b4698f --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271" @@ -0,0 +1,123 @@ +#include +#include + +using namespace std; +char board[10][10]; +int ans = 100; + +bool check(int i, int j, int size) +{ + if (i + size - 1 >= 10 || j + size - 1 >= 10) + return false; + + bool flag = true; + for (int x = i; x <= i + size - 1; x++) + { + for (int y = j; y <= j + size - 1 && flag; y++) + { + if (board[x][y] == '0') + flag = false; + } + } + return flag; +} + +void cover(int i, int j, int size) +{ + for (int x = i; x <= i + size - 1; x++) + { + for (int y = j; y <= j + size - 1; y++) + { + board[x][y] = '0'; + } + } +} + +void uncover(int i, int j, int size) +{ + for (int x = i; x <= i + size - 1; x++) + { + for (int y = j; y <= j + size - 1; y++) + { + board[x][y] = '1'; + } + } +} + +void dfs(int i, int j, int count, int paper, int arg[]) +{ + int size_paper[5] = {arg[0], arg[1], arg[2], arg[3], arg[4]}; + + if (count == 0) + { + ans = min(ans, paper); + return; + } + + if (i == 10) + { + return; + } + + if (ans <= paper) + return; + + int nexti = i; + int nextj = j + 1; + if (nextj == 10) + { + nexti++; + nextj = 0; + } + + if (board[i][j] == '1') + { + for (int size = 5; size >= 1; size--) + { + if (check(i, j, size) && size_paper[size - 1] < 5) + { + //cout << i << " " << j << " " << size << "\n"; + cover(i, j, size); + int arr[5] = {size_paper[0], size_paper[1], size_paper[2], size_paper[3], size_paper[4]}; + arr[size - 1]++; + //cout << i << " " << j << " " << count - size * size << " " << paper + 1 << " " << arr[0] << arr[1] << arr[2] << arr[3] << arr[4] << "\n"; + dfs(nexti, nextj, count - size * size, paper + 1, arr); + uncover(i, j, size); + } + } + } + else + { + dfs(nexti, nextj, count, paper, size_paper); + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + int i, j; + int count = 0; + for (i = 0; i < 10; i++) + { + for (j = 0; j < 10; j++) + { + cin >> board[i][j]; + if (board[i][j] == '1') + count++; + } + } + + int arr[5] = {0, 0, 0, 0, 0}; + dfs(0, 0, count, 0, arr); + + if (ans == 100) + { + cout << "-1\n"; + } + else + { + cout << ans << "\n"; + } + return 0; +} From 7f33735c02816e9b292b60f3ade963cc8db58d23 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 6 Feb 2021 19:35:00 +0900 Subject: [PATCH 037/203] =?UTF-8?q?Rename=20=EB=B0=B1=ED=8A=B8=EB=9E=98?= =?UTF-8?q?=ED=82=B9=20to=20=EB=B0=B1=ED=8A=B8=EB=9E=98=ED=82=B9-17136.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\260\261\355\212\270\353\236\230\355\202\271-17136.cpp" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271" => "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271-17136.cpp" (100%) diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271-17136.cpp" similarity index 100% rename from "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271" rename to "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271-17136.cpp" From 3b1d36f18cb5b7431cd7b4d81d7b8d18ca56740d Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 6 Feb 2021 19:35:22 +0900 Subject: [PATCH 038/203] =?UTF-8?q?=EC=95=8C=ED=8C=8C=EB=B2=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1987.cpp" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/1987.cpp" diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/1987.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/1987.cpp" new file mode 100644 index 0000000..1407c2b --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/1987.cpp" @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int R, C; +string board[20]; + +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +bool include[26]; +int ans = 0; + +void dfs(int i, int j, int dist) +{ + ans = max(ans, dist); + for (int x = 0; x < 4; x++) + { + int nx = i + dx[x]; + int ny = j + dy[x]; + if (nx >= 0 && ny >= 0 && nx < R && ny < C && !include[board[nx][ny] - 'A']) + { + include[board[nx][ny] - 'A'] = true; + dfs(nx, ny, dist + 1); + include[board[nx][ny] - 'A'] = false; + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> R >> C; + + int i, j; + for (i = 0; i < R; i++) + { + cin >> board[i]; + } + + include[board[0][0] - 'A'] = true; + dfs(0, 0, 1); + + cout << ans << "\n"; + + return 0; +} From a65652ec4b2052e57fc76cb7760a3a5bb72979d6 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 6 Feb 2021 20:37:13 +0900 Subject: [PATCH 039/203] =?UTF-8?q?=EC=A2=8B=EC=9D=80=EC=88=98=EC=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2661.cpp" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2661.cpp" diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2661.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2661.cpp" new file mode 100644 index 0000000..80c72ed --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2661.cpp" @@ -0,0 +1,52 @@ +#include + +using namespace std; +int N; +string ans = ""; + +void solve(int n, string str) +{ + //cout << n << " " << str << "\n"; + + if (n == N) + { + if (ans == "" || ans > str) + ans = str; + return; + } + + if (ans.length() == N && ans < str) + return; + + for (int i = 1; i <= 3; i++) + { + string now = str + char(i + '0'); + bool flag = true; + for (int j = 1; j <= now.length() / 2; j++) + { + //cout << now.substr(now.length() - j, j) << "/" << now.substr(now.length() - 2 * j, j) << "\n"; + if (now.substr(now.length() - j, j) == now.substr(now.length() - 2 * j, j)) + { + flag = false; + break; + } + } + if (flag) + { + solve(n + 1, now); + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + solve(0, ""); + cout << ans << "\n"; + + return 0; +} From 2d3113acc3265b7f19a326ad6d5fcede9103976f Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 6 Feb 2021 22:13:24 +0900 Subject: [PATCH 040/203] =?UTF-8?q?=EC=8B=A0=EA=B8=B0=ED=95=9C=20=EC=86=8C?= =?UTF-8?q?=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023.cpp" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2023.cpp" diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2023.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2023.cpp" new file mode 100644 index 0000000..51e757c --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2023.cpp" @@ -0,0 +1,57 @@ +#include + +using namespace std; +int N; + +bool check(string str) +{ + int n = 0; + + for (int i = 0; i < str.length(); i++) + { + n = n * 10 + (str[i] - '0'); + } + + if (n < 2) + return false; + + bool flag = true; + for (int i = 2; i <= n / 2; i++) + { + if (n % i == 0) + { + flag = false; + break; + } + } + return flag; +} + +void solve(int n, string str) +{ + //cout << n << " " << str << "\n"; + if (n == N) + { + cout << str << "\n"; + return; + } + + for (int i = 0; i < 10; i++) + { + string now = str + char('0' + i); + if (check(now.substr(0, n + 1))) + { + solve(n + 1, now); + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + cin >> N; + solve(0, ""); + return 0; +} From 89e01e5dfc383c4c08ec111e8f4997883c50c19a Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 7 Feb 2021 14:51:27 +0900 Subject: [PATCH 041/203] =?UTF-8?q?=EC=8A=A4=EB=8F=84=EC=BF=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2580.cpp" | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2580.cpp" diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2580.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2580.cpp" new file mode 100644 index 0000000..4580917 --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2580.cpp" @@ -0,0 +1,94 @@ +#include + +using namespace std; + +int board[9][9]; +bool isEnd = false; + +bool check(int x, int y, int n) +{ + for (int i = 0; i < 9; i++) + { + if (board[x][i] == n) + return false; + if (board[i][y] == n) + return false; + } + + for (int i = x - x % 3; i < x - x % 3 + 3; i++) + { + for (int j = y - y % 3; j < y - y % 3 + 3; j++) + { + if (board[i][j] == n) + return false; + } + } + + return true; +} + +void solve(int x, int y) +{ + if (x == 9 && y == 0) + { + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + cout << board[i][j] << " "; + } + cout << "\n"; + } + isEnd = true; + return; + } + + if (isEnd) + return; + + int nextx = x; + int nexty = y + 1; + if (nexty == 9) + { + nextx++; + nexty = 0; + } + + if (board[x][y] == 0) + { + for (int i = 1; i <= 9; i++) + { + if (check(x, y, i)) + { + board[x][y] = i; + solve(nextx, nexty); + board[x][y] = 0; + } + } + + return; + } + + solve(nextx, nexty); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + int i, j; + + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + { + cin >> board[i][j]; + } + } + + solve(0, 0); + + return 0; +} From 1a481bf9711c225a78f6f14c00bef8fa679ba4da Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 7 Feb 2021 14:56:21 +0900 Subject: [PATCH 042/203] =?UTF-8?q?=EC=8A=A4=EB=8F=84=EC=BF=A0=20-2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2239.cpp" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2239.cpp" diff --git "a/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2239.cpp" "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2239.cpp" new file mode 100644 index 0000000..30ce251 --- /dev/null +++ "b/baekjoon/\353\260\261\355\212\270\353\236\230\355\202\271/2239.cpp" @@ -0,0 +1,87 @@ +#include + +using namespace std; + +string board[9]; +bool isEnd = false; + +bool check(int x, int y, int n) +{ + for (int i = 0; i < 9; i++) + { + if (board[x][i] == n + '0') + return false; + if (board[i][y] == n + '0') + return false; + } + + for (int i = x - x % 3; i < x - x % 3 + 3; i++) + { + for (int j = y - y % 3; j < y - y % 3 + 3; j++) + { + if (board[i][j] == n + '0') + return false; + } + } + + return true; +} + +void solve(int x, int y) +{ + if (x == 9 && y == 0) + { + for (int i = 0; i < 9; i++) + { + cout << board[i] << "\n"; + } + isEnd = true; + return; + } + + if (isEnd) + return; + + int nextx = x; + int nexty = y + 1; + if (nexty == 9) + { + nextx++; + nexty = 0; + } + + if (board[x][y] == '0') + { + for (int i = 1; i <= 9; i++) + { + if (check(x, y, i)) + { + board[x][y] = '0' + i; + solve(nextx, nexty); + board[x][y] = '0'; + } + } + + return; + } + + solve(nextx, nexty); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + int i, j; + + for (i = 0; i < 9; i++) + { + cin >> board[i]; + } + + solve(0, 0); + + return 0; +} From 5ba88a8117c619d5c6261d47655cefca53a54d7d Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 9 Feb 2021 20:59:08 +0900 Subject: [PATCH 043/203] LCA --- .../11437.cpp" | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 "baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/11437.cpp" diff --git "a/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/11437.cpp" "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/11437.cpp" new file mode 100644 index 0000000..c49ecac --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/11437.cpp" @@ -0,0 +1,96 @@ +#include +#include + +using namespace std; + +int N, T; + +vector edges[50001]; +int depth[50001]; +bool visited[50001]; +int lca[50001][16]; + +void dfs(int n, int d, int pre) +{ + visited[n] = true; + depth[n] = d; + lca[n][0] = pre; + + for (int v : edges[n]) + { + if (!visited[v]) + { + dfs(v, d + 1, n); + } + } +} + +void buildlca() +{ + for (int j = 1; j < 16; j++) + { + for (int i = 1; i <= N; i++) + { + lca[i][j] = lca[lca[i][j - 1]][j - 1]; + } + } +} + +int getlca(int x, int y) +{ + if (depth[x] > depth[y]) + { + swap(x, y); + } + + for (int i = 15; i >= 0; i--) + { + if (depth[y] - depth[x] >= (1 << i)) + { + y = lca[y][i]; + } + } + + if (x == y) + return x; + + for (int i = 15; i >= 0; i--) + { + if (lca[x][i] != lca[y][i]) + { + x = lca[x][i]; + y = lca[y][i]; + } + } + return lca[x][0]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y, i; + + for (i = 0; i < N - 1; i++) + { + cin >> x >> y; + edges[x].push_back(y); + edges[y].push_back(x); + } + + dfs(1, 1, 0); + buildlca(); + + cin >> T; + + for (i = 0; i < T; i++) + { + cin >> x >> y; + cout << getlca(x, y) << "\n"; + } + + return 0; +} From 5426e23688322a42f227468b6216316dd64448db Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 9 Feb 2021 21:43:10 +0900 Subject: [PATCH 044/203] =?UTF-8?q?=EC=A0=95=EC=A0=90=EB=93=A4=EC=9D=98=20?= =?UTF-8?q?=EA=B1=B0=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1761.cpp" | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 "baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/1761.cpp" diff --git "a/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/1761.cpp" "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/1761.cpp" new file mode 100644 index 0000000..29ebdb4 --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/1761.cpp" @@ -0,0 +1,103 @@ +#include +#include + +using namespace std; + +int N, M; +int depth[40001]; +vector > edges[40001]; +int lca[40001][18]; +int dist[40001][18]; +bool visited[40001]; + +void dfs(int n, int d, int pre, int predist) +{ + visited[n] = true; + depth[n] = d; + lca[n][0] = pre; + dist[n][0] = predist; + + for (pair v : edges[n]) + { + if (!visited[v.second]) + { + dfs(v.second, d + 1, n, v.first); + } + } +} + +void build_lca() +{ + int i, j; + for (j = 1; j < 18; j++) + { + for (i = 1; i <= N; i++) + { + lca[i][j] = lca[lca[i][j - 1]][j - 1]; + dist[i][j] = dist[i][j - 1] + dist[lca[i][j - 1]][j - 1]; + } + } +} + +int getlca(int x, int y) +{ + int ans = 0; + + if (depth[x] > depth[y]) + { + swap(x, y); + } + + for (int i = 17; i >= 0; i--) + { + if (depth[y] - depth[x] >= (1 << i)) + { + ans += dist[y][i]; + y = lca[y][i]; + } + } + + if (x == y) + return ans; + + for (int i = 17; i >= 0; i--) + { + if (lca[x][i] != lca[y][i]) + { + ans += dist[x][i] + dist[y][i]; + x = lca[x][i]; + y = lca[y][i]; + } + } + return ans + dist[x][0] + dist[y][0]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y, z, i; + + for (i = 0; i < N - 1; i++) + { + cin >> x >> y >> z; + edges[x].push_back({z, y}); + edges[y].push_back({z, x}); + } + + dfs(1, 1, 0, 0); + build_lca(); + + cin >> M; + + for (i = 0; i < M; i++) + { + cin >> x >> y; + cout << getlca(x, y) << "\n"; + } + + return 0; +} From 729572fa226f68549ff4d18728a6547bba56fcf1 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 10 Feb 2021 18:38:31 +0900 Subject: [PATCH 045/203] =?UTF-8?q?=EC=A0=84=ED=99=94=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5052.cpp" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "baekjoon/\355\212\270\353\235\274\354\235\264/5052.cpp" diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/5052.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/5052.cpp" new file mode 100644 index 0000000..1ea1866 --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/5052.cpp" @@ -0,0 +1,83 @@ +#include + +using namespace std; + +int T, N; + +class TrieNode +{ +public: + TrieNode *next[10]; + bool isEnd = false; +}; + +class Trie +{ +public: + TrieNode *root = new TrieNode(); + + bool insert(string word) + { + TrieNode *current = root; + for (int i = 0; i < word.length(); i++) + { + int index = word[i] - '0'; + if (current->next[index] == NULL) + { + current->next[index] = new TrieNode(); + } + else if (i == word.length() - 1) + { + current = current->next[index]; + current->isEnd = true; + return false; + } + + current = current->next[index]; + if (current->isEnd) + return false; + } + current->isEnd = true; + return true; + } +}; + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + + int i, j; + string str; + + cin >> T; + + for (i = 0; i < T; i++) + { + + Trie TrieTree; + + bool flag = true; + cin >> N; + for (j = 0; j < N; j++) + { + cin >> str; + if (!TrieTree.insert(str)) + { + flag = false; + } + } + + if (flag) + { + cout << "YES\n"; + } + else + { + cout << "NO\n"; + } + } + + return 0; +} From 75ed781ac780aef3dcd3a739d5b9a9567f8f0bd0 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 11 Feb 2021 20:43:11 +0900 Subject: [PATCH 046/203] =?UTF-8?q?=EC=A0=80=EC=9A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2437.cpp" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/2437.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2437.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2437.cpp" new file mode 100644 index 0000000..cae73ac --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2437.cpp" @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + +int N; +int A[1000]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int i; + for (i = 0; i < N; i++) + { + cin >> A[i]; + } + + sort(A, A + N); + + int sum = 0; + for (i = 0; i < N; i++) + { + if (sum + 1 < A[i]) + { + cout << sum + 1 << "\n"; + return 0; + } + sum += A[i]; + } + + cout << sum + 1 << "\n"; + + return 0; +} From 57a90d53d5e6cb1eb0577f2e61b7a9cd273b231c Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 11 Feb 2021 21:09:41 +0900 Subject: [PATCH 047/203] =?UTF-8?q?=EA=B3=BC=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../13904.cpp" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/13904.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/13904.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/13904.cpp" new file mode 100644 index 0000000..7aa853d --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/13904.cpp" @@ -0,0 +1,65 @@ +#include +#include + +using namespace std; + +struct node +{ + int due; + int score; + + bool operator<(const node &a) const + { + if (a.due == due) + return score < a.score; + return due > a.due; + } +}; + +int N; +priority_queue pq; +priority_queue doit; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y; + + for (int i = 0; i < N; i++) + { + cin >> x >> y; + pq.push({x, y}); + } + + int time = 1; + int ans = 0; + while (!pq.empty()) + { + int d = pq.top().due; + int s = pq.top().score; + pq.pop(); + + //cout << d << " " << s << " " << ans << "\n"; + + if (d >= time) + { + ans += s; + doit.push(-s); + time++; + } + else if (!doit.empty() && -doit.top() < s) + { + ans += doit.top(); + doit.pop(); + doit.push(-s); + ans += s; + } + } + + cout << ans << "\n"; + return 0; +} From 8b495e5a35de8979891e443138b8d6734e20e612 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 15 Feb 2021 12:15:23 +0900 Subject: [PATCH 048/203] Create 2146.cpp --- .../2146.cpp" | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2146.cpp" diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2146.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2146.cpp" new file mode 100644 index 0000000..3ce5fbe --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2146.cpp" @@ -0,0 +1,108 @@ +#include +#include + +using namespace std; + +int dist[100][100]; +int board[100][100]; +bool visited[100][100]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int N; + +struct bfs +{ + int x, y, c, turn; + + bool operator<(const bfs &a) const + { + return turn > a.turn; + } +}; + +priority_queue bfsQueue; + +void dfs(int i, int j, int n) +{ + visited[i][j] = true; + board[i][j] = n; + bfsQueue.push({i, j, n, 0}); + + for (int x = 0; x < 4; x++) + { + int nx = i + dx[x]; + int ny = j + dy[x]; + + if (nx >= 0 && ny >= 0 && nx < N && ny < N && !visited[nx][ny] && board[nx][ny] != 0) + { + dfs(nx, ny, n); + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int n = 1, i, j; + + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + cin >> board[i][j]; + } + } + + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + if (!visited[i][j] && board[i][j] != 0) + { + dfs(i, j, n); + n++; + } + } + } + + while (!bfsQueue.empty()) + { + int x = bfsQueue.top().x; + int y = bfsQueue.top().y; + int turn = bfsQueue.top().turn; + int c = bfsQueue.top().c; + bfsQueue.pop(); + + if (c == -1) + { + cout << turn << "\n"; + return 0; + } + + for (i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < N) + { + if (board[nx][ny] == 0 && !visited[nx][ny]) + { + board[nx][ny] = c; + dist[nx][ny] = turn + 1; + bfsQueue.push({nx, ny, c, turn + 1}); + visited[nx][ny] = true; + } + else if (board[nx][ny] != 0 && board[nx][ny] != c) + { + bfsQueue.push({nx, ny, -1, turn + dist[nx][ny]}); + } + } + } + } + + return 0; +} From 69e09cbbf7b0a59e3077a88554515d63477f0d77 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 15 Feb 2021 12:35:46 +0900 Subject: [PATCH 049/203] =?UTF-8?q?=EC=97=AD=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1613.cpp" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1613.cpp" diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1613.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1613.cpp" new file mode 100644 index 0000000..db1734c --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1613.cpp" @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define INF 1600000 + +int N, K, S; +int d[401][401]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N >> K; + + int i, j, x, y; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + d[i][j] = INF; + } + d[i][i] = 0; + } + + for (i = 0; i < K; i++) + { + cin >> x >> y; + d[x][y] = 1; + } + + for (x = 1; x <= N; x++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (d[i][j] > d[i][x] + d[x][j]) + { + d[i][j] = d[i][x] + d[x][j]; + } + } + } + } + + cin >> S; + + for (i = 0; i < S; i++) + { + cin >> x >> y; + if (d[x][y] < INF) + { + cout << "-1\n"; + } + else if (d[y][x] < INF) + { + cout << "1\n"; + } + else + { + cout << "0\n"; + } + } + + return 0; +} From 8ed327d3d6956c73055f59b6091025c3a29bd870 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 15 Feb 2021 12:49:06 +0900 Subject: [PATCH 050/203] =?UTF-8?q?=ED=94=8C=EB=A1=9C=EC=9D=B4=EB=93=9C=20?= =?UTF-8?q?=EC=99=80=EC=83=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../10159.cpp" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/10159.cpp" diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/10159.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/10159.cpp" new file mode 100644 index 0000000..058f95a --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/10159.cpp" @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define INF 10001 + +int N, M; +int d[101][101]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + int i, j, x, y; + + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + d[i][j] = INF; + } + d[i][i] = 0; + } + + for (i = 0; i < M; i++) + { + cin >> x >> y; + d[x][y] = 1; + } + + for (x = 1; x <= N; x++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (d[i][j] > d[i][x] + d[x][j]) + { + d[i][j] = d[i][x] + d[x][j]; + } + } + } + } + + for (i = 1; i <= N; i++) + { + int count = 0; + for (j = 1; j <= N; j++) + { + if (d[i][j] == INF && d[j][i] == INF) + { + count++; + } + } + cout << count << "\n"; + } + return 0; +} From 2e73f8a50e3022e88161f6ca412bcb09e7fb42eb Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 16 Feb 2021 12:28:52 +0900 Subject: [PATCH 051/203] =?UTF-8?q?=ED=9A=8C=EC=9E=A5=EB=BD=91=EA=B8=B0=20?= =?UTF-8?q?(=ED=94=8C=EB=A1=9C=EC=9D=B4=EB=93=9C=20=EC=99=80=EC=83=AC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2660.cpp" | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2660.cpp" diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2660.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2660.cpp" new file mode 100644 index 0000000..ef0d451 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/2660.cpp" @@ -0,0 +1,85 @@ +#include +#include + +using namespace std; + +#define INF 2500 + +int N; +int d[51][51]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y; + + for (x = 1; x <= N; x++) + { + for (y = 1; y <= N; y++) + { + d[x][y] = INF; + } + d[x][x] = 0; + } + + while (true) + { + cin >> x >> y; + if (x == -1 && y == -1) + { + break; + } + + d[x][y] = 1; + d[y][x] = 1; + } + + for (int k = 1; k <= N; k++) + { + for (x = 1; x <= N; x++) + { + for (y = 1; y <= N; y++) + { + if (d[x][y] > d[x][k] + d[k][y]) + { + d[x][y] = d[x][k] + d[k][y]; + } + } + } + } + + int minscore = INF; + vector ans; + + for (x = 1; x <= N; x++) + { + int nowmax = 0; + for (y = 1; y <= N; y++) + { + nowmax = max(d[x][y], nowmax); + } + if (nowmax < minscore) + { + minscore = nowmax; + ans.clear(); + ans.push_back(x); + } + else if (nowmax == minscore) + { + ans.push_back(x); + } + } + + cout << minscore << " " << ans.size() << "\n"; + for (int a : ans) + { + cout << a << " "; + } + cout << "\n"; + + return 0; +} From fef1337a65058a559544b35e878240edc26b93f0 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 16 Feb 2021 13:10:05 +0900 Subject: [PATCH 052/203] =?UTF-8?q?=EA=B6=81=EA=B8=88=ED=95=9C=20=EB=AF=BC?= =?UTF-8?q?=ED=98=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1507.cpp" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1507.cpp" diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1507.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1507.cpp" new file mode 100644 index 0000000..d9503f2 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1507.cpp" @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int N; +int graph[20][20]; +bool exclude[20][20]; +int result; + +void floyd() +{ + for (int k = 0; k < N; k++) + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + + if (i == j || j == k || i == k) + { + continue; + } + else if (graph[i][j] > graph[i][k] + graph[k][j]) + { + result = -1; + return; + } + else if (graph[i][j] == graph[i][k] + graph[k][j]) + { + exclude[i][j] = true; + } + } +} + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + cin >> graph[i][j]; + + floyd(); + + if (result != -1) + { + + for (int i = 0; i < N; i++) + + for (int j = 0; j < N; j++) + + if (!exclude[i][j]) + result += graph[i][j]; + + cout << result / 2 << "\n"; + } + else + { + cout << -1 << "\n"; + } + + return 0; +} From 9d0f20017c989ac8bc791d92433692abcbd6746c Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 19 Feb 2021 11:59:19 +0900 Subject: [PATCH 053/203] =?UTF-8?q?=EC=83=9D=ED=83=9C=ED=95=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../4358.cpp" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "baekjoon/\355\212\270\353\235\274\354\235\264/4358.cpp" diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/4358.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/4358.cpp" new file mode 100644 index 0000000..81e9f13 --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/4358.cpp" @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +using namespace std; + +map trees; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + string str; + int total = 0; + + while (getline(cin, str)) + { + if (trees.count(str) == 0) + { + trees[str] = 1; + } + else + { + trees[str]++; + } + total++; + } + + vector > toVector(trees.begin(), trees.end()); + + sort(toVector.begin(), toVector.end()); + for (pair v : toVector) + { + printf("%s %.4f\n", v.first.c_str(), (float)v.second * 100 / total); + } + + return 0; +} From 9aca830bd26d17248d7bee0db642f39a25df425b Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 19 Feb 2021 12:50:54 +0900 Subject: [PATCH 054/203] =?UTF-8?q?=EA=B2=8C=EC=9E=84=20=EB=8B=89=EB=84=A4?= =?UTF-8?q?=EC=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16934.cpp" | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 "baekjoon/\355\212\270\353\235\274\354\235\264/16934.cpp" diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/16934.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/16934.cpp" new file mode 100644 index 0000000..a4c47dd --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/16934.cpp" @@ -0,0 +1,77 @@ +#include + +using namespace std; + +class Tree +{ +public: + Tree *next[26] = {NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL}; + int count = 0; +}; + +class TrieTree +{ +public: + Tree *root = new Tree(); + + void insert(string str) + { + bool first = true; + string ans = ""; + Tree *current = root; + for (int i = 0; i < str.length(); i++) + { + if (current->next[str[i] - 'a'] == NULL) + { + current->next[str[i] - 'a'] = new Tree(); + if (first) + { + ans = str.substr(0, i + 1); + first = false; + } + } + current = current->next[str[i] - 'a']; + } + + current->count++; + + if (first) + { + if (current->count == 1) + { + cout << str << "\n"; + } + else + { + cout << str << current->count << "\n"; + } + } + else + { + cout << ans << "\n"; + } + } +}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int N; + string tmp; + TrieTree trieTree; + + cin >> N; + for (int i = 0; i < N; i++) + { + cin >> tmp; + trieTree.insert(tmp); + } + + return 0; +} From bf6fba99acde63d1bab74cdb3fa6b064bce9c694 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 20 Feb 2021 12:52:38 +0900 Subject: [PATCH 055/203] =?UTF-8?q?=ED=9C=B4=EB=8C=80=ED=8F=B0=20=EC=9E=90?= =?UTF-8?q?=ED=8C=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5670.cpp" | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 "baekjoon/\355\212\270\353\235\274\354\235\264/5670.cpp" diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/5670.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/5670.cpp" new file mode 100644 index 0000000..b01a540 --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/5670.cpp" @@ -0,0 +1,97 @@ +#include + +using namespace std; + +int N; + +class Tree +{ + +public: + bool isEnd = false; + Tree *next[26]; + int nextcount = 0; + int count = 0; + + ~Tree() + { + for (int i = 0; i < 26; i++) + if (next[i] != NULL) + delete next[i]; + } +}; + +class TrieTree +{ + +public: + Tree *root = new Tree(); + + void insert(string str) + { + Tree *current = root; + + for (int i = 0; i < str.length(); i++) + { + int index = str[i] - 'a'; + if (current->next[index] == NULL) + { + current->next[index] = new Tree(); + current->nextcount++; + } + current = current->next[index]; + current->count++; + } + + current->isEnd = true; + } + + int dfs(Tree *current) + { + int res = 0; + for (int i = 0; i < 26; i++) + { + if (current->next[i] != NULL) + { + if (current->isEnd || current->nextcount != 1 || current == root) + { + res += current->next[i]->count; + } + res += dfs(current->next[i]); + } + } + return res; + } + + int ans() + { + int res = dfs(root); + return res; + } + + ~TrieTree() + { + delete root; + } +}; +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + + while (cin >> N) + { + TrieTree trieTree; + for (int i = 0; i < N; i++) + { + string str; + cin >> str; + trieTree.insert(str); + } + + printf("%.2f\n", (double)trieTree.ans() / N); + } + + return 0; +} From a23a3244b4e051c5da34aade1b9b4552fafbb810 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 20 Feb 2021 22:00:20 +0900 Subject: [PATCH 056/203] =?UTF-8?q?=EC=A4=84=EC=84=B8=EC=9A=B0=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2631.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 baekjoon/DP/2631.cpp diff --git a/baekjoon/DP/2631.cpp b/baekjoon/DP/2631.cpp new file mode 100644 index 0000000..ff4aa8f --- /dev/null +++ b/baekjoon/DP/2631.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +using namespace std; + +int N; +int A[200]; +vector lis; + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int i; + + lis.push_back(-1); + + for (i = 0; i < N; i++) + { + cin >> A[i]; + + if (lis.back() <= A[i]) + { + lis.push_back(A[i]); + } + else + { + vector::iterator it = lower_bound(lis.begin(), lis.end(), A[i]); + *it = A[i]; + } + } + + cout << N - (lis.size() - 1) << "\n"; + return 0; +} From 86740f4c159489c36298e75997f18bfa839361c6 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 22 Feb 2021 20:07:29 +0900 Subject: [PATCH 057/203] =?UTF-8?q?(=ED=91=B8=EB=8A=94=20=EC=A4=91)=20?= =?UTF-8?q?=EB=A1=9C=EB=B4=87=EC=B2=AD=EC=86=8C=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/14503.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 baekjoon/14503.cpp diff --git a/baekjoon/14503.cpp b/baekjoon/14503.cpp new file mode 100644 index 0000000..ea06bcc --- /dev/null +++ b/baekjoon/14503.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int N, M; +int R, C, dir; +bool visited[50][50]; +bool wall[50][50]; + +int dx[8] = {0, 1, 0, -1, 0, 1, 0, -1}; +int dy[8] = {-1, 0, 1, 0, -1, 0, 1, 0}; +int bx[4] = {1, 0, -1, 0}; +int by[4] = {0, 1, 0, -1}; + +int dfs(int x, int y, int d) +{ + int res = 0; + + if (!visited[x][y]) + res += 1; + + visited[x][y] = true; + + cout << x << " " << y << " " << d << "\n"; + + for (int i = d; i < d + 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!visited[nx][ny] && !wall[nx][ny]) + { + res += dfs(nx, ny, (i + 1) % 4); + break; + } + } + } + + if (res == 1) + { + int nx = x + bx[d]; + int ny = y + by[d]; + cout << x << "/" << y << "/" << nx << "/" << ny << "\n"; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && !wall[nx][ny]) + { + res += dfs(nx, ny, d); + } + } + + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + cin >> R >> C >> dir; + + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + int t; + cin >> t; + if (t == 1) + wall[i][j] = true; + } + } + + cout << dfs(R, C, dir) << "\n"; + + return 0; +} From b767b2876030733d5e147f5d17bd80cc6c7a9f6d Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 23 Feb 2021 13:23:56 +0900 Subject: [PATCH 058/203] =?UTF-8?q?=EC=9D=B8=EC=A0=91=ED=95=9C=20=EB=B9=84?= =?UTF-8?q?=ED=8A=B8=EC=9D=98=20=EA=B0=9C=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2698.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 baekjoon/DP/2698.cpp diff --git a/baekjoon/DP/2698.cpp b/baekjoon/DP/2698.cpp new file mode 100644 index 0000000..2bf6230 --- /dev/null +++ b/baekjoon/DP/2698.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; +int T, N, K; +int D[101][101][2]; + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + + int t; + + cin >> T; + int i, j; + for (t = 0; t < T; t++) + { + cin >> N >> K; + for (i = 0; i <= N; i++) + { + for (j = 0; j <= K; j++) + { + D[i][j][0] = 0; + D[i][j][1] = 0; + } + } + + D[1][0][0] = 1; + D[1][0][1] = 1; + + for (i = 2; i <= N; i++) + { + D[i][0][1] = D[i - 1][0][0]; + D[i][0][0] = D[i - 1][0][0] + D[i - 1][0][1]; + } + + for (i = 2; i <= N; i++) + { + for (j = 1; j <= K; j++) + { + D[i][j][0] = D[i - 1][j][0] + D[i - 1][j][1]; + D[i][j][1] = D[i - 1][j - 1][1] + D[i - 1][j][0]; + } + } + + cout << D[N][K][0] + D[N][K][1] << "\n"; + } + + return 0; +} From 5434cc104649ac61b03d71f4b9975b12d2ca87ab Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 26 Feb 2021 15:31:16 +0900 Subject: [PATCH 059/203] =?UTF-8?q?=EC=A4=84=EC=96=B4=EB=93=A4=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EC=95=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2688.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 baekjoon/DP/2688.cpp diff --git a/baekjoon/DP/2688.cpp b/baekjoon/DP/2688.cpp new file mode 100644 index 0000000..59465b7 --- /dev/null +++ b/baekjoon/DP/2688.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +long long D[65][10]; +int T, N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + + for (int t = 0; t < T; t++) + { + cin >> N; + + for (int i = 0; i <= N; i++) + { + for (int j = 0; j < 10; j++) + { + D[i][j] = 0; + } + } + for (int j = 0; j < 10; j++) + { + D[1][j] = 1; + } + + for (int i = 2; i <= N; i++) + { + for (int j = 0; j < 10; j++) + { + for (int k = 0; k <= j; k++) + { + D[i][j] += D[i - 1][k]; + } + } + } + + long long ans = 0; + for (int j = 0; j < 10; j++) + { + ans += D[N][j]; + } + cout << ans << "\n"; + } + return 0; +} From da68b39eb15f92bcfa26d88c07908473ef06df09 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 26 Feb 2021 22:59:16 +0900 Subject: [PATCH 060/203] =?UTF-8?q?=EC=95=8C=EC=95=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/4811.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 baekjoon/DP/4811.cpp diff --git a/baekjoon/DP/4811.cpp b/baekjoon/DP/4811.cpp new file mode 100644 index 0000000..75491f1 --- /dev/null +++ b/baekjoon/DP/4811.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int T, N; +long long D[31][31]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + while (true) + { + + cin >> N; + + if (N == 0) + break; + + for (int i = 0; i <= N; i++) + { + for (int j = 0; j <= N; j++) + { + D[i][j] = 0; + } + } + + D[0][N] = 1; + + for (int j = N - 1; j >= 0; j--) + { + for (int i = N; i >= 0; i--) + { + if (i - 1 >= 0) + { + D[i][j] += D[i - 1][j + 1]; + } + if (i + 1 <= N) + { + D[i][j] += D[i + 1][j]; + } + } + } + + cout << D[0][0] << "\n"; + } + + return 0; +} From afd3b7ed0a7360b2fe98cf2f7513804b675ca967 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 1 Mar 2021 16:39:26 +0900 Subject: [PATCH 061/203] =?UTF-8?q?=EB=B6=80=EB=B6=84=20=EB=B0=B0=EC=97=B4?= =?UTF-8?q?=20=EA=B3=A0=EB=A5=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2014.cpp" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 "baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/2014.cpp" diff --git "a/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/2014.cpp" "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/2014.cpp" new file mode 100644 index 0000000..7ab2e25 --- /dev/null +++ "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/2014.cpp" @@ -0,0 +1,98 @@ +#include +#include + +using namespace std; + +int N; +long long A[100001]; +int seg[400000]; +long long sum[100001]; + +int init(int node, int start, int end) +{ + if (start > N) + return -1; + if (start == end) + return seg[node] = start; + + int middle = (start + end) / 2; + + int lefti = init(node * 2, start, middle); + int righti = init(node * 2 + 1, middle + 1, end); + if (lefti == -1) + seg[node] = righti; + else if (righti == -1) + seg[node] = lefti; + else if (A[lefti] < A[righti]) + seg[node] = lefti; + else + seg[node] = righti; + return seg[node]; +} + +int get_min_index(int node, int left, int right, int start, int end) +{ + + if (left > end || right < start) + return -1; + + if (left <= start && right >= end) + return seg[node]; + + int middle = (start + end) / 2; + + int lefti = get_min_index(node * 2, left, right, start, middle); + int righti = get_min_index(node * 2 + 1, left, right, middle + 1, end); + + if (lefti == -1) + return righti; + if (righti == -1) + return lefti; + if (A[lefti] < A[righti]) + return lefti; + return righti; +} + +long long solve(int left, int right) +{ + int index = get_min_index(1, left, right, 1, pow(2, ceil(log2(N)))); + long long ans = sum[right] - sum[left - 1]; + ans = ans * A[index]; + + if (index - 1 >= left) + { + ans = max(ans, solve(left, index - 1)); + } + if (index + 1 <= right) + { + ans = max(ans, solve(index + 1, right)); + } + + //cout << left << " " << right << " " << ans << "\n"; + return ans; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int i; + for (i = 1; i <= N; i++) + { + cin >> A[i]; + } + + for (i = 1; i <= N; i++) + { + sum[i] = sum[i - 1] + A[i]; + } + + init(1, 1, pow(2, ceil(log2(N)))); + + cout << solve(1, N) << "\n"; + + return 0; +} From 60975ad3470aaeb737c802747762e62f2ab55362 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 1 Mar 2021 20:16:47 +0900 Subject: [PATCH 062/203] =?UTF-8?q?LCA=EC=99=80=20=EC=BF=BC=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../15480.cpp" | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 "baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/15480.cpp" diff --git "a/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/15480.cpp" "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/15480.cpp" new file mode 100644 index 0000000..fe349f9 --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\352\263\265\355\206\265\354\241\260\354\203\201/15480.cpp" @@ -0,0 +1,106 @@ +#include +#include + +using namespace std; + +int N, M; + +vector edges[100001]; +bool visited[100001]; +int depth[100001]; +int lca[100001][18]; + +void dfs(int n, int d, int p) +{ + visited[n] = true; + depth[n] = d; + lca[n][0] = p; + + for (int v : edges[n]) + { + if (!visited[v]) + { + dfs(v, d + 1, n); + } + } +} + +void build_lca() +{ + for (int j = 1; j < 18; j++) + { + for (int i = 1; i <= N; i++) + { + lca[i][j] = lca[lca[i][j - 1]][j - 1]; + } + } +} + +int find_lca(int x, int y) +{ + if (depth[x] > depth[y]) + { + swap(x, y); + } + + for (int i = 17; i >= 0; i--) + { + if (depth[y] >= depth[x] + (1 << i)) + { + y = lca[y][i]; + } + } + + if (x == y) + return x; + + for (int i = 17; i >= 0; i--) + { + if (lca[x][i] != lca[y][i]) + { + x = lca[x][i]; + y = lca[y][i]; + } + } + return lca[x][0]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N; + + int i; + int x, y, z; + for (i = 0; i < N - 1; i++) + { + cin >> x >> y; + edges[x].push_back(y); + edges[y].push_back(x); + } + + dfs(1, 0, 0); + build_lca(); + + cin >> M; + for (i = 0; i < M; i++) + { + cin >> x >> y >> z; + + int lca1 = find_lca(x, y); + int lca2 = find_lca(x, z); + int lca3 = find_lca(y, z); + + int ans = lca1; + if (depth[lca2] > depth[ans]) + ans = lca2; + if (depth[lca3] > depth[ans]) + ans = lca3; + + cout << ans << "\n"; + } + return 0; +} From b17a6c7d0351128320fe5bf2e4d48750790cb5b9 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 2 Mar 2021 22:36:27 +0900 Subject: [PATCH 063/203] =?UTF-8?q?=EB=AC=B8=EC=A0=9C=EC=A7=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1766.cpp" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 "baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/1766.cpp" diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/1766.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/1766.cpp" new file mode 100644 index 0000000..482306a --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/1766.cpp" @@ -0,0 +1,60 @@ +#include +#include +#include + +using namespace std; + +int N, M; +priority_queue pq; +vector edges[32001]; +int degree[32001]; +bool visited[32001]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N >> M; + int x, y; + + for (int i = 0; i < M; i++) + { + cin >> x >> y; + edges[x].push_back(y); + degree[y]++; + } + + int selected = 0; + + for (int i = 1; i <= N; i++) + { + if (!visited[i] && degree[i] == 0) + { + pq.push(-i); + visited[i] = true; + } + } + + while (selected < N) + { + int t = -pq.top(); + pq.pop(); + + cout << t << " "; + selected++; + + for (int v : edges[t]) + { + degree[v]--; + if (!visited[v] && degree[v] == 0) + { + pq.push(-v); + visited[v] = true; + } + } + } + + return 0; +} From 52f230161096e046f4332657b5dc832f231b4356 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 2 Mar 2021 22:48:29 +0900 Subject: [PATCH 064/203] =?UTF-8?q?=EC=9D=8C=EC=95=85=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EB=9E=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2623.cpp" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 "baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2623.cpp" diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2623.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2623.cpp" new file mode 100644 index 0000000..ec672af --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2623.cpp" @@ -0,0 +1,78 @@ +#include +#include +#include + +using namespace std; + +int N, M; +vector edges[1001]; +int degree[1001]; +queue q; +bool visited[1001]; +vector ans; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + int x, y, z; + for (int i = 0; i < M; i++) + { + cin >> x >> y; + for (int j = 0; j < x - 1; j++) + { + cin >> z; + edges[y].push_back(z); + degree[z]++; + y = z; + } + } + + for (int i = 1; i <= N; i++) + { + if (degree[i] == 0) + { + q.push(i); + visited[i] = true; + } + } + + int selected = 0; + while (!q.empty()) + { + x = q.front(); + q.pop(); + + selected += 1; + ans.push_back(x); + + for (int v : edges[x]) + { + if (!visited[v]) + { + degree[v]--; + if (degree[v] == 0) + { + q.push(v); + visited[v] = true; + } + } + } + } + + if (selected < N) + { + cout << "0\n"; + } + else + { + for (int a : ans) + { + cout << a << "\n"; + } + } + + return 0; +} From 6b83f0e09511da2684a26112d9d1c90672bdb17a Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 2 Mar 2021 23:00:49 +0900 Subject: [PATCH 065/203] =?UTF-8?q?=EC=84=A0=EC=88=98=20=EA=B3=BC=EB=AA=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14567.cpp" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 "baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/14567.cpp" diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/14567.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/14567.cpp" new file mode 100644 index 0000000..6697ac0 --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/14567.cpp" @@ -0,0 +1,65 @@ +#include +#include +#include + +using namespace std; + +int N, M; +int degree[1001]; +int ans[1001]; +vector edges[1001]; +queue q; +bool visited[1001]; + +int main() +{ + ios::sync_with_stdio(0); + + cin >> N >> M; + + int x, y; + for (int i = 0; i < M; i++) + { + cin >> x >> y; + edges[x].push_back(y); + degree[y]++; + } + + int time = 1; + for (int i = 1; i <= N; i++) + { + if (degree[i] == 0) + { + q.push(i); + visited[i] = true; + ans[i] = time; + } + } + + while (!q.empty()) + { + x = q.front(); + q.pop(); + + for (int v : edges[x]) + { + if (!visited[v]) + { + degree[v]--; + if (degree[v] == 0) + { + visited[v] = true; + q.push(v); + ans[v] = ans[x] + 1; + } + } + } + } + + for (int i = 1; i <= N; i++) + { + cout << ans[i] << " "; + } + + return 0; +} From 5c25d3ef7ce7b7503eca848c358bbf1bd31bd3fc Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 2 Mar 2021 23:25:36 +0900 Subject: [PATCH 066/203] =?UTF-8?q?=EC=9E=A5=EB=82=9C=EA=B0=90=20=EC=A1=B0?= =?UTF-8?q?=EB=A6=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2637.cpp" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2637.cpp" diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2637.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2637.cpp" new file mode 100644 index 0000000..0b448f2 --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2637.cpp" @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +int degree[101]; +vector > edges[101]; +queue q; +bool visited[101]; +int ans[101]; +int N, M; + +int main() +{ + ios::sync_with_stdio(0); + + cin >> N >> M; + int x, y, z; + + for (int i = 0; i < M; i++) + { + cin >> x >> y >> z; + degree[y]++; + edges[x].push_back({z, y}); + } + + ans[N] = 1; + visited[N] = true; + q.push(N); + + while (!q.empty()) + { + x = q.front(); + q.pop(); + + if (edges[x].empty()) + continue; + + for (pair vertex : edges[x]) + { + int c = vertex.first; + int v = vertex.second; + if (!visited[v]) + { + degree[v]--; + if (degree[v] == 0) + { + visited[v] = true; + q.push(v); + } + ans[v] += ans[x] * c; + } + } + + ans[x] = 0; + } + + for (int i = 1; i <= N; i++) + { + if (ans[i] != 0) + { + cout << i << " " << ans[i] << "\n"; + } + } + + return 0; +} From 9fe9f7ff3c3e69ad707882e512876ce05439bd13 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 6 Mar 2021 19:00:27 +0900 Subject: [PATCH 067/203] =?UTF-8?q?=EC=B9=9C=EA=B5=AC=EB=B9=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16562.cpp" | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 "baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/16562.cpp" diff --git "a/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/16562.cpp" "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/16562.cpp" new file mode 100644 index 0000000..6efb3c7 --- /dev/null +++ "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/16562.cpp" @@ -0,0 +1,101 @@ +#include +#include + +using namespace std; + +typedef struct node +{ + int n; + int c; + + bool operator<(const node &a) const + { + return c > a.c; + } +} node; + +int N, M, K; +int P[10001]; +int A[10001]; +priority_queue pq; + +int findP(int n) +{ + if (P[n] == n) + { + return n; + } + + if (P[P[n]] == P[n]) + { + return P[n]; + } + + A[P[n]] += A[n]; + + return findP(P[n]); +} + +void Union(int x, int y) +{ + x = findP(x); + y = findP(y); + + if (x == y) + return; + + if (x < y) + { + swap(x, y); + } + + A[y] += A[P[x]]; + P[P[x]] = P[y]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int x, y; + + cin >> N >> M >> K; + for (int i = 1; i <= N; i++) + { + cin >> x; + pq.push({i, x}); + P[i] = i; + A[i] = 1; + } + + for (int i = 0; i < M; i++) + { + cin >> x >> y; + Union(x, y); + } + + int ans = 0; + + while (A[0] < N && ans < K && !pq.empty()) + { + node top = pq.top(); + pq.pop(); + if (findP(top.n) != 0) + { + Union(0, top.n); + ans += top.c; + } + } + + if (A[0] != N || ans > K) + { + cout << "Oh no\n"; + } + else + { + cout << ans << "\n"; + } + + return 0; +} From e5759f1bf19337e82c179e0f933736dbcd1a82d2 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 6 Mar 2021 20:44:31 +0900 Subject: [PATCH 068/203] =?UTF-8?q?=EB=A9=80=EC=A9=A1=ED=95=9C=20=EC=82=AC?= =?UTF-8?q?=EA=B0=81=ED=98=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- programmers/030601.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 programmers/030601.cpp diff --git a/programmers/030601.cpp b/programmers/030601.cpp new file mode 100644 index 0000000..1e442c8 --- /dev/null +++ b/programmers/030601.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; + +int gcd(int a, int b){ + if(a < b) swap(a, b); + + while(b!=0){ + int tmp = a%b; + a = b; + b = tmp; + } + return a; +} + +long long solution(int w, int h) { + long long answer = 1; + + answer = (long long)w*h - (w+h-gcd(w,h)); + + return answer; +} From 0cdd1f75d8fd251f369693f87b8290b711f1b84f Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 7 Mar 2021 21:03:17 +0900 Subject: [PATCH 069/203] =?UTF-8?q?=EC=82=AC=EC=9D=B4=ED=81=B4=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20040.cpp" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/20040.cpp" diff --git "a/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/20040.cpp" "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/20040.cpp" new file mode 100644 index 0000000..e0eb027 --- /dev/null +++ "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/20040.cpp" @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int N, M; +int p[500000]; + +int getp(int n) +{ + if (p[n] == n) + return n; + return p[n] = getp(p[n]); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + int x, y; + bool flag = false; + int ans = 0; + for (int i = 0; i < N; i++) + { + p[i] = i; + } + for (int i = 0; i < M; i++) + { + cin >> x >> y; + if (getp(x) != getp(y)) + { + p[p[x]] = p[y]; + } + else if (!flag) + { + flag = true; + ans = i + 1; + } + } + + cout << ans << "\n"; + + return 0; +} From 9298c132660c452a1d7a4e3a280f7221f5252b0a Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 7 Mar 2021 21:44:52 +0900 Subject: [PATCH 070/203] =?UTF-8?q?=EB=84=A4=ED=8A=B8=EC=9B=8C=ED=81=AC=20?= =?UTF-8?q?=EC=97=B0=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../3780.cpp" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/3780.cpp" diff --git "a/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/3780.cpp" "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/3780.cpp" new file mode 100644 index 0000000..da25290 --- /dev/null +++ "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/3780.cpp" @@ -0,0 +1,63 @@ +#include +#include + +using namespace std; + +int T, N; +int p[20001]; +int dist[20001]; + +int getP(int x) +{ + if (p[x] == x) + return x; + + int t = getP(p[x]); + dist[x] += dist[p[x]]; + return p[x] = t; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> T; + char c; + int x, y; + for (int t = 0; t < T; t++) + { + cin >> N; + for (int i = 1; i <= N; i++) + { + dist[i] = 0; + p[i] = i; + } + + while (true) + { + cin >> c; + if (c == 'O') + break; + + if (c == 'E') + { + cin >> x; + getP(x); + cout << dist[x] << "\n"; + } + else if (c == 'I') + { + cin >> x >> y; + getP(x); + getP(y); + + dist[x] += abs(x - y) % 1000; + p[x] = y; + } + } + } + + return 0; +} From f15d47622539a81416d1428a4c19e9e2be72276e Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 8 Mar 2021 12:08:43 +0900 Subject: [PATCH 071/203] =?UTF-8?q?=EB=AF=B8=EB=A1=9C=20=ED=83=88=EC=B6=9C?= =?UTF-8?q?=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../17090.cpp" | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 "baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/17090.cpp" diff --git "a/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/17090.cpp" "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/17090.cpp" new file mode 100644 index 0000000..aa8bfda --- /dev/null +++ "b/baekjoon/\353\266\204\353\246\254\354\247\221\355\225\251/17090.cpp" @@ -0,0 +1,103 @@ +#include + +using namespace std; +int N, M; +string board[500]; +pair p[501][501]; +int sum[501][501]; + +pair getp(int i, int j) +{ + if (p[i][j] == make_pair(i, j)) + return make_pair(i, j); + + if (p[p[i][j].first][p[i][j].second] == p[i][j]) + return p[i][j]; + + sum[p[i][j].first][p[i][j].second] += sum[i][j]; + + return p[i][j] = getp(p[i][j].first, p[i][j].second); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + for (int i = 0; i < N; i++) + { + cin >> board[i]; + for (int j = 0; j < M; j++) + { + p[i][j] = make_pair(i, j); + sum[i][j] = 1; + } + } + + p[N][M] = make_pair(N, M); + + for (int x = 0; x < N; x++) + { + for (int y = 0; y < M; y++) + { + + int nx, ny; + if (board[x][y] == 'D') + { + nx = x + 1; + ny = y; + } + else if (board[x][y] == 'U') + { + nx = x - 1; + ny = y; + } + else if (board[x][y] == 'R') + { + nx = x; + ny = y + 1; + } + else + { + nx = x; + ny = y - 1; + } + + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (getp(x, y) != getp(nx, ny)) + { + + if (getp(x, y) == make_pair(N, M)) + { + sum[N][M] += sum[p[nx][ny].first][p[nx][ny].second]; + p[p[nx][ny].first][p[nx][ny].second] = make_pair(N, M); + } + else if (getp(nx, ny) == make_pair(N, M)) + { + sum[N][M] += sum[p[x][y].first][p[x][y].second]; + p[p[x][y].first][p[x][y].second] = make_pair(N, M); + } + else + { + sum[p[nx][ny].first][p[nx][ny].second] += sum[p[x][y].first][p[x][y].second]; + p[p[x][y].first][p[x][y].second] = make_pair(p[nx][ny].first, p[nx][ny].second); + } + } + } + else + { + if (getp(x, y) != make_pair(N, M)) + { + sum[N][M] += sum[p[x][y].first][p[x][y].second]; + p[p[x][y].first][p[x][y].second] = make_pair(N, M); + } + } + } + } + + cout << sum[N][M] << "\n"; + return 0; +} From 2a879c6491889f99266fb3130d4fcf8570364132 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 8 Mar 2021 18:57:13 +0900 Subject: [PATCH 072/203] =?UTF-8?q?=ED=95=A9=EC=8A=B9=20=ED=83=9D=EC=8B=9C?= =?UTF-8?q?=20=EC=9A=94=EA=B8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- programmers/030801.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 programmers/030801.cpp diff --git a/programmers/030801.cpp b/programmers/030801.cpp new file mode 100644 index 0000000..62481fc --- /dev/null +++ b/programmers/030801.cpp @@ -0,0 +1,42 @@ +#include +#include + +using namespace std; + +#define INF 2100000000 + +int dist[201][201]; + +int solution(int n, int s, int a, int b, vector> fares) { + int answer = 0; + + for(int i=1; i<= n; i++){ + for(int j=1; j<=n; j++){ + dist[i][j] = INF; + } + dist[i][i] = 0; + } + + for(vector fare: fares){ + dist[fare[0]][fare[1]]=fare[2]; + dist[fare[1]][fare[0]]=fare[2]; + } + + for(int k=1; k<=n; k++){ + for(int i=1; i<=n; i++){ + for(int j=1; j<=n; j++){ + if((long long)dist[i][j] > (long long)dist[i][k] + dist[k][j]){ + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + } + + answer = dist[s][a] + dist[s][b]; + + for(int i=1; i<=n; i++){ + answer = min(answer, dist[s][i] + dist[i][a] + dist[i][b]); + } + + return answer; +} From c3895e2689404e45fa78913c291182625461ad81 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 10 Mar 2021 17:53:33 +0900 Subject: [PATCH 073/203] =?UTF-8?q?=EC=B1=85=20=EB=82=98=EB=88=A0=EC=A3=BC?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../9576.cpp" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/9576.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/9576.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/9576.cpp" new file mode 100644 index 0000000..e9ff2f1 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/9576.cpp" @@ -0,0 +1,66 @@ +#include +#include + +using namespace std; + +typedef struct node +{ + int start, end; + + bool operator<(const node &a) const + { + if (end == a.end) + { + return start > a.start; + } + + return end > a.end; + } +} node; + +int T, N, M; +bool check[1001]; + +int main() +{ + ios::sync_with_stdio(0); + cin >> T; + + int x, y; + for (int t = 0; t < T; t++) + { + priority_queue pq; + cin >> N >> M; + for (int i = 0; i <= N; i++) + { + check[i] = false; + } + + for (int i = 0; i < M; i++) + { + cin >> x >> y; + pq.push({x, y}); + } + + int answer = 0; + while (!pq.empty()) + { + x = pq.top().start; + y = pq.top().end; + pq.pop(); + + for (int i = x; i <= y; i++) + { + if (!check[i]) + { + check[i] = true; + answer++; + break; + } + } + } + cout << answer << "\n"; + } + + return 0; +} From 6028b4e0d33c459760ab43c113a6c228e81ad112 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 12 Mar 2021 23:51:29 +0900 Subject: [PATCH 074/203] =?UTF-8?q?=ED=83=9D=EB=B0=B0(=ED=91=B8=EB=8A=94?= =?UTF-8?q?=EC=A4=91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../8980.cpp" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" new file mode 100644 index 0000000..a28d03a --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" @@ -0,0 +1,65 @@ +#include +#include + +using namespace std; + +typedef struct node +{ + int end, cost; + + bool operator<(const node &a) const + { + if (end == a.end) + return cost < a.cost; + return end > a.end; + } +} node; + +int N, C, M; +priority_queue pq[2001]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> C >> M; + + int x, y, z; + + for (int i = 0; i < M; i++) + { + cin >> x >> y >> z; + pq[x].push({y, z}); + } + + int ans = 0; + int now = 0; + + priority_queue queue; + + for (int i = 1; i <= N; i++) + { + while (!queue.empty() && queue.top().end == i) + { + now -= queue.top().cost; + ans += queue.top().cost; + queue.pop(); + } + + while (!pq[i].empty() && now < C) + { + int e = pq[i].top().end; + int c = pq[i].top().cost; + + int k = min(c, C - now); + now += k; + pq[i].pop(); + queue.push({e, k}); + cout << i << " " << e << " " << k << " " << ans << " " << now << "\n"; + } + } + + cout << ans << "\n"; + return 0; +} From 41c8b50bb760c1f01c0a5549c0fecc197650a3e3 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 14 Mar 2021 11:55:30 +0900 Subject: [PATCH 075/203] =?UTF-8?q?=EC=A0=84=EB=A0=A5=EB=82=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../6497.cpp" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/6497.cpp" diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/6497.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/6497.cpp" new file mode 100644 index 0000000..d32a748 --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/6497.cpp" @@ -0,0 +1,81 @@ +#include +#include + +using namespace std; + +int p[200001]; + +struct node +{ + long long w; + int x, y; + + bool operator<(const node &a) const + { + return w > a.w; + } +}; + +int find(int n) +{ + if (n == p[n]) + return n; + return p[n] = find(p[n]); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int N, M, x, y; + long long z; + + while (true) + { + cin >> N >> M; + if (N == 0 && M == 0) + { + break; + } + + for (int i = 0; i < N; i++) + { + p[i] = i; + } + + long long total = 0; + priority_queue pq; + + for (int i = 0; i < M; i++) + { + cin >> x >> y >> z; + total += z; + node new_node; + new_node.w = z; + new_node.x = x; + new_node.y = y; + pq.push(new_node); + } + + int selected = 0; + long long select = 0; + + while (selected < N - 1) + { + node top = pq.top(); + pq.pop(); + + if (find(top.x) != find(top.y)) + { + select += top.w; + selected++; + p[p[top.x]] = p[top.y]; + } + } + + cout << total - select << "\n"; + } + + return 0; +} From 7bad4a9f98a67f29cd98e9d0495e4832ef11cb0f Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 14 Mar 2021 12:12:48 +0900 Subject: [PATCH 076/203] =?UTF-8?q?=EC=A0=84=EA=B8=B0=EA=B0=80=20=EB=B6=80?= =?UTF-8?q?=EC=A1=B1=ED=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../10423.cpp" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 "baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/10423.cpp" diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/10423.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/10423.cpp" new file mode 100644 index 0000000..f044f6c --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/10423.cpp" @@ -0,0 +1,73 @@ +#include +#include + +using namespace std; + +int p[1001]; +int selected = 0; +int ans = 0; + +priority_queue > > pq; + +int find(int n) +{ + if (p[n] == n) + return n; + return p[n] = find(p[n]); +} + +void Union(int x, int y) +{ + if (x > y) + swap(x, y); + + p[y] = x; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + int N, M, K; + cin >> N >> M >> K; + + for (int i = 0; i <= N; i++) + { + p[i] = i; + } + + int x, y, z; + for (int i = 0; i < K; i++) + { + cin >> x; + p[x] = 0; + if (i != K - 1) + selected++; + } + + for (int i = 0; i < M; i++) + { + cin >> x >> y >> z; + pq.push(make_pair(-z, make_pair(x, y))); + } + + while (selected < N - 1) + { + int w = -pq.top().first; + x = pq.top().second.first; + y = pq.top().second.second; + pq.pop(); + + if (find(x) != find(y)) + { + Union(p[x], p[y]); + selected++; + ans += w; + } + } + + cout << ans << "\n"; + + return 0; +} From 8e88546ad12b565e7b3b1781436212cdde2d73bd Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 14 Mar 2021 22:12:17 +0900 Subject: [PATCH 077/203] =?UTF-8?q?=EC=9E=90=EB=AC=BC=EC=87=A0=EC=99=80=20?= =?UTF-8?q?=EC=97=B4=EC=87=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- programmers/031401.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 programmers/031401.cpp diff --git a/programmers/031401.cpp b/programmers/031401.cpp new file mode 100644 index 0000000..c73317e --- /dev/null +++ b/programmers/031401.cpp @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; + +bool solution(vector> key, vector> lock) { + bool answer = false; + + int kx = key.size(); + int ky = key[0].size(); + int lx = lock.size(); + int ly = lock[0].size(); + + int tmp[20][20]; + int check[20][20]; + + for(int t=0; t<4 && !answer; t++){ + for(int x = -kx+1; x < lx && !answer; x++){ + for(int y = -ky+1; y < ly && !answer; y++){ + + bool flag = true; + + for(int i=0; i=0 && b>=0 && a < lx && b < ly){ + if(key[m][n]==0 && lock[a][b] == 0){ + flag = false; + } + else if(key[m][n]==1 && lock[a][b] == 1){ + flag = false; + } + else if(key[m][n]==1){ + check[a][b] = 1; + } + } + } + } + + if(flag){ + bool flag2 = true; + for(int i=0; i Date: Tue, 16 Mar 2021 14:30:12 +0900 Subject: [PATCH 078/203] =?UTF-8?q?=ED=8A=B9=EC=A0=95=ED=95=9C=20=EC=B5=9C?= =?UTF-8?q?=EB=8B=A8=20=EA=B2=BD=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1504.cpp" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/1504.cpp" diff --git "a/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/1504.cpp" "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/1504.cpp" new file mode 100644 index 0000000..6415bd3 --- /dev/null +++ "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/1504.cpp" @@ -0,0 +1,80 @@ +#include +#include +#include + +using namespace std; + +int N, E; +int node1, node2; + +#define INF 1999999999 +vector > edges[801]; + +vector dijkstra(int s, int N) +{ + vector dist(N + 1); + fill(dist.begin(), dist.end(), INF); + priority_queue > pq; + dist[s] = 0; + pq.push(make_pair(0, s)); + + while (!pq.empty()) + { + int d = -pq.top().first; + int now = pq.top().second; + pq.pop(); + + if (dist[now] < d) + continue; + + for (pair e : edges[now]) + { + int next = e.second; + int c = e.first; + if (dist[now] == INF) + continue; + + if (dist[next] > dist[now] + c) + { + dist[next] = dist[now] + c; + pq.push(make_pair(-dist[next], next)); + } + } + } + return dist; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> E; + + for (int i = 0; i < E; i++) + { + int x, y, z; + cin >> x >> y >> z; + edges[x].push_back({z, y}); + edges[y].push_back({z, x}); + } + cin >> node1 >> node2; + + vector dist = dijkstra(1, N); + vector dist1 = dijkstra(node1, N); + vector dist2 = dijkstra(node2, N); + + long long ans = (long long)dist[node1] + dist1[node2] + dist2[N]; + ans = min(ans, (long long)dist[node2] + dist2[node1] + dist1[N]); + + if (ans >= INF) + { + cout << "-1\n"; + } + else + { + cout << ans << "\n"; + } + + return 0; +} From 4bc07ad5de9184b88b44cd2e221d2657a44aed8d Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 16 Mar 2021 15:08:05 +0900 Subject: [PATCH 079/203] =?UTF-8?q?=EC=84=9C=EA=B0=95=20=EA=B7=B8=EB=9D=BC?= =?UTF-8?q?=EC=9A=B4=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14938.cpp" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "baekjoon/\355\224\214\353\241\234\354\235\264\353\223\234\354\231\200\354\203\254/14938.cpp" diff --git "a/baekjoon/\355\224\214\353\241\234\354\235\264\353\223\234\354\231\200\354\203\254/14938.cpp" "b/baekjoon/\355\224\214\353\241\234\354\235\264\353\223\234\354\231\200\354\203\254/14938.cpp" new file mode 100644 index 0000000..3e79871 --- /dev/null +++ "b/baekjoon/\355\224\214\353\241\234\354\235\264\353\223\234\354\231\200\354\203\254/14938.cpp" @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int N, M, R; +int item[101]; + +#define INF 3000 +int d[101][101]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> R; + for (int i = 1; i <= N; i++) + { + cin >> item[i]; + for (int j = 1; j <= N; j++) + { + d[i][j] = INF; + } + d[i][i] = 0; + } + + int x, y, z; + + for (int i = 0; i < R; i++) + { + cin >> x >> y >> z; + d[x][y] = min(d[x][y], z); + d[y][x] = min(d[y][x], z); + } + + for (int k = 1; k <= N; k++) + { + for (int i = 1; i <= N; i++) + { + for (int j = 1; j <= N; j++) + { + d[i][j] = min(d[i][j], d[i][k] + d[k][j]); + } + } + } + + int ans = 0; + + for (int i = 1; i <= N; i++) + { + int temp = 0; + for (int j = 1; j <= N; j++) + { + if (d[i][j] <= M) + { + temp += item[j]; + } + } + ans = max(ans, temp); + } + cout << ans << "\n"; + + return 0; +} From bc134d5e19e81678738461246044ae6c78bfcfcd Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 16 Mar 2021 15:45:37 +0900 Subject: [PATCH 080/203] =?UTF-8?q?=EC=9D=B8=EA=B0=84=20=EB=8C=80=ED=8F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../10473.cpp" | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 "baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/10473.cpp" diff --git "a/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/10473.cpp" "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/10473.cpp" new file mode 100644 index 0000000..3c4a4b4 --- /dev/null +++ "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/10473.cpp" @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +using namespace std; + +#define INF 3000000 + +double sx, sy, ex, ey; +int N; + +int main() +{ + cin >> sx >> sy >> ex >> ey; + cin >> N; + N += 2; + + vector > loc(N + 1); + vector dist(N + 1); + fill(dist.begin(), dist.end(), INF); + + loc[1] = make_pair(sx, sy); + loc[N] = make_pair(ex, ey); + + for (int i = 0; i < N - 2; i++) + { + cin >> sx >> sy; + loc[i + 2] = make_pair(sx, sy); + } + + dist[1] = 0.0; + priority_queue > pq; + pq.push(make_pair(0.0, 1)); + + while (!pq.empty()) + { + double d = -pq.top().first; + int now = pq.top().second; + pq.pop(); + + if (dist[now] < d) + continue; + + for (int next = 1; next <= N; next++) + { + bool flag = false; + double x = sqrt(pow(loc[now].first - loc[next].first, 2) + pow(loc[now].second - loc[next].second, 2)); + double y = x / 5; + + if (d + y < dist[next]) + { + dist[next] = d + y; + flag = true; + } + + if (now != 1 && now != N) + { + if (x >= 50) + { + y = (x - 50) / 5 + 2; + if (d + y < dist[next]) + { + dist[next] = d + y; + flag = true; + } + } + else + { + y = (50 - x) / 5 + 2; + if (d + y < dist[next]) + { + dist[next] = d + y; + flag = true; + } + } + } + + if (flag) + { + pq.push(make_pair(-dist[next], next)); + } + } + } + + cout << dist[N] << "\n"; + + return 0; +} From 7deb0e7c789ab2ff66e0c635c00b215dc51823b2 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 16 Mar 2021 17:06:41 +0900 Subject: [PATCH 081/203] =?UTF-8?q?=EC=97=B4=EC=87=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/9328.cpp" | 139 +++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/9328.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/9328.cpp" "b/baekjoon/\352\265\254\355\230\204/9328.cpp" new file mode 100644 index 0000000..8493d24 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/9328.cpp" @@ -0,0 +1,139 @@ +#include +#include + +using namespace std; + +int T, N, M; +string board[100]; +bool visited[100][100][27]; +bool key[26]; + +int count_key() +{ + int res = 0; + for (int i = 0; i < 26; i++) + { + if (key[i]) + res++; + } + return res; +} + +void check_boundary(queue, int> > &q) +{ + int ihave = count_key(); + for (int i = 0; i < N; i++) + { + if (board[i][0] != '*' && !visited[i][0][ihave]) + { + q.push(make_pair(make_pair(i, 0), ihave)); + visited[i][0][ihave] = true; + } + if (board[i][M - 1] != '*' && !visited[i][M - 1][ihave]) + { + q.push(make_pair(make_pair(i, M - 1), ihave)); + visited[i][M - 1][ihave] = true; + } + } + + for (int i = 1; i < M - 1; i++) + { + if (board[0][i] != '*' && !visited[0][i][ihave]) + { + q.push(make_pair(make_pair(0, i), ihave)); + visited[0][i][ihave] = true; + } + if (board[N - 1][i] != '*' && !visited[N - 1][i][ihave]) + { + q.push(make_pair(make_pair(N - 1, i), ihave)); + visited[N - 1][i][ihave] = true; + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for (int t = 0; t < T; t++) + { + cin >> N >> M; + for (int i = 0; i < N; i++) + { + cin >> board[i]; + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + for (int k = 0; k < 27; k++) + { + visited[i][j][k] = false; + } + } + } + for (int k = 0; k < 26; k++) + { + key[k] = false; + } + + string k; + cin >> k; + if (k[0] != '0') + { + for (int i = 0; i < k.length(); i++) + { + key[k[i] - 'a'] = true; + } + } + + queue, int> > q; + check_boundary(q); + int ans = 0; + + while (!q.empty()) + { + int x = q.front().first.first; + int y = q.front().first.second; + int k = q.front().second; + q.pop(); + + if (board[x][y] == '$') + { + ans++; + board[x][y] = '.'; + } + if (board[x][y] >= 'a' && board[x][y] <= 'z' && !key[board[x][y] - 'a']) + { + key[board[x][y] - 'a'] = true; + } + if (board[x][y] >= 'A' && board[x][y] <= 'Z' && !key[board[x][y] - 'A']) + continue; + + int dx[4] = {0, 0, 1, -1}; + int dy[4] = {1, -1, 0, 0}; + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (nx >= 0 && ny >= 0 && nx < N && ny < M && board[nx][ny] != '*') + { + if (!visited[nx][ny][count_key()]) + { + q.push(make_pair(make_pair(nx, ny), count_key())); + visited[nx][ny][count_key()] = true; + } + } + } + check_boundary(q); + } + + cout << ans << "\n"; + } + return 0; +} From 9eaf54ffb93bfc095d846b9b1e1ef0a39ac215de Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 16 Mar 2021 18:36:38 +0900 Subject: [PATCH 082/203] Create Year of the Cow.cpp --- .../2020-2021 Season/Year of the Cow.cpp | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 USACO Bronze/2020-2021 Season/Year of the Cow.cpp diff --git a/USACO Bronze/2020-2021 Season/Year of the Cow.cpp b/USACO Bronze/2020-2021 Season/Year of the Cow.cpp new file mode 100644 index 0000000..35a4a30 --- /dev/null +++ b/USACO Bronze/2020-2021 Season/Year of the Cow.cpp @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +int get_year(string w) +{ + if (w == "Ox") + return 1; + if (w == "Tiger") + return 2; + if (w == "Rabbit") + return 3; + if (w == "Dragon") + return 4; + if (w == "Snake") + return 5; + if (w == "Horse") + return 6; + if (w == "Goat") + return 7; + if (w == "Monkey") + return 8; + if (w == "Rooster") + return 9; + if (w == "Dog") + return 10; + if (w == "Pig") + return 11; + return 12; +} + +int get_month(int n) +{ + return (n - 1) % 12 + 1; +} + +map input; + +int main() +{ + input["Bessie"] = 1; + + int N; + cin >> N; + string tmp[8]; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < 8; j++) + { + cin >> tmp[j]; + } + + int diff = abs(input[tmp[7]] - get_year(tmp[0])); + if (tmp[3] == "previous") + { + int dx = get_month(input[tmp[7]]) - get_year(tmp[4]); + if (dx < 0) + dx += 12; + input[tmp[0]] = input[tmp[7]] - dx; + cout << dx << "\n"; + } + else + { + int dx = get_year(tmp[4]) - get_month(input[tmp[7]]); + if (dx > 0) + dx -= 12; + input[tmp[0]] = input[tmp[7]] + dx; + cout << dx << "\n"; + } + + cout << tmp[0] << " " << input[tmp[0]] << "\n"; + } + + cout << abs(input["Bessie"] - input["Elsie"]) << "\n"; + + return 0; +} From 4c5f669a8426897704c7d6f2457e8f0d1dc7a559 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 16 Mar 2021 19:17:01 +0900 Subject: [PATCH 083/203] Create Comfortable Cows.cpp --- .../2020-2021 Season/Comfortable Cows.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 USACO Bronze/2020-2021 Season/Comfortable Cows.cpp diff --git a/USACO Bronze/2020-2021 Season/Comfortable Cows.cpp b/USACO Bronze/2020-2021 Season/Comfortable Cows.cpp new file mode 100644 index 0000000..83557e7 --- /dev/null +++ b/USACO Bronze/2020-2021 Season/Comfortable Cows.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +int N; +bool board[1001][1001]; +int ans = 0; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +void check(int x, int y) +{ + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx <= 1000 && ny <= 1000) + { + if (board[nx][ny]) + { + int count = 0; + for (int j = 0; j < 4; j++) + { + int nnx = nx + dx[j]; + int nny = ny + dy[j]; + if (nnx >= 0 && nny >= 0 && nnx <= 1000 && nny <= 1000) + { + if (board[nnx][nny]) + count++; + } + } + if (!board[x][y] && count == 3) + ans -= 1; + else if (!board[x][y] && count == 2) + ans += 1; + } + } + } + + board[x][y] = true; +} + +int main() +{ + + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N; + for (int i = 0; i < N; i++) + { + int x, y; + cin >> x >> y; + check(x, y); + cout << ans << "\n"; + } + + return 0; +} From 6b9dfffc5cc837179464d1dddc64b15a4a007a8f Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 17 Mar 2021 13:20:02 +0900 Subject: [PATCH 084/203] Comfortable Cows -Silver --- .../2020-2021 Season/Comfortable Cows2.cpp | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 USACO Bronze/2020-2021 Season/Comfortable Cows2.cpp diff --git a/USACO Bronze/2020-2021 Season/Comfortable Cows2.cpp b/USACO Bronze/2020-2021 Season/Comfortable Cows2.cpp new file mode 100644 index 0000000..6119446 --- /dev/null +++ b/USACO Bronze/2020-2021 Season/Comfortable Cows2.cpp @@ -0,0 +1,85 @@ +#include +#include +#include + +using namespace std; + +bool dot[3000][3000]; +int total_cow = 0; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +queue > q; + +void evaluate(int x, int y) +{ + if (!dot[x][y]) + return; + //cout << x << " " << y << "\n"; + int count = 0; + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (dot[nx][ny]) + count++; + } + + if (count == 3) + { + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (!dot[nx][ny]) + { + q.push({nx, ny}); + } + } + } +} + +void check_dot(int x, int y) +{ + q.push({x, y}); + while (!q.empty()) + { + x = q.front().first; + y = q.front().second; + q.pop(); + + if (dot[x][y]) + continue; + + total_cow++; + dot[x][y] = true; + evaluate(x, y); + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + evaluate(nx, ny); + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int N, x, y; + cin >> N; + + for (int i = 1; i <= N; i++) + { + cin >> x >> y; + check_dot(x + 1000, y + 1000); + cout << total_cow - i << "\n"; + } + + return 0; +} From abeab1cb8318851a368fd08554c1c6092d03a3e0 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 19 Mar 2021 23:17:45 +0900 Subject: [PATCH 085/203] =?UTF-8?q?=EB=B0=B0=EC=97=B4=20=EB=8F=8C=EB=A6=AC?= =?UTF-8?q?=EA=B8=B0=20-=20=ED=91=B8=EB=8A=94=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/17406.cpp" | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/17406.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/17406.cpp" "b/baekjoon/\352\265\254\355\230\204/17406.cpp" new file mode 100644 index 0000000..9f4d462 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/17406.cpp" @@ -0,0 +1,92 @@ +#include + +using namespace std; + +int N, M, K; +int A[100][100]; + +int[][] spin(int[][] A, int x, int y, int z) +{ + int temp[100][100]; + for (int k = z; k >= 0; k--) + { + for (int j = y - k + 1; j <= y + k; j++) + { + temp[x - k][j] = A[x - k][j - 1]; + } + + for (int i = x - k + 1; i <= x + k; i++) + { + temp[i][y + k] = A[i - 1][y + k]; + } + + for (int j = y - k; j < y + k; j++) + { + temp[x + k][j] = A[x + k][j + 1]; + } + + for (int i = x - k; i < x + k; i++) + { + temp[i][y - k] = A[i + 1][y - k]; + } + } + + temp[x][y] = A[x][y]; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + if (i < x - z || i > x + z || j < y - z || j > y + z) + { + temp[i][j] = A[i][j]; + } + } + } + return temp; +} + +int get_sum(int[][] temp) +{ + + int res = 200000; + + for (int i = 0; i < N; i++) + { + int sum = 0; + for (int j = 0; j < M; j++) + { + sum += temp[i][j]; + } + res = min(res, sum); + } + + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> K; + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cin >> A[i][j]; + } + } + + int x, y, z; + int ans = 2000000; + + for (int i = 0; i < K; i++) + { + cin >> x >> y >> z; + } + + cout << ans << "\n"; + + return 0; +} From f7a5ab4b56568852517f5189f9d40b4eea86a5a1 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 21 Mar 2021 12:43:01 +0900 Subject: [PATCH 086/203] =?UTF-8?q?=EC=95=84=EA=B8=B0=20=EC=83=81=EC=96=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/16236.cpp" | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/16236.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/16236.cpp" "b/baekjoon/\352\265\254\355\230\204/16236.cpp" new file mode 100644 index 0000000..c92f267 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/16236.cpp" @@ -0,0 +1,124 @@ +#include +#include + +using namespace std; + +int A[20][20]; +int N; +int sx, sy; +int ans = 0; +int ssize = 2; +int scount = 0; + +struct node +{ + int x, y, move; + + bool operator<(const node &a) const + { + if (move == a.move) + { + if (x == a.x) + { + return y > a.y; + } + return x > a.x; + } + return move > a.move; + } +}; + +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +void solve() +{ + while (true) + { + queue search; + priority_queue find; + bool visited[400]; + fill(visited, visited + 400, 0); + + search.push({sx, sy, 0}); + visited[sx * N + sy] = true; + while (!search.empty()) + { + int x = search.front().x; + int y = search.front().y; + int move = search.front().move; + search.pop(); + + if (A[x][y] > 0 && A[x][y] < ssize) + { + find.push({x, y, move}); + continue; + } + + if (!find.empty() && find.top().move < move + 1) + { + continue; + } + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < N && !visited[nx * N + ny] && A[nx][ny] <= ssize) + { + search.push({nx, ny, move + 1}); + visited[nx * N + ny] = true; + } + } + } + + if (!find.empty()) + { + sx = find.top().x; + sy = find.top().y; + scount++; + if (scount == ssize) + { + ssize++; + scount = 0; + } + + A[sx][sy] = 0; + ans += find.top().move; + cout << sx << " " << sy << " " << ans << "\n"; + } + else + { + break; + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + cin >> A[i][j]; + if (A[i][j] == 9) + { + sx = i; + sy = j; + A[i][j] = 0; + } + } + } + + int x; + + solve(); + + cout << ans << "\n"; + + return 0; +} From c52f05839b4f125a6ac5cbfdc95a8aa818e95342 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 22 Mar 2021 18:13:41 +0900 Subject: [PATCH 087/203] =?UTF-8?q?A=EC=99=80=20B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/12904.cpp" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/12904.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/12904.cpp" "b/baekjoon/\352\265\254\355\230\204/12904.cpp" new file mode 100644 index 0000000..93b7579 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/12904.cpp" @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +int main() +{ + + string s, target; + cin >> s >> target; + + while (s != target && target.length() > 0) + { + int index = target.length() - 1; + if (target[index] == 'A') + { + target = target.substr(0, index); + } + else + { + target = target.substr(0, index); + reverse(target.begin(), target.end()); + } + cout << target << "\t"; + } + + if (s == target) + { + cout << "1\n"; + } + else + { + cout << "0\n"; + } + + return 0; +} From 4d0677a1b81229ccf57b74fb3ff8d53a9997e407 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 23 Mar 2021 13:27:25 +0900 Subject: [PATCH 088/203] =?UTF-8?q?=EB=B9=97=EB=AC=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/14719.cpp" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/14719.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/14719.cpp" "b/baekjoon/\352\265\254\355\230\204/14719.cpp" new file mode 100644 index 0000000..cc6a129 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/14719.cpp" @@ -0,0 +1,67 @@ +#include + +using namespace std; + +int N, M; +int A[500]; +bool rain[500][500]; +int ans; + +void check(int i, int j) +{ + bool left = false, right = false; + + for (int y = j - 1; y >= 0; y--) + { + if (A[y] - 1 >= i) + { + left = true; + break; + } + } + for (int y = j + 1; y < M; y++) + { + if (A[y] - 1 >= i) + { + right = true; + break; + } + } + + if (left && right) + { + rain[i][j] = true; + ans++; + cout << i << " " << j << " " << ans << "\n"; + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for (int i = 0; i < M; i++) + { + cin >> A[i]; + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + if (A[j] > i) + continue; + + if (i == 0 || rain[i - 1][j] || A[j] == i) + { + check(i, j); + } + } + } + + cout << ans << "\n"; + + return 0; +} From 36d95e236d9710576047cc8f2038c27e0a04dec3 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 23 Mar 2021 14:34:28 +0900 Subject: [PATCH 089/203] =?UTF-8?q?=EC=A2=8B=EC=9D=80=20=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/5624.cpp" | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/5624.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/5624.cpp" "b/baekjoon/\352\265\254\355\230\204/5624.cpp" new file mode 100644 index 0000000..1f0ac77 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/5624.cpp" @@ -0,0 +1,39 @@ +#include + +using namespace std; + +#define OFFSET 200000 +bool exist[400010]; +int A[5000]; +int N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + int x; + int ans = 0; + + for (int i = 0; i < N; i++) + { + cin >> A[i]; + + for (int j = 0; j < i; j++) + { + if (exist[A[i] - A[j] + OFFSET]) + { + ans++; + break; + } + } + + for (int j = 0; j <= i; j++) + { + exist[A[j] + A[i] + OFFSET] = true; + } + } + + cout << ans << "\n"; + return 0; +} From b4e93f45681b4d81ef76ed6928463e7d19ebd631 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 23 Mar 2021 14:51:31 +0900 Subject: [PATCH 090/203] =?UTF-8?q?=EB=A1=9C=EB=B4=87=20=EC=B2=AD=EC=86=8C?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/14503.cpp" | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/14503.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/14503.cpp" "b/baekjoon/\352\265\254\355\230\204/14503.cpp" new file mode 100644 index 0000000..1d0197a --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/14503.cpp" @@ -0,0 +1,88 @@ +#include + +using namespace std; + +int N, M; +int R, C, dir; +bool visited[50][50]; +bool wall[50][50]; + +// 0 1 2 3 +// 위 왼 아 오 +// 위 동 아 왼 +int dx[8] = {0, 1, 0, -1, 0, 1, 0, -1}; +int dy[8] = {-1, 0, 1, 0, -1, 0, 1, 0}; +int bx[4] = {1, 0, -1, 0}; +int by[4] = {0, 1, 0, -1}; + +int dfs(int x, int y, int d) +{ + int res = 0; + + if (!visited[x][y]) + { + res += 1; + visited[x][y] = true; + } + + //cout << x << " " << y << " " << d << "\n"; + + bool flag = false; + + for (int i = d; i < d + 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!visited[nx][ny] && !wall[nx][ny]) + { + res += dfs(nx, ny, (i + 1) % 4); + flag = true; + break; + } + } + } + + if (!flag) + { + int nx = x + bx[d]; + int ny = y + by[d]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M && !wall[nx][ny]) + { + res += dfs(nx, ny, d); + } + } + + return res; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + cin >> R >> C >> dir; + + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + cin >> wall[i][j]; + } + } + + if (dir == 1) + { + dir = 3; + } + else if (dir == 3) + { + dir = 1; + } + + cout << dfs(R, C, dir) << "\n"; + + return 0; +} From ca97bdd3a0c17c88e3595b9852ffe4527506fefb Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 23 Mar 2021 16:56:27 +0900 Subject: [PATCH 091/203] =?UTF-8?q?=EA=B5=AC=EC=8A=AC=20=ED=83=88=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/13459.cpp" | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/13459.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/13459.cpp" "b/baekjoon/\352\265\254\355\230\204/13459.cpp" new file mode 100644 index 0000000..3b785ca --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/13459.cpp" @@ -0,0 +1,154 @@ +#include +#include + +using namespace std; + +#define RED 0 +#define BLUE 1 + +struct node +{ + int x, y, move; +}; + +int N, M; +string board[10]; +queue bfs[2]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {-1, 1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for (int i = 0; i < N; i++) + { + cin >> board[i]; + for (int j = 0; j < M; j++) + { + if (board[i][j] == 'R') + { + bfs[RED].push({i, j, 0}); + board[i][j] = '.'; + } + else if (board[i][j] == 'B') + { + bfs[BLUE].push({i, j, 0}); + board[i][j] = '.'; + } + } + } + + while (!bfs[RED].empty()) + { + int rx = bfs[RED].front().x; + int ry = bfs[RED].front().y; + int bx = bfs[BLUE].front().x; + int by = bfs[BLUE].front().y; + int move = bfs[RED].front().move; + + if (bx == -1) + { + bfs[RED].pop(); + bfs[BLUE].pop(); + continue; + } + + if (board[rx][ry] == 'O') + { + cout << "1\n"; + return 0; + } + + if (move >= 10) + { + bfs[RED].pop(); + bfs[BLUE].pop(); + continue; + } + + for (int dir = 0; dir < 4; dir++) + { + int idx = 0; + if (dir == 0) + { + if (ry > by) + idx = 1; + } + else if (dir == 1) + { + if (by > ry) + idx = 1; + } + else if (dir == 2) + { + if (bx > rx) + idx = 1; + } + else + { + if (bx < rx) + { + idx = 1; + } + } + + for (int t = 0; t < 2; t++) + { + int x = bfs[idx].front().x; + int y = bfs[idx].front().y; + + while (true) + { + int nx = x + dx[dir]; + int ny = y + dy[dir]; + + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (board[nx][ny] == 'O') + { + if (idx == RED) + { + x = nx; + y = ny; + break; + } + else + { + x = -1; + y = -1; + break; + } + } + else if (board[nx][ny] == '#') + break; + else + { + if (t == 1 && bfs[1 - idx].back().x == nx && bfs[1 - idx].back().y == ny) + { + break; + } + else + { + x = nx; + y = ny; + } + } + } + } + + bfs[idx].push({x, y, move + 1}); + idx = 1 - idx; + } + } + + bfs[RED].pop(); + bfs[BLUE].pop(); + } + + cout << "0\n"; + + return 0; +} From 94e231573fe62b2ab09f2a7e0be55e237e502fe9 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 23 Mar 2021 16:58:48 +0900 Subject: [PATCH 092/203] =?UTF-8?q?=EA=B5=AC=EC=8A=AC=20=ED=83=88=EC=B6=9C?= =?UTF-8?q?=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/13490.cpp" | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/13490.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/13490.cpp" "b/baekjoon/\352\265\254\355\230\204/13490.cpp" new file mode 100644 index 0000000..94312dd --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/13490.cpp" @@ -0,0 +1,154 @@ +#include +#include + +using namespace std; + +#define RED 0 +#define BLUE 1 + +struct node +{ + int x, y, move; +}; + +int N, M; +string board[10]; +queue bfs[2]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {-1, 1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for (int i = 0; i < N; i++) + { + cin >> board[i]; + for (int j = 0; j < M; j++) + { + if (board[i][j] == 'R') + { + bfs[RED].push({i, j, 0}); + board[i][j] = '.'; + } + else if (board[i][j] == 'B') + { + bfs[BLUE].push({i, j, 0}); + board[i][j] = '.'; + } + } + } + + while (!bfs[RED].empty()) + { + int rx = bfs[RED].front().x; + int ry = bfs[RED].front().y; + int bx = bfs[BLUE].front().x; + int by = bfs[BLUE].front().y; + int move = bfs[RED].front().move; + + if (bx == -1) + { + bfs[RED].pop(); + bfs[BLUE].pop(); + continue; + } + + if (board[rx][ry] == 'O') + { + cout << move << "\n"; + return 0; + } + + if (move >= 10) + { + bfs[RED].pop(); + bfs[BLUE].pop(); + continue; + } + + for (int dir = 0; dir < 4; dir++) + { + int idx = 0; + if (dir == 0) + { + if (ry > by) + idx = 1; + } + else if (dir == 1) + { + if (by > ry) + idx = 1; + } + else if (dir == 2) + { + if (bx > rx) + idx = 1; + } + else + { + if (bx < rx) + { + idx = 1; + } + } + + for (int t = 0; t < 2; t++) + { + int x = bfs[idx].front().x; + int y = bfs[idx].front().y; + + while (true) + { + int nx = x + dx[dir]; + int ny = y + dy[dir]; + + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (board[nx][ny] == 'O') + { + if (idx == RED) + { + x = nx; + y = ny; + break; + } + else + { + x = -1; + y = -1; + break; + } + } + else if (board[nx][ny] == '#') + break; + else + { + if (t == 1 && bfs[1 - idx].back().x == nx && bfs[1 - idx].back().y == ny) + { + break; + } + else + { + x = nx; + y = ny; + } + } + } + } + + bfs[idx].push({x, y, move + 1}); + idx = 1 - idx; + } + } + + bfs[RED].pop(); + bfs[BLUE].pop(); + } + + cout << "-1\n"; + + return 0; +} From 86663028659bac0a86aed5e4b7f88aef47210e1f Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 25 Mar 2021 19:08:22 +0900 Subject: [PATCH 093/203] =?UTF-8?q?=EB=B6=80=EB=B6=84=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=EC=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/KMP/16916.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 baekjoon/KMP/16916.cpp diff --git a/baekjoon/KMP/16916.cpp b/baekjoon/KMP/16916.cpp new file mode 100644 index 0000000..c682645 --- /dev/null +++ b/baekjoon/KMP/16916.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +int main() +{ + + string s, p; + cin >> s >> p; + + vector pi(p.length(), 0); + int j = 0; + for (int i = 1; i < p.length(); i++) + { + while (j > 0 && p[i] != p[j]) + { + j = pi[j - 1]; + } + + if (p[i] == p[j]) + { + pi[i] = ++j; + } + } + + j = 0; + for (int i = 0; i < s.length(); i++) + { + + while (j > 0 && s[i] != p[j]) + { + j = pi[j - 1]; + } + + if (s[i] == p[j]) + { + j++; + } + + if (j == p.length()) + { + cout << "1\n"; + return 0; + } + } + + cout << "0\n"; + + return 0; +} From 25b0bd71df0a3c1c4702b0e0cc0b6d34f8456fca Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 25 Mar 2021 19:31:07 +0900 Subject: [PATCH 094/203] Cubeditor --- baekjoon/KMP/1701.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 baekjoon/KMP/1701.cpp diff --git a/baekjoon/KMP/1701.cpp b/baekjoon/KMP/1701.cpp new file mode 100644 index 0000000..c201052 --- /dev/null +++ b/baekjoon/KMP/1701.cpp @@ -0,0 +1,39 @@ +#include +#include + +using namespace std; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + string s; + cin >> s; + + vector pi(s.length(), 0); + + int ans = 0; + + for (int index = 0; index < s.length() - 1; index++) + { + fill(pi.begin(), pi.end(), 0); + int j = 0; + for (int i = index + 1; i < s.length(); i++) + { + while (j > 0 && s[i] != s[j + index]) + { + j = pi[j - 1 + index]; + } + if (s[i] == s[j + index]) + { + pi[i] = ++j; + } + ans = max(ans, pi[i]); + } + } + + cout << ans << "\n"; + + return 0; +} From 45db11684c6fbd4218f3d9c9bd6d8a2175a4e39d Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 28 Mar 2021 11:30:42 +0900 Subject: [PATCH 095/203] =?UTF-8?q?=EB=AF=B8=EB=A1=9C=20=ED=83=88=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14923.cpp" | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14923.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14923.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14923.cpp" new file mode 100644 index 0000000..f315328 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14923.cpp" @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +struct node +{ + int x, y, z; + bool broken; +}; + +int N, M, sx, sy, ex, ey; +bool wall[1000][1000]; +bool visited[1000][1000][2]; +queue bfsQueue; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> sx >> sy >> ex >> ey; + sx--; + sy--; + ex--; + ey--; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cin >> wall[i][j]; + } + } + + bfsQueue.push({sx, sy, 0, false}); + visited[sx][sy][0] = true; + + int dx[4] = {0, 0, 1, -1}; + int dy[4] = {1, -1, 0, 0}; + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int z = bfsQueue.front().z; + bool broken = bfsQueue.front().broken; + bfsQueue.pop(); + + if (ex == x && ey == y) + { + cout << z << "\n"; + return 0; + } + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!wall[nx][ny] && !visited[nx][ny][broken]) + { + bfsQueue.push({nx, ny, z + 1, broken}); + visited[nx][ny][broken] = true; + } + else if (wall[nx][ny] && !visited[nx][ny][true] && !broken) + { + bfsQueue.push({nx, ny, z + 1, true}); + visited[nx][ny][true] = true; + } + } + } + } + + cout << "-1\n"; + + return 0; +} From 7cd08c1726d4f5238b2a111a6c151c27615f0bca Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 28 Mar 2021 12:14:13 +0900 Subject: [PATCH 096/203] =?UTF-8?q?=EB=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5427.cpp" | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/5427.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/5427.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/5427.cpp" new file mode 100644 index 0000000..54ceb92 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/5427.cpp" @@ -0,0 +1,112 @@ +#include +#include + +using namespace std; + +struct node +{ + int type, x, y, d; +}; + +int T, N, M; +string board[1000]; +bool visited[1000][1000][2]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> T; + + for (int t = 0; t < T; t++) + { + queue bfsQueue; + cin >> M >> N; + + for (int i = 0; i < N; i++) + { + cin >> board[i]; + for (int j = 0; j < M; j++) + { + for (int k = 0; k < 2; k++) + { + visited[i][j][k] = false; + } + } + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + if (board[i][j] == '*') + { + bfsQueue.push({1, i, j, 0}); + visited[i][j][1] = true; + } + } + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + if (board[i][j] == '@') + { + bfsQueue.push({0, i, j, 0}); + visited[i][j][0] = true; + } + } + } + + bool flag = false; + + while (!bfsQueue.empty()) + { + int type = bfsQueue.front().type; + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int d = bfsQueue.front().d; + bfsQueue.pop(); + + if (type == 0 && (x <= 0 || y <= 0 || x >= N - 1 || y >= M - 1)) + { + cout << d + 1 << "\n"; + flag = true; + break; + } + + for (int i = 0; i < 4; i++) + { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (type == 1 && nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!visited[nx][ny][1] && board[nx][ny] != '#') + { + bfsQueue.push({1, nx, ny, d + 1}); + visited[nx][ny][1] = true; + board[nx][ny] = '*'; + } + } + else if (type == 0 && nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (!visited[nx][ny][0] && board[nx][ny] == '.') + { + bfsQueue.push({0, nx, ny, d + 1}); + visited[nx][ny][0] = true; + } + } + } + } + + if (!flag) + { + cout << "IMPOSSIBLE\n"; + } + } + return 0; +} From 768dae11d4126776b533d4155e9951c3afaf212c Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 28 Mar 2021 14:10:30 +0900 Subject: [PATCH 097/203] =?UTF-8?q?=EB=82=9A=EC=8B=9C=EC=99=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/17143.cpp" | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/17143.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/17143.cpp" "b/baekjoon/\352\265\254\355\230\204/17143.cpp" new file mode 100644 index 0000000..f794c36 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/17143.cpp" @@ -0,0 +1,143 @@ +#include +#include + +using namespace std; + +int dx[4] = {-1, 1, 0, 0}; +int dy[4] = {0, 0, 1, -1}; +int bd[4] = {1, 0, 3, 2}; + +struct shark +{ + int x, y, speed, dir, size; + bool live; +}; + +int N, M, S; +int ans = 0; + +void catch_shark(int here, vector &myshark) +{ + + int x = 200; + int index = -1; + + for (int i = 0; i < myshark.size(); i++) + { + if (myshark[i].live && here == myshark[i].y && myshark[i].x < x) + { + index = i; + x = myshark[i].x; + } + } + + if (index != -1) + { + ans += myshark[index].size; + myshark[index].live = false; + } +} + +void shark_move(vector &myshark) +{ + int temp[100][100]; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + temp[i][j] = -1; + } + } + + for (int i = 0; i < myshark.size(); i++) + { + if (!myshark[i].live) + continue; + + int x = myshark[i].x; + int y = myshark[i].y; + int dir = myshark[i].dir; + int speed = myshark[i].speed; + + int nx = x + dx[dir] * speed; + int ny = y + dy[dir] * speed; + + while (nx < 0 || nx >= N) + { + dir = bd[dir]; + if (nx < 0) + nx *= -1; + else + nx = 2 * N - nx - 2; + } + while (ny < 0 || ny >= M) + { + dir = bd[dir]; + if (ny < 0) + ny *= -1; + else + ny = 2 * M - ny - 2; + } + + myshark[i].dir = dir; + myshark[i].x = nx; + myshark[i].y = ny; + + //cout << "!" << i << " " << myshark[i].x << " " << myshark[i].y << " " << myshark[i].dir << "\n"; + + if (temp[nx][ny] == -1) + { + temp[nx][ny] = i; + } + else + { + int there = temp[nx][ny]; + if (!myshark[there].live) + { + temp[nx][ny] = i; + } + else if (myshark[there].size < myshark[i].size) + { + myshark[there].live = false; + temp[nx][ny] = i; + } + else + { + myshark[i].live = false; + } + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> S; + + vector myshark; + + for (int i = 0; i < S; i++) + { + int r, c, s, d, z; + cin >> r >> c >> s >> d >> z; + myshark.push_back({r - 1, c - 1, s, d - 1, z, true}); + } + + for (int move = 0; move < M; move++) + { + catch_shark(move, myshark); + shark_move(myshark); + + for (int i = 0; i < myshark.size(); i++) + { + //cout << myshark[i].x << " " << myshark[i].y << " " << myshark[i].dir << " " << myshark[i].size << " " << myshark[i].speed << "\n"; + } + } + + cout << ans << "\n"; + + return 0; +} From d1dacdf1e36be252a284df61dda0f8c2d77c7252 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 29 Mar 2021 13:12:50 +0900 Subject: [PATCH 098/203] =?UTF-8?q?=EC=84=9C=EC=9A=B8=20=EC=A7=80=ED=95=98?= =?UTF-8?q?=EC=B2=A0=202=ED=98=B8=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16947.cpp" | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/16947.cpp" diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/16947.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/16947.cpp" new file mode 100644 index 0000000..6eec575 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/16947.cpp" @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +vector edges[3001]; +int N; +bool visited[3001]; +bool cycle[3001]; + +void init() +{ + for (int i = 1; i <= N; i++) + { + visited[i] = false; + } +} + +void check(int n, int start, int d) +{ + visited[start] = true; + for (int v : edges[start]) + { + if (v == n && d > 1) + { + cycle[n] = true; + return; + } + if (!visited[v]) + { + check(n, v, d + 1); + } + } +} + +int solve(int n, int r) +{ + visited[n] = true; + if (cycle[n]) + return 0; + + r = 100000000; + for (int v : edges[n]) + { + if (!visited[v]) + r = min(r, solve(v, r) + 1); + } + return r; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N; + int x, y; + for (int i = 0; i < N; i++) + { + cin >> x >> y; + edges[x].push_back(y); + edges[y].push_back(x); + } + + for (int i = 1; i <= N; i++) + { + init(); + check(i, i, 0); + } + + for (int i = 1; i <= N; i++) + { + init(); + cout << solve(i, 0) << " "; + } + + return 0; +} From 18e0eb9136520822bf8176b0d7c1a0776b279ca5 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 29 Mar 2021 13:53:21 +0900 Subject: [PATCH 099/203] =?UTF-8?q?=EC=B6=9C=EA=B7=BC=EA=B8=B0=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/14239.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 baekjoon/DP/14239.cpp diff --git a/baekjoon/DP/14239.cpp b/baekjoon/DP/14239.cpp new file mode 100644 index 0000000..1d42afe --- /dev/null +++ b/baekjoon/DP/14239.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int Acount = 0, Bcount = 0, Ccount = 0; +char ans[51]; +bool DP[51][51][51][3][3]; + +bool solve(int a, int b, int c, int pre, int pre2) +{ + if (a == Acount && b == Bcount && c == Ccount) + return true; + + if (DP[a][b][c][pre][pre2]) + return false; + DP[a][b][c][pre][pre2] = true; + + if (a < Acount) + { + ans[a + b + c] = 'A'; + if (solve(a + 1, b, c, 0, pre)) + return true; + } + if (b < Bcount && pre != 1) + { + ans[a + b + c] = 'B'; + if (solve(a, b + 1, c, 1, pre)) + return true; + } + if (c < Ccount && pre != 2 && pre2 != 2) + { + ans[a + b + c] = 'C'; + if (solve(a, b, c + 1, 2, pre)) + return true; + } + return false; +} + +int main() +{ + string s; + cin >> s; + + for (int i = 0; i < s.length(); i++) + { + if (s[i] == 'A') + Acount++; + else if (s[i] == 'B') + Bcount++; + else + Ccount++; + } + + if (solve(0, 0, 0, -1, -1)) + { + for (int i = 0; i < s.length(); i++) + { + cout << ans[i]; + } + } + else + { + cout << "-1\n"; + } + return 0; +} From 45002bc765f38460a6788fd1be43e30ae5ef1b88 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 30 Mar 2021 12:55:04 +0900 Subject: [PATCH 100/203] =?UTF-8?q?=EC=B5=9C=EC=A2=85=20=EC=88=9C=EC=9C=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../3665.cpp" | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 "baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/3665.cpp" diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/3665.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/3665.cpp" new file mode 100644 index 0000000..847e4e5 --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/3665.cpp" @@ -0,0 +1,116 @@ +#include +#include +#include + +using namespace std; + +int T, N, S; +int A[501]; +int degree[501]; +bool edge[501][501]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> T; + for (int t = 0; t < T; t++) + { + cin >> N; + for (int i = 0; i < N; i++) + { + cin >> A[i]; + } + + for (int i = 0; i <= N; i++) + { + degree[i] = 0; + for (int j = 0; j <= N; j++) + { + edge[i][j] = false; + } + } + + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + degree[A[j]]++; + edge[A[i]][A[j]] = true; + } + } + cin >> S; + int x, y; + for (int i = 0; i < S; i++) + { + cin >> x >> y; + if (edge[x][y]) + { + degree[y]--; + degree[x]++; + edge[x][y] = false; + edge[y][x] = true; + } + else if (edge[y][x]) + { + degree[x]--; + degree[y]++; + edge[y][x] = false; + edge[x][y] = true; + } + } + + queue q; + vector ans; + for (int i = 1; i <= N; i++) + { + if (degree[i] == 0) + q.push(i); + } + + while (!q.empty()) + { + if (q.size() >= 2) + { + break; + } + + x = q.front(); + q.pop(); + ans.push_back(x); + + for (int i = 1; i <= N; i++) + { + if (edge[x][i]) + { + edge[x][i] = false; + degree[i]--; + if (degree[i] == 0) + { + q.push(i); + } + } + } + } + + if (ans.size() == N) + { + for (int a : ans) + { + cout << a << " "; + } + cout << "\n"; + } + else if (q.size() >= 2) + { + cout << "?\n"; + } + else + { + cout << "IMPOSSIBLE\n"; + } + } + return 0; +} From 0e8002debaec63fe2d1efa5bfcc0a4f884454add Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 30 Mar 2021 14:17:17 +0900 Subject: [PATCH 101/203] =?UTF-8?q?=EA=B5=AC=EA=B0=84=20=EA=B3=B1=20?= =?UTF-8?q?=EA=B5=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../11505.cpp" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/11505.cpp" diff --git "a/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/11505.cpp" "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/11505.cpp" new file mode 100644 index 0000000..6c58c4d --- /dev/null +++ "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/11505.cpp" @@ -0,0 +1,74 @@ +#include +#include + +using namespace std; + +int N, M, K; + +int seg[4000000]; +int A[1000001]; + +int init(int node, int start, int end) +{ + if (start > N) + return 1; + if (start == end) + return seg[node] = A[start]; + int middle = (start + end) / 2; + return seg[node] = + (long long)init(node * 2, start, middle) * init(node * 2 + 1, middle + 1, end) % 1000000007; +} + +int find(int node, int left, int right, int start, int end) +{ + if (start >= left && end <= right) + return seg[node]; + if (start > right || end < left) + return 1; + + int middle = (start + end) / 2; + + return (long long)find(node * 2, left, right, start, middle) * find(node * 2 + 1, left, right, middle + 1, end) % 1000000007; +} + +int rebuild(int node, int start, int end, int index, int n) +{ + if (start > N || index < start || end < index) + return seg[node]; + + if (start == end && index == start) + return seg[node] = n; + + int middle = (start + end) / 2; + return seg[node] = (long long)rebuild(node * 2, start, middle, index, n) * rebuild(node * 2 + 1, middle + 1, end, index, n) % 1000000007; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> K; + for (int i = 1; i <= N; i++) + { + cin >> A[i]; + } + + init(1, 1, pow(2, ceil(log2(N)))); + int x, y, z; + + for (int i = 0; i < M + K; i++) + { + cin >> x >> y >> z; + if (x == 1) + { + rebuild(1, 1, pow(2, ceil(log2(N))), y, z); + } + else + { + cout << find(1, y, z, 1, pow(2, ceil(log2(N)))) << "\n"; + } + } + + return 0; +} From aeb7464fc9880c705008cd18a63f22f23ed1a5f3 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 30 Mar 2021 14:56:45 +0900 Subject: [PATCH 102/203] =?UTF-8?q?=EB=AC=BC=ED=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14867.cpp" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14867.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14867.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14867.cpp" new file mode 100644 index 0000000..2c2a3d2 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/14867.cpp" @@ -0,0 +1,83 @@ +#include +#include +#include + +using namespace std; +int A, B, C, D; + +struct node +{ + int a, b, d; +}; + +set visited[100010]; +queue bfsQueue; + +bool isVisited(int x, int y) +{ + if (visited[x].find(y) == visited[x].end()) + return false; + return true; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> A >> B >> C >> D; + bfsQueue.push({0, 0, 0}); + visited[0].insert(0); + + while (!bfsQueue.empty()) + { + int a = bfsQueue.front().a; + int b = bfsQueue.front().b; + int d = bfsQueue.front().d; + bfsQueue.pop(); + + if (a == C && b == D) + { + cout << d << "\n"; + return 0; + } + + if (a != A && !isVisited(A, b)) + { + bfsQueue.push({A, b, d + 1}); + visited[A].insert(b); + } + if (b != B && !isVisited(a, B)) + { + bfsQueue.push({a, B, d + 1}); + visited[a].insert(B); + } + if (a != 0 && !isVisited(0, b)) + { + bfsQueue.push({0, b, d + 1}); + visited[0].insert(b); + } + if (b != 0 && !isVisited(a, 0)) + { + bfsQueue.push({a, 0, d + 1}); + visited[a].insert(0); + } + int x = min(A, a + b); + int y = max((a + b - A), 0); + if (x != a && y != b && !isVisited(x, y)) + { + bfsQueue.push({x, y, d + 1}); + visited[x].insert(y); + } + x = min(B, a + b); + y = max((a + b - B), 0); + if (x != b && y != a && !isVisited(y, x)) + { + bfsQueue.push({y, x, d + 1}); + visited[y].insert(x); + } + } + + cout << "-1\n"; + + return 0; +} From 8f8aa27c43f9130c00de4885246acf7528abe1fd Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 3 Apr 2021 15:08:59 +0900 Subject: [PATCH 103/203] 210403.java --- programmers/210403.java | 102 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 programmers/210403.java diff --git a/programmers/210403.java b/programmers/210403.java new file mode 100644 index 0000000..c8c1e9a --- /dev/null +++ b/programmers/210403.java @@ -0,0 +1,102 @@ +class Solution { + + private int[] gift = new int[100001]; + + public int solution(int[] gift_cards, int[] wants) { + int answer = 0; + + for(int i=0; i 0){ + gift[wants[i]]--; + answer++; + } + } + + return gift_cards.length - answer; + } +} + +class Solution { + private boolean[] include= new boolean[16]; + + public int solve(int n, int selected, int r, int[][] needs){ + if(n == needs[0].length && selected == r){ + int count = 0; + for(int i=0; i vec[] = new ArrayList[100001]; + + private void dfs(int n, int k, int[] passenger){ + visited[n] = true; + dist[n] = k; + for(int next : vec[n]){ + if(!visited[next]){ + dfs(next, k+passenger[next-1], passenger); + } + } + } + + public int[] solution(int n, int[] passenger, int[][] train) { + int[] answer = {0, 0}; + + for(int i=1; i<=n; i++){ + vec[i] = new ArrayList(); + visited[i] = false; + } + for(int i=0; i= maxcount){ + maxcount = dist[i]; + maxi = i; + } + } + answer[0] = maxi; + answer[1] = maxcount; + + return answer; + } + +} From d9a10b59c6fe23f4c41702bc005b85b14c93401f Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 3 Apr 2021 15:50:57 +0900 Subject: [PATCH 104/203] =?UTF-8?q?=EC=A0=81=EB=A1=9D=EC=83=89=EC=95=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../10026.cpp" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 "baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/10026.cpp" diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/10026.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/10026.cpp" new file mode 100644 index 0000000..47a23e7 --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/10026.cpp" @@ -0,0 +1,29 @@ +#include +using namespace std; +int N; +string b[100]; +bool v[2][100][100]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; +#define f for(i=0;i=N||ny>=N||v[n][nx][ny]) continue; + if(b[i][j]==b[nx][ny]||(n==1&&(b[i][j]=='R'||b[i][j]=='G')&&(b[nx][ny]=='R'||b[nx][ny]=='G')))dfs(nx,ny,n); + } +} +int main(){ + cin>>N; + int i,j,k,c[2]={0,0}; + f cin>>b[i];} + f for(j=0;j Date: Sat, 3 Apr 2021 17:00:51 +0900 Subject: [PATCH 105/203] Create 21040302.java --- programmers/21040302.java | 125 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 programmers/21040302.java diff --git a/programmers/21040302.java b/programmers/21040302.java new file mode 100644 index 0000000..b6baff5 --- /dev/null +++ b/programmers/21040302.java @@ -0,0 +1,125 @@ +class Solution { + + public int[] solution(int[] lottos, int[] win_nums) { + int[] answer = {0,0}; + int c0 = 0, right = 0, wrong = 0; + for(int i=0; i<6; i++){ + boolean flag = false; + for(int j=0; j<6; j++){ + if(lottos[i] == win_nums[j]){ + flag = true; + break; + } + } + if(lottos[i] == 0) c0++; + else if(flag) right++; + else wrong++; + } + + answer[0] = right + c0; + answer[1] = right; + + for(int i=0; i<2; i++){ + if(answer[i] == 6) answer[i] = 1; + else if(answer[i] == 5) answer[i] = 2; + else if(answer[i] == 4) answer[i] = 3; + else if(answer[i] == 3) answer[i] = 4; + else if(answer[i] == 2) answer[i] = 5; + else answer[i] = 6; + } + return answer; + } +} + +class Solution { + public int[] solution(int rows, int columns, int[][] queries) { + int[] answer = new int[queries.length]; + int[][] board = new int[rows][]; + for(int i=0; i=y1-1; y--){ + temp[x2-1][y] = board[x2-1][y+1]; + } + for(int x=x2-2; x>=x1-1; x--){ + temp[x][y1-1] = board[x+1][y1-1]; + } + + int a = 100000000; + for(int x=0; x name; + + void check(int n, int money){ + if(n==-1) return; + if(money < 10){ + answer[n] += money; + } + else{ + int temp = (int)(money*0.1); + answer[n] += money - temp; + check(p[n], temp); + } + } + + public int[] solution(String[] enroll, String[] referral, String[] seller, int[] amount) { + answer = new int[enroll.length]; + p = new int[enroll.length]; + name = new HashMap<>(); + + name.put("-", -1); + for(int i=0; i Date: Mon, 5 Apr 2021 19:33:05 +0900 Subject: [PATCH 106/203] =?UTF-8?q?=EB=B0=B0=EC=97=B4=20=EB=8F=8C=EB=A6=AC?= =?UTF-8?q?=EA=B8=B0=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/16935.cpp" | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/16935.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/16935.cpp" "b/baekjoon/\352\265\254\355\230\204/16935.cpp" new file mode 100644 index 0000000..e1718d2 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/16935.cpp" @@ -0,0 +1,155 @@ +#include + +using namespace std; +int N, M, R; +int A[100][100]; + +void turn(int n) +{ + int temp[100][100]; + if (n == 1) + { + for (int i = 0; i < M; i++) + { + for (int j = 0; j < N; j++) + { + temp[N - 1 - j][i] = A[j][i]; + } + } + } + else if (n == 2) + { + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + temp[i][M - 1 - j] = A[i][j]; + } + } + } + else if (n == 3) + { + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + temp[j][N - i - 1] = A[i][j]; + } + } + swap(N, M); + } + else if (n == 4) + { + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + temp[M - 1 - j][i] = A[i][j]; + } + } + swap(N, M); + } + else if (n == 5) + { + for (int i = 0; i < N / 2; i++) + { + for (int j = 0; j < M / 2; j++) + { + temp[i][j] = A[i + N / 2][j]; + } + } + for (int i = 0; i < N / 2; i++) + { + for (int j = M / 2; j < M; j++) + { + temp[i][j] = A[i][j - M / 2]; + } + } + for (int i = N / 2; i < N; i++) + { + for (int j = 0; j < M / 2; j++) + { + temp[i][j] = A[i][j + M / 2]; + } + } + for (int i = N / 2; i < N; i++) + { + for (int j = M / 2; j < M; j++) + { + temp[i][j] = A[i - N / 2][j]; + } + } + } + else + { + for (int i = 0; i < N / 2; i++) + { + for (int j = 0; j < M / 2; j++) + { + temp[i][j] = A[i][j + M / 2]; + } + } + for (int i = 0; i < N / 2; i++) + { + for (int j = M / 2; j < M; j++) + { + temp[i][j] = A[i + N / 2][j]; + } + } + for (int i = N / 2; i < N; i++) + { + for (int j = 0; j < M / 2; j++) + { + temp[i][j] = A[i - N / 2][j]; + } + } + for (int i = N / 2; i < N; i++) + { + for (int j = M / 2; j < M; j++) + { + temp[i][j] = A[i][j - M / 2]; + } + } + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + A[i][j] = temp[i][j]; + } + } +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> R; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cin >> A[i][j]; + } + } + + for (int i = 0; i < R; i++) + { + int x; + cin >> x; + turn(x); + } + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cout << A[i][j] << " "; + } + cout << "\n"; + } + + return 0; +} From 1eae22513c8d3c3066fb3f76e5febd1dbb9bae49 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 6 Apr 2021 15:54:59 +0900 Subject: [PATCH 107/203] =?UTF-8?q?=EB=A1=9C=EB=B4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1726.cpp" | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1726.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1726.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1726.cpp" new file mode 100644 index 0000000..d3aa6ae --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1726.cpp" @@ -0,0 +1,84 @@ +#include +#include + +using namespace std; + +struct node +{ + int x, y, dir, d; +}; + +int N, M; +bool wall[100][100]; +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +queue bfsQueue; +int sx, sy, sdir, ex, ey, edir; +bool visited[100][100][4]; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + cin >> wall[i][j]; + } + } + cin >> sx >> sy >> sdir >> ex >> ey >> edir; + + ex--; + ey--; + edir--; + bfsQueue.push({sx - 1, sy - 1, sdir - 1, 0}); + visited[sx][sy][sdir] = true; + + while (!bfsQueue.empty()) + { + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int dir = bfsQueue.front().dir; + int d = bfsQueue.front().d; + bfsQueue.pop(); + + if (x == ex && y == ey && dir == edir) + { + cout << d << "\n"; + break; + } + + for (int i = 1; i <= 3; i++) + { + int nx = x + dx[dir] * i; + int ny = y + dy[dir] * i; + if (nx >= 0 && ny >= 0 && nx < N && ny < M) + { + if (wall[nx][ny]) + break; + + if (!visited[nx][ny][dir]) + { + bfsQueue.push({nx, ny, dir, d + 1}); + visited[nx][ny][dir] = true; + } + } + } + + if (!visited[x][y][(dir + 2) % 4]) + { + bfsQueue.push({x, y, (dir + 2) % 4, d + 1}); + visited[x][y][(dir + 2) % 4] = true; + } + if (!visited[x][y][(dir + 3 - dir % 2 * 2) % 4]) + { + bfsQueue.push({x, y, (dir + 3 - dir % 2 * 2) % 4, d + 1}); + visited[x][y][(dir + 3 - dir % 2 * 2) % 4] = true; + } + } + return 0; +} From 9a5346c4f0aa3af47bd1f46341478581dddcafa2 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 6 Apr 2021 16:41:40 +0900 Subject: [PATCH 108/203] =?UTF-8?q?=EC=9D=8C=EC=A3=BC=20=EC=BD=94=EB=94=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5676.cpp" | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 "baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/5676.cpp" diff --git "a/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/5676.cpp" "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/5676.cpp" new file mode 100644 index 0000000..0254d9b --- /dev/null +++ "b/baekjoon/\354\204\270\352\267\270\353\250\274\355\212\270\355\212\270\353\246\254/5676.cpp" @@ -0,0 +1,95 @@ +#include +#include + +using namespace std; + +int N, K; +int A[100001]; +int seg[400002]; + +int get101(int n) +{ + if (n > 0) + return 1; + else if (n < 0) + return -1; + return 0; +} + +int init(int node, int left, int right) +{ + if (left > N) + return 1; + if (left == right) + return seg[node] = get101(A[left]); + + int middle = (left + right) / 2; + return seg[node] = init(node * 2, left, middle) * init(node * 2 + 1, middle + 1, right); +} + +int change(int node, int left, int right, int index, int n) +{ + if (left > index || right < index) + return seg[node]; + + if (left == right) + { + return seg[node] = get101(n); + } + + int middle = (left + right) / 2; + return seg[node] = change(node * 2, left, middle, index, n) * change(node * 2 + 1, middle + 1, right, index, n); +} + +int getseg(int node, int left, int right, int start, int end) +{ + if (left > end || right < start) + return 1; + if (left >= start && right <= end) + return seg[node]; + int middle = (left + right) / 2; + return getseg(node * 2, left, middle, start, end) * getseg(node * 2 + 1, middle + 1, right, start, end); +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + int x, y; + + while (cin >> N >> K) + { + for (int i = 1; i <= N; i++) + { + cin >> A[i]; + } + + init(1, 1, pow(2, ceil(log2(N)))); + + for (int i = 0; i < K; i++) + { + char c; + cin >> c; + if (c == 'C') + { + cin >> x >> y; + change(1, 1, pow(2, ceil(log2(N))), x, y); + } + else + { + cin >> x >> y; + int a = getseg(1, 1, pow(2, ceil(log2(N))), x, y); + if (a == 1) + cout << "+"; + else if (a == 0) + cout << "0"; + else + cout << "-"; + } + } + + cout << "\n"; + } + + return 0; +} From 36c78833299b5bae1f0d503e54d90f3ba01a03d7 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 8 Apr 2021 12:32:14 +0900 Subject: [PATCH 109/203] =?UTF-8?q?=EB=BF=8C=EC=9A=94=EB=BF=8C=EC=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../11559.cpp" | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 "baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/11559.cpp" diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/11559.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/11559.cpp" new file mode 100644 index 0000000..efcca85 --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/11559.cpp" @@ -0,0 +1,123 @@ +#include + +using namespace std; + +string board[12]; +bool visited[12][6]; +int check[12][6]; + +void initvisit() +{ + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < 6; j++) + { + visited[i][j] = false; + check[i][j] = 0; + } + } +} + +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +int dfs(int i, int j, int n) +{ + visited[i][j] = true; + check[i][j] = n; + + int res = 1; + + for (int d = 0; d < 4; d++) + { + int nx = i + dx[d]; + int ny = j + dy[d]; + if (nx >= 0 && ny >= 0 && nx < 12 && ny < 6 && !visited[nx][ny] && board[i][j] == board[nx][ny]) + { + res += dfs(nx, ny, n); + } + } + + return res; +} + +void gravity() +{ + for (int i = 11; i >= 0; i--) + { + for (int j = 0; j < 6; j++) + { + if (board[i][j] == '.') + { + for (int k = i - 1; k >= 0; k--) + { + if (board[k][j] != '.') + { + board[i][j] = board[k][j]; + board[k][j] = '.'; + break; + } + } + } + } + } +} + +int ans = 0; + +void boom(int n) +{ + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < 6; j++) + { + if (check[i][j] == n) + { + board[i][j] = '.'; + visited[i][j] = false; + check[i][j] = 0; + } + } + } +} + +int main() +{ + for (int i = 0; i < 12; i++) + { + cin >> board[i]; + } + + int n; + bool flag; + + do + { + flag = false; + n = 1; + initvisit(); + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < 6; j++) + { + if (board[i][j] != '.' && !visited[i][j]) + { + int a = dfs(i, j, n); + if (a > 3) + { + boom(n); + flag = true; + } + n++; + } + } + } + if (flag) + ans++; + gravity(); + } while (flag == true); + + cout << ans << "\n"; + + return 0; +} From 5001436fc6a597336b9e7e93c646c67735c6eb91 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 8 Apr 2021 13:04:11 +0900 Subject: [PATCH 110/203] =?UTF-8?q?=ED=85=80=20=ED=94=84=EB=A1=9C=EC=A0=9D?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../9466.cpp" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/9466.cpp" diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/9466.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/9466.cpp" new file mode 100644 index 0000000..2601226 --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/9466.cpp" @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int T, N; +int p[100001]; +bool visited[100001]; +bool done[100001]; +int ans; + +void dfs(int n) +{ + visited[n] = true; + + int next = p[n]; + if (!visited[next]) + { + dfs(next); + } + else if (!done[next]) + { + for (int i = next; i != n; i = p[i]) + { + ans++; + } + ans++; + } + done[n] = true; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> T; + for (int t = 0; t < T; t++) + { + ans = 0; + cin >> N; + for (int i = 1; i <= N; i++) + { + cin >> p[i]; + visited[i] = false; + done[i] = false; + } + + for (int i = 1; i <= N; i++) + { + if (!visited[i]) + dfs(i); + } + cout << N - ans << "\n"; + } + return 0; +} From 7ed082c27f6c1ae78d1254272179a1f493970156 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 8 Apr 2021 13:12:20 +0900 Subject: [PATCH 111/203] =?UTF-8?q?=EC=88=AB=EC=9E=90=EA=B3=A0=EB=A5=B4?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2668.cpp" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/2668.cpp" diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/2668.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/2668.cpp" new file mode 100644 index 0000000..a441e0b --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/2668.cpp" @@ -0,0 +1,49 @@ +#include +#include +#include + +using namespace std; +int N; +int A[101]; +bool visited[101]; +bool done[101]; +vector ans; + +void dfs(int n) +{ + visited[n] = true; + int next = A[n]; + if (!visited[next]) + dfs(next); + + else if (!done[next]) + { + for (int i = next; i != n; i = A[i]) + ans.push_back(i); + ans.push_back(n); + } + done[n] = true; +} + +int main() +{ + cin >> N; + for (int i = 1; i <= N; i++) + { + cin >> A[i]; + } + + for (int i = 1; i <= N; i++) + { + if (!visited[i]) + dfs(i); + } + + cout << ans.size() << "\n"; + sort(ans.begin(), ans.end()); + for (int a : ans) + { + cout << a << "\n"; + } + return 0; +} From 5f4caffa6d02a99d70dd8b54fe1c36c5de1e1f69 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 9 Apr 2021 22:52:54 +0900 Subject: [PATCH 112/203] =?UTF-8?q?0=20=EB=A7=8C=EB=93=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/7490.cpp" | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/7490.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/7490.cpp" "b/baekjoon/\352\265\254\355\230\204/7490.cpp" new file mode 100644 index 0000000..7224864 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/7490.cpp" @@ -0,0 +1,55 @@ +#include +#include + +using namespace std; + +int T, N; + +void solve(int n, string s) +{ + if (n == N) + { + int a = 0, pre = 0; + char c = '+'; + for (int i = 0; i < s.length(); i++) + { + if (s[i] >= '0' && s[i] <= '9') + { + pre = pre * 10 + s[i] - '0'; + } + else if (s[i] != ' ') + { + if (c == '+') + a = a + pre; + else + a = a - pre; + + c = s[i]; + pre = 0; + } + } + if (c == '+') + a = a + pre; + else + a = a - pre; + if (a == 0) + cout << s << "\n"; + + return; + } + solve(n + 1, s + " " + to_string(n + 1)); + solve(n + 1, s + "+" + to_string(n + 1)); + solve(n + 1, s + "-" + to_string(n + 1)); +} + +int main() +{ + cin >> T; + for (int t = 0; t < T; t++) + { + cin >> N; + solve(1, "1"); + cout << "\n"; + } + return 0; +} From 15f165ac0a687a3e568d3857270550643f26a4a9 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 10 Apr 2021 22:03:00 +0900 Subject: [PATCH 113/203] =?UTF-8?q?=ED=8A=B8=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../4803.cpp" | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 "baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/4803.cpp" diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/4803.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/4803.cpp" new file mode 100644 index 0000000..bdd188b --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/4803.cpp" @@ -0,0 +1,72 @@ +#include +#include + +using namespace std; +bool visited[501]; +int N, M; +vector adj[501]; + +bool dfs(int n, int pre) +{ + visited[n] = true; + + for (int next : adj[n]) + { + if (pre == next) + continue; + + if (visited[next]) + return false; + if (dfs(next, n) == false) + return false; + } + + return true; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + int t = 0; + while (true) + { + t++; + int tree = 0; + cin >> N >> M; + if (N == 0 && M == 0) + break; + + for (int i = 1; i <= N; i++) + { + adj[i].clear(); + visited[i] = false; + } + + int x, y; + for (int i = 0; i < M; i++) + { + cin >> x >> y; + adj[x].push_back(y); + adj[y].push_back(x); + } + + for (int i = 1; i <= N; i++) + { + if (!visited[i]) + { + if (dfs(i, 0)) + tree++; + } + } + cout << "Case " << t << ": "; + if (tree == 0) + cout << "No trees.\n"; + else if (tree == 1) + cout << "There is one tree.\n"; + else + cout << "A forest of " << tree << " trees.\n"; + } + return 0; +} From 664e6147a7aa8c2ea0214337267681df5824d38e Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 11 Apr 2021 13:38:24 +0900 Subject: [PATCH 114/203] =?UTF-8?q?=EC=86=8C=EC=88=98&=ED=8C=B0=EB=A6=B0?= =?UTF-8?q?=EB=93=9C=EB=A1=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1747.cpp" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/1747.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/1747.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/1747.cpp" new file mode 100644 index 0000000..ef3c09a --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/1747.cpp" @@ -0,0 +1,54 @@ +#include +#include + +using namespace std; + +bool notPrime[1003002]; + +void goprime() +{ + notPrime[1] = true; + for (long long i = 2; i <= 500000; i++) + { + if (notPrime[i]) + continue; + for (long long j = 2; i * j <= (long long)1003001; j++) + { + notPrime[i * j] = true; + } + } +} + +bool pelindrum(string s, int n) +{ + if (n == 1 || n == 0) + return true; + + if (s[0] == s[n - 1]) + return pelindrum(s.substr(1, n - 2), n - 2); + else + return false; +} + +int main() +{ + goprime(); + + int N; + cin >> N; + + for (int i = N; i <= 1003001; i++) + { + if (notPrime[i]) + continue; + + string s = to_string(i); + if (pelindrum(s, s.length())) + { + cout << i << "\n"; + break; + } + } + + return 0; +} From 67ab60afdcdbfc792007878935eaf9746322afbf Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 11 Apr 2021 14:18:24 +0900 Subject: [PATCH 115/203] =?UTF-8?q?=EA=B4=84=ED=98=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2800" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/2800" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/2800" "b/baekjoon/\353\254\270\354\236\220\354\227\264/2800" new file mode 100644 index 0000000..c23987b --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/2800" @@ -0,0 +1,80 @@ +#include +#include +#include + +using namespace std; + +vector stack; +vector ans; +int par[201]; +string s; +int include[201]; + +void solve(int index, int n) +{ + if (index == s.length()) + { + if (n > 0) + { + string temp = ""; + for (int i = 0; i < s.length(); i++) + { + if (include[i]) + temp += s[i]; + } + ans.push_back(temp); + } + return; + } + + if (s[index] == '(') + { + include[index] = true; + include[par[index]] = true; + solve(index + 1, n); + include[index] = false; + include[par[index]] = false; + solve(index + 1, n + 1); + } + else if (s[index] != ')') + { + include[index] = true; + solve(index + 1, n); + } + else + { + include[index] = include[par[index]]; + solve(index + 1, n); + } +} + +int main() +{ + cin >> s; + fill(par, par + s.length(), -1); + + for (int i = 0; i < s.length(); i++) + { + if (s[i] == '(') + { + stack.push_back(i); + } + else if (s[i] == ')') + { + int t = stack.back(); + stack.pop_back(); + par[t] = i; + par[i] = t; + } + } + + solve(0, 0); + sort(ans.begin(), ans.end()); + ans.erase(unique(ans.begin(), ans.end()), ans.end()); + + for (string a : ans) + { + cout << a << "\n"; + } + return 0; +} From 343c709b800cbf7a1094df07696c6abb1ce6728d Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 12 Apr 2021 11:29:17 +0900 Subject: [PATCH 116/203] =?UTF-8?q?=EC=A4=91=EC=95=99=EA=B0=92=20=EA=B5=AC?= =?UTF-8?q?=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2696.cpp" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 "baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2696.cpp" diff --git "a/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2696.cpp" "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2696.cpp" new file mode 100644 index 0000000..ea1e96e --- /dev/null +++ "b/baekjoon/\354\232\260\354\204\240\354\210\234\354\234\204\355\201\220/2696.cpp" @@ -0,0 +1,59 @@ +#include +#include +#include + +using namespace std; +int T, N; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int t=0;t> N; + priority_queue maxHeap; + priority_queue minHeap; + vector ans; + + int x; + + for(int i=1;i<=N;i++){ + cin >> x; + if(maxHeap.empty()){ + maxHeap.push(x); + } + else if(maxHeap.size()>minHeap.size()){ + int middle = maxHeap.top(); + if(x < middle){ + maxHeap.pop(); + maxHeap.push(x); + minHeap.push(-middle); + } + else{ + minHeap.push(-x); + } + } + else{ + int maxmiddle = maxHeap.top(); + int minmiddle = -minHeap.top(); + + if(x < minmiddle){ + maxHeap.push(x); + }else{ + minHeap.pop(); + maxHeap.push(minmiddle); + minHeap.push(-x); + } + } + if(i%2==1) ans.push_back(maxHeap.top()); + } + cout << ans.size() << "\n"; + for(int a :ans){ + cout << a << " "; + } + cout << "\n"; + } + + return 0; +} From e68fce9807c8529397bc2405c7f9017dc9ca3932 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 13 Apr 2021 17:21:30 +0900 Subject: [PATCH 117/203] =?UTF-8?q?=EA=B0=9C=EA=B7=BC=EC=83=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/1563.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 baekjoon/DP/1563.cpp diff --git a/baekjoon/DP/1563.cpp b/baekjoon/DP/1563.cpp new file mode 100644 index 0000000..4abf4f1 --- /dev/null +++ b/baekjoon/DP/1563.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int N; +bool visited[1001][5][5]; +int D[1001][5][5]; + +// 출석 0 지각 1 결석 2 + +int solve(int n, int l, int a){ + if(visited[n][l][a]) return D[n][l][a]; + + visited[n][l][a] = true; + + if(n == 1){ + if(l+a >= 2) return D[1][l][a] = 0; + return D[1][l][a] = 1; + } + + int res = 0; + + if(a>0){ + res += solve(n-1, l, a-1); + }else{ + for(int i=0;i<=2; i++){ + res += solve(n-1, l, i); + } + if(l>0){ + for(int i=0;i<=2;i++){ + res += solve(n-1, l-1, i); + } + } + } + + return D[n][l][a] = res % 1000000; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + cout << (solve(N, 0, 0) + solve(N, 1, 0) + solve(N, 0, 1) + solve(N, 0, 2) + solve(N, 1, 1) + solve(N,1,2))%1000000<< "\n"; + + return 0; +} From cadcca07187746a74c8e7b799b90ca2dce8c3199 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 14 Apr 2021 16:34:06 +0900 Subject: [PATCH 118/203] =?UTF-8?q?=EB=8F=99=EC=A0=84=20=EB=B0=94=EA=BF=94?= =?UTF-8?q?=EC=A3=BC=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2624.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 baekjoon/DP/2624.cpp diff --git a/baekjoon/DP/2624.cpp b/baekjoon/DP/2624.cpp new file mode 100644 index 0000000..c6d0dba --- /dev/null +++ b/baekjoon/DP/2624.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int A[100][2]; +long long D[100][10001]; + +int main() +{ + int T, N; + cin >> T >> N; + + for(int i=0;i> A[i][0] >> A[i][1]; + } + + for(int k=0; k<=A[0][1]; k++){ + if(A[0][0]*k > T) break; + D[0][A[0][0]*k] = 1; + } + + for(int i=1;i T) break; + D[i][j+A[i][0]*k] += D[i-1][j]; + } + } + } + + cout << D[N-1][T] << "\n"; + + return 0; +} From d8164396b2e953644aa10538d8744a069c118f60 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 15 Apr 2021 12:54:48 +0900 Subject: [PATCH 119/203] =?UTF-8?q?=EA=B4=84=ED=98=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/10422.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 baekjoon/DP/10422.cpp diff --git a/baekjoon/DP/10422.cpp b/baekjoon/DP/10422.cpp new file mode 100644 index 0000000..47851b2 --- /dev/null +++ b/baekjoon/DP/10422.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int T, L; +long long D[5001]; + +int solve(int n){ + if(D[n]!=0 || n%2==1) return D[n]; + + for(int y = 2; y<= n; y+= 2){ + D[n] = (D[n] + (long long)solve(y-2)*solve(n-y)) % 1000000007; + } + return D[n]; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + + int i, j; + + D[0]=1; + D[2]=1; + + for (i = 0; i < T; i++) + { + cin >> L; + cout << solve(L) << "\n"; + } + return 0; +} From 1813c1b64fbaefeefcb5454ea8c16426c15f4acb Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 18 Apr 2021 13:49:01 +0900 Subject: [PATCH 120/203] =?UTF-8?q?=EC=98=A5=EC=83=81=20=EC=A0=95=EC=9B=90?= =?UTF-8?q?=20=EA=BE=B8=EB=AF=B8=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\354\212\244\355\203\235/6198.cpp" | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "baekjoon/\354\212\244\355\203\235/6198.cpp" diff --git "a/baekjoon/\354\212\244\355\203\235/6198.cpp" "b/baekjoon/\354\212\244\355\203\235/6198.cpp" new file mode 100644 index 0000000..83806b9 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/6198.cpp" @@ -0,0 +1,54 @@ +#include +#include + +using namespace std; + +int N; +vector > stack; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + long long ans = 0; + + for (int i = 0; i < N; i++) + { + int x; + cin >> x; + if (stack.empty()) + { + stack.push_back(make_pair(x, 0)); + } + else if (stack.back().first > x) + { + stack.push_back(make_pair(x, 0)); + } + else + { + while(!stack.empty() && stack.back().first <= x){ + pair d = stack.back(); + ans += d.second; + stack.pop_back(); + if(!stack.empty()){ + stack.back().second += d.second + 1; + } + } + stack.push_back(make_pair(x, 0)); + } + } + + while(!stack.empty()){ + pair d = stack.back(); + ans += d.second; + stack.pop_back(); + if(!stack.empty()){ + stack.back().second += d.second + 1; + } + } + + cout << ans << "\n"; + return 0; +} From b4ab69bacf91cb131944a5609b585085699efa9e Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 19 Apr 2021 12:06:51 +0900 Subject: [PATCH 121/203] =?UTF-8?q?=EB=8F=8C=EB=8B=A4=EB=A6=AC=20=EA=B1=B4?= =?UTF-8?q?=EB=84=88=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2602.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 baekjoon/DP/2602.cpp diff --git a/baekjoon/DP/2602.cpp b/baekjoon/DP/2602.cpp new file mode 100644 index 0000000..d3d6814 --- /dev/null +++ b/baekjoon/DP/2602.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +string target; +string dari[2]; +int DP[2][101][21]; + +long long solve(int row, int col, int idx){ + + //cout << row << col << idx <<"\n"; + + if(idx == target.length()-1){ + return 1; + } + + if(DP[row][col][idx] != -1) return DP[row][col][idx]; + + int nrow = 1 - row; + + long long res = 0; + for(int j=col+1; j> target >> dari[0] >> dari[1]; + + for(int i=0;i<2;i++){ + for(int j=0;j Date: Tue, 20 Apr 2021 21:19:11 +0900 Subject: [PATCH 122/203] =?UTF-8?q?=EB=A1=9C=EB=98=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2758.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 baekjoon/DP/2758.cpp diff --git a/baekjoon/DP/2758.cpp b/baekjoon/DP/2758.cpp new file mode 100644 index 0000000..504791f --- /dev/null +++ b/baekjoon/DP/2758.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int T, N, M; +long long D[15][2001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int i=0;i> N >> M; + for(int x=1; x<=M; x++) D[1][x] = 1; + for(int x=2; x<=N; x++){ + for(int y=1; y<=M; y++){ + D[x][y] = 0; + for(int k=1;k<=y/2;k++){ + D[x][y] += D[x-1][k]; + } + } + } + long long ans = 0; + for(int y=1; y<=M; y++){ + ans += D[N][y]; + } + cout << ans << "\n"; + } + + return 0; +} From f5a5598845ace09b4fb0194c05e516baca97d0de Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 22 Apr 2021 21:41:47 +0900 Subject: [PATCH 123/203] =?UTF-8?q?=ED=81=AC=EB=A6=AC=EB=B3=B4=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/11058.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 baekjoon/DP/11058.cpp diff --git a/baekjoon/DP/11058.cpp b/baekjoon/DP/11058.cpp new file mode 100644 index 0000000..1b29f57 --- /dev/null +++ b/baekjoon/DP/11058.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +long long D[105]; +int N; + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + D[1]=1;D[2]=2;D[3]=3; + + for(int i=3; i<=N; i++){ + D[i] = D[i-1]+1; + for(int j=1; j<=i-3-1; j++){ + D[i] = max(D[i], D[i-3-j]*(2+j)); + } + } + + cout << D[N]<<"\n"; + + return 0; +} From 27a74d1ac9bc51a0655f6d750db26959adcbce6d Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 24 Apr 2021 18:46:55 +0900 Subject: [PATCH 124/203] AC --- .../5430.cpp" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/5430.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/5430.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/5430.cpp" new file mode 100644 index 0000000..54dfba4 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/5430.cpp" @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +using namespace std; + +int T, N; +string command; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int t=0;t> command; + cin >> N; + string tmp; + cin >> tmp; + list mylist; + + int key = 0; + + for(int i=1; tmp[i] != ']'; i++){ + if(tmp[i] >= '0' && tmp[i] <= '9'){ + key = key*10 + tmp[i]-'0'; + } + else if(tmp[i] == ','){ + mylist.push_back(key); + key = 0; + } + } + + if(key!=0) mylist.push_back(key); + + for(int i=0; i::iterator it; + if(!reverse){ + it = mylist.begin(); + } + else{ + it = mylist.end(); + it--; + } + it = mylist.erase(it); + } + } + + if(!flag) continue; + vector ans; + for (int const &c: mylist) { + ans.push_back(c); + } + + cout << "["; + if(!reverse){ + for(int i=0; i=0; i--){ + cout << ans[i]; + if(i!=0) cout << ","; + } + } + cout << "]\n"; + } + return 0; +} From 6d32ddb1935a2637f527e9e526b43ce7c0c4aeca Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 25 Apr 2021 13:36:05 +0900 Subject: [PATCH 125/203] Contact --- .../1013.cpp" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/1013.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/1013.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/1013.cpp" new file mode 100644 index 0000000..2bc1ea1 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/1013.cpp" @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int main(){ + int N; + string str; + + cin >> N; + for(int i=0; i> str; + + while(str.length() > 1){ + cout << str << "\n"; + if(str.substr(str.length()-2, 2) == "01" && (str.length() == 2 || str[str.length()-3] == '1')){ + str = str.substr(0, str.length() - 2); + } + else{ + bool start = false, end = false; + int count = 0; + int idx = str.length()-1; + + while(idx>=0 && str[idx] == '1'){ + idx--; + end = true; + } + + for(; idx>=0; idx--){ + if(str[idx] == '0') count++; + else break; + } + + if(idx>=0 && str[idx] == '1') start = true; + if(start && end && count >=2){ + str = str.substr(0, idx); + } + else break; + } + } + + if(str.length() > 0){ + cout << "NO\n"; + } + else{ + cout << "YES\n"; + } + } + + return 0; +} From b8eea64085b89f54d0dcc73e2512d736cf7051a0 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 28 Apr 2021 13:33:59 +0900 Subject: [PATCH 126/203] =?UTF-8?q?=EB=AA=A9=EC=9E=A5=20=EA=B1=B4=EC=84=A4?= =?UTF-8?q?=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/14925.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 baekjoon/DP/14925.cpp diff --git a/baekjoon/DP/14925.cpp b/baekjoon/DP/14925.cpp new file mode 100644 index 0000000..952209e --- /dev/null +++ b/baekjoon/DP/14925.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int N, M; +int board[1000][1000]; +int D[1000][1000]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + + for(int i=0; i> board[i][j]; + if(board[i][j]==0) D[i][j] = 1; + } + } + + for(int i=1; i Date: Sat, 1 May 2021 22:05:45 +0900 Subject: [PATCH 127/203] =?UTF-8?q?=EC=83=81=EB=B2=94=20=EB=B9=8C=EB=94=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../6593.cpp" | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6593.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6593.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6593.cpp" new file mode 100644 index 0000000..c837b50 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6593.cpp" @@ -0,0 +1,80 @@ +#include +#include + +using namespace std; + +struct node{ + int x, y, z, d; +}; + +int L, R, C; +string board[30][30]; +bool visited[30][30][30]; +int startL, startR, startC; +int dx[6] = {0,0,0,0,1,-1}; +int dy[6] = {0,0,1,-1,0,0}; +int dz[6] = {1,-1,0,0,0,0}; + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + string tmp; + + while(true){ + cin >> L >> R >> C; + + if(L==0 && R==0 && C==0) break; + + for(int i=0; i> board[i][j]; + for(int k=0; k bfsQueue; + bfsQueue.push({startL, startR, startC, 0}); + visited[startL][startR][startC] = true; + + bool flag = false; + + while(!bfsQueue.empty()){ + int x = bfsQueue.front().x; + int y = bfsQueue.front().y; + int z = bfsQueue.front().z; + int d = bfsQueue.front().d; + bfsQueue.pop(); + + if(board[x][y][z] == 'E'){ + flag = true; + cout << "Escaped in " << d << " minute(s).\n"; + break; + } + + for(int i=0; i<6; i++){ + int nx = x + dx[i]; + int ny = y + dy[i]; + int nz = z + dz[i]; + + if(nx >=0 && ny >= 0 && nz >= 0 && nx < L && ny < R && nz < C && board[nx][ny][nz] != '#' && !visited[nx][ny][nz]){ + visited[nx][ny][nz] = true; + bfsQueue.push({nx, ny, nz, d+1}); + } + } + } + if(!flag){ + cout << "Trapped!\n"; + } + } + + return 0; +} From 3f7c41525a768aade6b7b509d0a02e5c231eb403 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 3 May 2021 20:10:38 +0900 Subject: [PATCH 128/203] Update 8980.cpp --- .../8980.cpp" | 96 ++++++++----------- 1 file changed, 42 insertions(+), 54 deletions(-) diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" index a28d03a..7ac04a6 100644 --- "a/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/8980.cpp" @@ -1,65 +1,53 @@ #include -#include - +#include +#include using namespace std; - -typedef struct node -{ - int end, cost; - - bool operator<(const node &a) const - { - if (end == a.end) - return cost < a.cost; - return end > a.end; +class Delivery { +public: + int from; + int to; + int cnt; + Delivery(int from, int to, int cnt) { + this->from = from; + this->to = to; + this->cnt = cnt; } -} node; - -int N, C, M; -priority_queue pq[2001]; - -int main() -{ - ios::sync_with_stdio(0); - cin.tie(0); - - cin >> N >> C >> M; - - int x, y, z; - - for (int i = 0; i < M; i++) - { - cin >> x >> y >> z; - pq[x].push({y, z}); +}; + +bool cmp(Delivery a, Delivery b) { + if (a.to < b.to) return true; + else return false; +} + +int main(void) { + int n, c, m; + cin >> n >> c >> m; + vector list; + vector left(n + 1, c); + int box_cnt = 0; + for (int i = 0; i < m; i++) { + int from, to, cnt; + cin >> from >> to >> cnt; + list.push_back(Delivery(from, to, cnt)); } + sort(list.begin(), list.end(), cmp); + + for (auto d : list) { - int ans = 0; - int now = 0; - - priority_queue queue; - - for (int i = 1; i <= N; i++) - { - while (!queue.empty() && queue.top().end == i) - { - now -= queue.top().cost; - ans += queue.top().cost; - queue.pop(); + int min = left[d.from]; + for (int i = d.from + 1; i < d.to; i++) { + if (min > left[i]) min = left[i]; } - while (!pq[i].empty() && now < C) - { - int e = pq[i].top().end; - int c = pq[i].top().cost; - - int k = min(c, C - now); - now += k; - pq[i].pop(); - queue.push({e, k}); - cout << i << " " << e << " " << k << " " << ans << " " << now << "\n"; + int cnt = d.cnt; + if (min < cnt) cnt = min; + + box_cnt += cnt; + + for (int i = d.from; i < d.to; i++) { + left[i] -= cnt; } } - - cout << ans << "\n"; + cout << box_cnt; return 0; } From 9efaee97fc38487b8dbe0417dd383a608b48d768 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 4 May 2021 13:00:12 +0900 Subject: [PATCH 129/203] =?UTF-8?q?=EC=A3=BC=EC=82=AC=EC=9C=84=20=EC=8C=93?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2116.cpp" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2116.cpp" diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2116.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2116.cpp" new file mode 100644 index 0000000..a8cfe1b --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2116.cpp" @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int A[10000][6]; +int N; + +int getbiggest(int n, int i){ + if(i==0 || i==5) return max(max(A[n][1], A[n][2]), max(A[n][3], A[n][4])); + if(i==2 || i==4) return max(max(A[n][0], A[n][1]), max(A[n][3], A[n][5])); + return max(max(A[n][0], A[n][2]), max(A[n][4], A[n][5])); +} + +int oposite[6] = {5, 3, 4, 1, 2, 0}; + +int solve(int n, int top){ + + if(n==N) return 0; + + int down; + + for(int i=0; i<6; i++){ + if(A[n-1][top] == A[n][i]){ + down = i; + break; + } + } + + return solve(n+1, oposite[down]) + getbiggest(n, oposite[down]); + +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> A[i][j]; + } + } + + int ans = 0; + + for(int i=0; i<6; i++){ + ans = max(ans, solve(1, i) + getbiggest(0, i)); + } + + cout << ans << "\n"; + + return 0; +} From dabd71ce252e7484431ab943b68ee3279882537a Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 15 May 2021 09:43:44 +0900 Subject: [PATCH 130/203] kakao internship conding test 3 --- programmers/210508.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 programmers/210508.cpp diff --git a/programmers/210508.cpp b/programmers/210508.cpp new file mode 100644 index 0000000..3a1d7c6 --- /dev/null +++ b/programmers/210508.cpp @@ -0,0 +1,86 @@ +#include +#include + +using namespace std; + +typedef struct node{ + struct node* previous; + struct node* next; + int n; + bool exist; +}node; + +node* A[1000000]; +int N; +int k; +node* now; +vector cmd; +vector stack; + +int main(){ + + cin >> N; + for(int i=0; in = i; + A[i]->exist = true; + } + + for(int i=0; inext = A[i+1]; + } + for(int i=1; iprevious = A[i-1]; + } + + A[0]->previous = A[N-1]; + A[N-1]->next = A[0]; + + cin >> k; + now = A[k]; + + for(string c:cmd){ + if(c[0] == 'U'){ + int x = atoi(c.substr(2, c.length()-2).c_str()); + while(x>0){ + now = now -> previous; + x--; + } + } + else if(c[0] == 'D'){ + int x = atoi(c.substr(2, c.length()-2).c_str()); + while(x>0){ + now = now->next; + x--; + } + } + else if(c[0] == 'C'){ + now->exist = false; + node* tmp = now->previous; + tmp->next = now->next; + node* tmp2 = now->next; + tmp2->previous = now->previous; + stack.push_back(now->n); + + if(now->n < now->next->n) + now = now->next; + else + now = now->previous; + } + else if(c[0] == 'Z'){ + int a = stack.back(); + stack.pop_back(); + + A[a]->exist = true; + A[a]->previous->next = A[a]; + A[a]->next->previous = A[a]; + } + } + + string ans = ""; + for(int i=0; iexist) ans += "O"; + else ans += "X"; + } + + return 0; +} From 26434e2cafe5f4ffed8319ff3e1eb3a369e6d42c Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 18 May 2021 13:15:55 +0900 Subject: [PATCH 131/203] =?UTF-8?q?=EC=88=98=20=EA=B3=A0=EB=A5=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2230.cpp" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/2230.cpp" diff --git "a/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/2230.cpp" "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/2230.cpp" new file mode 100644 index 0000000..cfba708 --- /dev/null +++ "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/2230.cpp" @@ -0,0 +1,42 @@ +#include +#include + +using namespace std; + +long long N, M; +long long A[100000]; + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + int ans = 2000000000; + + cin >> N >> M; + + for(int i=0; i> A[i]; + } + + sort(A, A+N); + + int left = 0, right = 0; + int now = 0; + + while(left < N && right < N && left <= right){ + + now = A[right] - A[left]; + + if(now < M){ + right++; + } + else{ + ans = min(ans, now); + left++; + } + } + + cout << ans << "\n"; + return 0; +} From 9d9a1f888bd188f379337b8ad48cf6549c0f05fd Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 31 May 2021 11:39:02 +0900 Subject: [PATCH 132/203] =?UTF-8?q?=ED=95=99=EA=B5=90=20=ED=83=90=EB=B0=A9?= =?UTF-8?q?=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../13418.cpp" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 "baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/13418.cpp" diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/13418.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/13418.cpp" new file mode 100644 index 0000000..3b3918f --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/13418.cpp" @@ -0,0 +1,71 @@ +#include +#include +#include + +using namespace std; + +int p1[1001]; +int p2[1001]; +int selected1 = 0, selected2 = 0, N, M; +long long total1 = 0, total2 = 0; +pair > edges[1000001]; + +int getp1(int n){ + if(p1[n] == n) return n; + return p1[n] = getp1(p1[n]); +} + +int getp2(int n){ + if(p2[n] == n) return n; + return p2[n] = getp2(p2[n]); +} + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=0; i<=N; i++) { + p1[i] = i; + p2[i] = i; + } + + for(int i=0; i> x >> y >> z; + edges[i] = make_pair(z, make_pair(x,y)); + } + + sort(edges, edges + M + 1); + + for(int now=M; selected1 < N; now--){ + + int d = edges[now].first; + int x = edges[now].second.first; + int y = edges[now].second.second; + + if(getp1(x) != getp1(y)){ + p1[p1[x]] = p1[y]; + if(d==0) total1++; + selected1++; + } + } + + for(int now=0; selected2 < N; now++){ + + int d = edges[now].first; + int x = edges[now].second.first; + int y = edges[now].second.second; + + if(getp2(x) != getp2(y)){ + p2[p2[x]] = p2[y]; + if(d==0) total2++; + selected2++; + } + } + + cout << -total1*total1 + total2*total2 << "\n"; + + return 0; +} From fda4b557e9a0051b99d70ae72f717648a5e4ef99 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 31 May 2021 16:17:51 +0900 Subject: [PATCH 133/203] =?UTF-8?q?=EC=86=90=EC=BD=94=EB=94=A9=20=EC=97=B0?= =?UTF-8?q?=EC=8A=B5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dec_to_oct.cpp" | 11 +++++++ .../non_repeatable.cpp" | 30 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 "\352\270\260\354\210\240\353\251\264\354\240\221/dec_to_oct.cpp" create mode 100644 "\352\270\260\354\210\240\353\251\264\354\240\221/non_repeatable.cpp" diff --git "a/\352\270\260\354\210\240\353\251\264\354\240\221/dec_to_oct.cpp" "b/\352\270\260\354\210\240\353\251\264\354\240\221/dec_to_oct.cpp" new file mode 100644 index 0000000..8aa2a93 --- /dev/null +++ "b/\352\270\260\354\210\240\353\251\264\354\240\221/dec_to_oct.cpp" @@ -0,0 +1,11 @@ +/* 10진수를 8진수로 변환 */ + +void dec_to_oct(int n){ + if(n<8){ + cout << n; + return; + } + + dec_to_oct(n/8); + cout << n % 8; +} \ No newline at end of file diff --git "a/\352\270\260\354\210\240\353\251\264\354\240\221/non_repeatable.cpp" "b/\352\270\260\354\210\240\353\251\264\354\240\221/non_repeatable.cpp" new file mode 100644 index 0000000..980b0a4 --- /dev/null +++ "b/\352\270\260\354\210\240\353\251\264\354\240\221/non_repeatable.cpp" @@ -0,0 +1,30 @@ +/* +문자열에서 반복되지 않는 첫번째 문자를 찾아내는 효율적인 함수를 작성하여라. • “total” => “o” • “teeter” => “r” +*/ + +#include +#include + +using namespace std; + +int count[26]; + +int main(){ + + string str; + + cin >> str; + + for(int i=0; i Date: Tue, 1 Jun 2021 12:44:08 +0900 Subject: [PATCH 134/203] =?UTF-8?q?=EC=86=8C=EB=AC=B8=EB=82=9C=20=EC=B9=A0?= =?UTF-8?q?=EA=B3=B5=EC=A3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1941.cpp" | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 "baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1941.cpp" diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1941.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1941.cpp" new file mode 100644 index 0000000..513e055 --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1941.cpp" @@ -0,0 +1,93 @@ +#include +#include + +using namespace std; + +bool include[5][5]; +string board[5]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; +int ans; + +bool adjacent(){ + bool visited[5][5]; + + for(int i=0; i<5; i++){ + for(int j=0; j<5; j++){ + visited[i][j] = false; + } + } + + queue < pair > q; + for(int i=0; i<5 && q.empty(); i++){ + for(int j=0; j<5; j++){ + if(include[i][j]){ + q.push(make_pair(i,j)); + visited[i][j] = true; + break; + } + } + } + + int count = 1; + + while(!q.empty() && count < 7){ + int x = q.front().first; + int y = q.front().second; + q.pop(); + + for(int i=0; i<4; i++){ + int nx = x+dx[i]; + int ny = y+dy[i]; + if(nx>=0 && ny >=0 && nx < 5 && ny < 5 && !visited[nx][ny] && include[nx][ny]){ + visited[nx][ny] = true; + q.push(make_pair(nx,ny)); + count++; + } + } + } + + if(count == 7)return true; + return false; +} + +void brute(int i, int j, int selected, int y, int s){ + if(selected == 7){ + if(y=5){ + i++; + j=0; + } + + if(i>=5){ + return; + } + + int ny = y; + int ns = s; + + include[i][j] = true; + if(board[i][j] == 'Y') ny++; + else ns++; + brute(i, j+1, selected+1, ny, ns); + include[i][j] = false; + brute(i, j+1, selected, y, s); +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + for(int i=0; i<5; i++) cin >> board[i]; + + brute(0, 0, 0, 0, 0); + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file From 5ad638988de2be288a06353a1954d4a0a9724be9 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 8 Jun 2021 15:24:52 +0900 Subject: [PATCH 135/203] =?UTF-8?q?=EB=91=90=20=EB=A1=9C=EB=B4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../15971.cpp" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/15971.cpp" diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/15971.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/15971.cpp" new file mode 100644 index 0000000..15959bb --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/15971.cpp" @@ -0,0 +1,50 @@ +#include +#include + +using namespace std; + +int N, A, B; +vector< pair > edges[100001]; +bool visited[100001]; +bool flag = false; +long long total = 0; +long long maxCost = 0; + +bool dfs(int n){ + + visited[n] = true; + if(n==B) return true; + + for(pair edge: edges[n]){ + int next = edge.second; + int cost = edge.first; + if(!visited[next]){ + if(dfs(next)){ + total += cost; + if(maxCost < cost) maxCost = cost; + return true; + } + } + } + return false; +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> A >> B; + + for(int i=0; i> x >> y >> z; + edges[x].push_back(make_pair(z, y)); + edges[y].push_back(make_pair(z, x)); + } + + dfs(A); + + cout << total - maxCost << "\n"; + + return 0; +} \ No newline at end of file From a742b463c660a1416e3031d692d8cab68885f710 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 9 Jun 2021 15:53:20 +0900 Subject: [PATCH 136/203] =?UTF-8?q?=EC=9E=91=EC=97=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2056.cpp" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 "baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2056.cpp" diff --git "a/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2056.cpp" "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2056.cpp" new file mode 100644 index 0000000..0cbd65e --- /dev/null +++ "b/baekjoon/\354\234\204\354\203\201\354\240\225\353\240\254/2056.cpp" @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace std; + +int N; +vector v[10001]; +priority_queue< pair > q; +int t[10001]; +int d[10001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + for(int i=1; i<=N; i++){ + cin >> t[i]; + int x, y; + cin >> x; + for(int j=0; j> y; + v[y].push_back(i); + d[i]++; + } + } + + for(int i=1; i<=N; i++){ + if(d[i] == 0){ + q.push(make_pair(-t[i], i)); + } + } + + int ans = 0; + + while(!q.empty()){ + int x = q.top().second; + int y = -q.top().first; + q.pop(); + ans = max(ans, y); + + for(int k:v[x]){ + d[k]--; + if(d[k]==0){ + q.push(make_pair(-y-t[k], k)); + } + } + } + + cout << ans << "\n"; + + + return 0; +} \ No newline at end of file From acccbf18e380ff40e1c471d2ea6d4e0ddd01367a Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 10 Jun 2021 14:05:41 +0900 Subject: [PATCH 137/203] =?UTF-8?q?=EB=AC=B4=ED=95=9C=20=EC=88=98=EC=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\353\247\265/1351.cpp" | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 "baekjoon/\353\247\265/1351.cpp" diff --git "a/baekjoon/\353\247\265/1351.cpp" "b/baekjoon/\353\247\265/1351.cpp" new file mode 100644 index 0000000..d86f1b4 --- /dev/null +++ "b/baekjoon/\353\247\265/1351.cpp" @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + +long long N, P, Q; +map d; + +long long dp(long long n){ + if(d.find(n) == d.end()){ + return d[n] = dp(n/P) + dp(n/Q); + } + return d[n]; +} + + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> P >> Q; + + d[0] = 1; + + cout << dp(N) << "\n"; + + return 0; +} \ No newline at end of file From 2a1752809e2fad855982c148e2238656b2c968d2 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 13 Jun 2021 13:08:19 +0900 Subject: [PATCH 138/203] =?UTF-8?q?=ED=8D=BC=EC=A6=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1525.cpp" | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1525.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1525.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1525.cpp" new file mode 100644 index 0000000..efc774f --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/1525.cpp" @@ -0,0 +1,75 @@ +#include +#include +#include + +using namespace std; + +map visited; +queue< pair > q; +int dx[9][4] = { + {1, 3, -1, -1}, + {0, 2, 4, -1}, + {1, 5, -1, -1}, + {0, 4, 6, -1}, + {3, 5, 1, 7}, + {4, 2, 8, -1}, + {3, 7, -1, -1}, + {6, 4, 8, -1}, + {7, 5, -1, -1} +}; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + string start = ""; + + int x; + + for(int i=0; i<9; i++){ + cin >> x; + start += x+'0'; + } + + visited[start] = true; + q.push(make_pair(start, 0)); + + while(!q.empty()){ + string now = q.front().first; + int n = q.front().second; + q.pop(); + + if(now == "123456780"){ + cout << n << "\n"; + return 0; + } + + int find; + for(int i=0; i<9; i++){ + if(now[i]=='0'){ + find=i; + break; + } + } + + for(int i=0; i<4; i++){ + if(dx[find][i]==-1) break; + string next; + if(find Date: Mon, 14 Jun 2021 18:50:12 +0900 Subject: [PATCH 139/203] =?UTF-8?q?=EB=8F=84=EC=84=9C=EA=B4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1461.cpp" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/1461.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1461.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1461.cpp" new file mode 100644 index 0000000..5337cbc --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1461.cpp" @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; +int N, M; +vector positive; +vector negative; +int answer = 0; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + int x; + for(int i=0; i> x; + if(x<0) negative.push_back(x); + else positive.push_back(x); + } + + sort(negative.begin(), negative.end()); + sort(positive.begin(), positive.end()); + + int ps = 0, ns = 0; + int pn = positive.size()-1; + int nn = negative.size()-1; + + // ns nn ps pn + if(negative.empty() || (!positive.empty() && positive[pn] > -negative[0])){ + answer += positive[pn]; + pn -= M; + } + else{ + answer -= negative[ns]; + ns += M; + } + + while(ns <= nn){ + answer -= negative[ns]*2; + ns += M; + } + + while(ps <= pn){ + answer += positive[pn]*2; + pn -= M; + } + + cout << answer << "\n"; + + return 0; +} \ No newline at end of file From 072b5edcbfbac7daa6c3945ce54454f47599ce74 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 19 Jun 2021 22:36:10 +0900 Subject: [PATCH 140/203] =?UTF-8?q?DFS=20=EC=8A=A4=ED=8E=98=EC=85=9C=20?= =?UTF-8?q?=EC=A0=80=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16964.cpp" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/16964.cpp" diff --git "a/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/16964.cpp" "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/16964.cpp" new file mode 100644 index 0000000..b2ae9dd --- /dev/null +++ "b/baekjoon/\352\271\212\354\235\264\354\232\260\354\204\240\355\203\220\354\203\211/16964.cpp" @@ -0,0 +1,58 @@ +#include +#include +#include + +using namespace std; + +map graph[100001]; +bool visited[100001]; +int N; +queue check; +bool flag = true; + +void dfs(int n){ + + //cout << n << "/"; + + check.pop(); + visited[n]=true; + + while(!check.empty()){ + if(graph[n].find(check.front())!=graph[n].end() && !visited[check.front()]){ + dfs(check.front()); + } + else{ + return; + } + } +} + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y; + + for(int i=0; i> x >> y; + graph[x][y] = true; + graph[y][x] = true; + } + + for(int i=0; i> x; + check.push(x); + } + + if(check.front()==1) + dfs(check.front()); + + if(check.empty()) cout << "1\n"; + else cout << "0\n"; + + + return 0; +} \ No newline at end of file From c8f76f936335c2c0fceeadc1807e89fcca81424b Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 20 Jun 2021 20:40:24 +0900 Subject: [PATCH 141/203] =?UTF-8?q?=ED=96=89=EC=9A=B4=EC=9D=98=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EC=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1342.cpp" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1342.cpp" diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1342.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1342.cpp" new file mode 100644 index 0000000..2a4a66a --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/1342.cpp" @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int countA[26]; +long long ans = 0; +string str; + +void solve(int n, string s){ + if(n==str.length()){ + ans++; + return; + } + + for(int i=0; i<26; i++){ + if(s.length()==0 || s[s.length()-1] != char(i+'a')) + if(countA[i]>0){ + countA[i]--; + solve(n+1, s+(char)(i+'a')); + countA[i]++; + } + } +} + +int main(){ + + cin >> str; + + for(int i=0; i Date: Mon, 21 Jun 2021 18:26:41 +0900 Subject: [PATCH 142/203] =?UTF-8?q?=EA=B0=9C=EB=AF=B8=EA=B5=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14725.cpp" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "baekjoon/\355\212\270\353\235\274\354\235\264/14725.cpp" diff --git "a/baekjoon/\355\212\270\353\235\274\354\235\264/14725.cpp" "b/baekjoon/\355\212\270\353\235\274\354\235\264/14725.cpp" new file mode 100644 index 0000000..c35df91 --- /dev/null +++ "b/baekjoon/\355\212\270\353\235\274\354\235\264/14725.cpp" @@ -0,0 +1,54 @@ +#include +#include + +using namespace std; + +int N; + +class Node{ + public: + string str; + map children; + + Node(string s){ + str = s; + } +}; + +Node* trie = new Node("root"); + +void dfs(Node* now, int level){ + if(now->children.empty()) return; + map::iterator it; + for(it=now->children.begin(); it!=now->children.end(); it++){ + for(int i=0; ifirst << "\n"; + dfs(it->second, level+1); + } +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x; + string tmp; + + for(int i=0; i> x; + Node* now = trie; + for(int j=0; j> tmp; + if(now->children.find(tmp)==now->children.end()){ + now->children[tmp] = new Node(tmp); + } + now = now->children[tmp]; + } + } + + dfs(trie, 0); + + return 0; +} \ No newline at end of file From 09bf866789d225cba81de041577172a7c8417095 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 21 Jun 2021 18:30:03 +0900 Subject: [PATCH 143/203] =?UTF-8?q?=EC=86=90=EC=BD=94=EB=94=A9=20=EC=8B=A4?= =?UTF-8?q?=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../untitled.cpp" | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 "\352\270\260\354\210\240\353\251\264\354\240\221/untitled.cpp" diff --git "a/\352\270\260\354\210\240\353\251\264\354\240\221/untitled.cpp" "b/\352\270\260\354\210\240\353\251\264\354\240\221/untitled.cpp" new file mode 100644 index 0000000..c68fbdd --- /dev/null +++ "b/\352\270\260\354\210\240\353\251\264\354\240\221/untitled.cpp" @@ -0,0 +1,72 @@ +## 손코딩 실전 0615 + +bool flag = true; + +for(int i=0; i Date: Thu, 24 Jun 2021 22:42:08 +0900 Subject: [PATCH 144/203] =?UTF-8?q?=EB=8F=99=EC=A0=84(=EB=B0=B0=EB=82=AD?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/9084.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 baekjoon/DP/9084.cpp diff --git a/baekjoon/DP/9084.cpp b/baekjoon/DP/9084.cpp new file mode 100644 index 0000000..3bde789 --- /dev/null +++ b/baekjoon/DP/9084.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int T, N; +int won[20]; +int M; +long long DP[10001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int t=0; t> N; + for(int i=0; i> won[i]; + } + cin >> M; + + for(int j=0; j<=M; j++) DP[j] = 0; + DP[0] = 1; + + for(int i=0; i Date: Fri, 25 Jun 2021 23:35:23 +0900 Subject: [PATCH 145/203] =?UTF-8?q?=ED=8C=B0=EB=A6=B0=EB=93=9C=EB=A1=AC=20?= =?UTF-8?q?=EB=B6=84=ED=95=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/1509.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 baekjoon/DP/1509.cpp diff --git a/baekjoon/DP/1509.cpp b/baekjoon/DP/1509.cpp new file mode 100644 index 0000000..68f6cb2 --- /dev/null +++ b/baekjoon/DP/1509.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; +int DP[2500][2500]; +int DP2[2501]; + +int main(){ + string str; + cin >> str; + + for(int i=0; i=0; i--){ + if(str[i] == str[j] && DP[i+1][j-1] > 0) DP[i][j] = DP[i+1][j-1]+2; + } + } + + // for(int i=0; i0) DP2[j+1] = min(DP2[j+1], DP2[i] + 1); + } + } + + cout << DP2[str.length()] << "\n"; + + return 0; +} \ No newline at end of file From 749d3e303ab9ca90bb0082c21d4bcd6094808aef Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 26 Jun 2021 16:22:37 +0900 Subject: [PATCH 146/203] =?UTF-8?q?=EA=B3=A0=EC=B8=B5=20=EB=B9=8C=EB=94=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/1328.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 baekjoon/DP/1328.cpp diff --git a/baekjoon/DP/1328.cpp b/baekjoon/DP/1328.cpp new file mode 100644 index 0000000..8db415e --- /dev/null +++ b/baekjoon/DP/1328.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int N, L, R; +long long D[101][101][101]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> L >> R; + + D[1][1][1] = 1; + + for(int i=2; i<=N; i++){ + for(int j=1; j<=L; j++){ + for(int k=1; k<=R; k++){ + D[i][j][k] += D[i-1][j-1][k] + D[i-1][j][k-1] + D[i-1][j][k]*(i-2) % 1000000007; + D[i][j][k] %= 1000000007; + } + } + } + + cout << D[N][L][R] << "\n"; + + return 0; +} \ No newline at end of file From 5d87972b95b713c5b136fd06567c034489071d78 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 27 Jun 2021 12:37:41 +0900 Subject: [PATCH 147/203] Hello World! bruteforce --- .../13140.cpp" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/13140.cpp" diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/13140.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/13140.cpp" new file mode 100644 index 0000000..8244f48 --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/13140.cpp" @@ -0,0 +1,67 @@ +#include +#include + +using namespace std; + +string alpha = "helowrd"; +int idx[10] = {0,1,2,2,3,4,3,5,2,6}; +int digit[11]; +bool used[11]; +int N; +bool flag = false; + +void solve(int n){ + + if(flag) return; + if(n==7){ + + int answer = 0; + + for(int i=0; i<5; i++){ + answer += digit[idx[i]]*pow(10, 4-i) + digit[idx[i+5]]*pow(10, 4-i); + } + + if(answer == N){ + flag = true; + + cout << " "; + for(int i=0; i<5; i++){ + cout << digit[idx[i]]; + } + cout << "\n+ "; + for(int i=5; i<10; i++){ + cout << digit[idx[i]]; + } + cout << "\n-------\n"; + printf("%7d\n", N); + } + + return; + } + + for(int i=0; i<10; i++){ + if((n==0 || n==4) && i==0){ + continue; + } + + if(!used[i]){ + digit[n] = i; + used[i] = true; + solve(n+1); + used[i] = false; + } + } +} + +int main(){ + + cin >> N; + + solve(0); + + if(!flag){ + cout << "No Answer\n"; + } + + return 0; +} \ No newline at end of file From be5a8f3762916ce759de008ef13bc98b1920c34f Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 30 Jun 2021 16:42:58 +0900 Subject: [PATCH 148/203] =?UTF-8?q?=EB=B2=BD=EC=9E=A5=EB=AC=B8=EC=9D=98=20?= =?UTF-8?q?=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2666.cpp" | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 "baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2666.cpp" diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2666.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2666.cpp" new file mode 100644 index 0000000..9d32103 --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/2666.cpp" @@ -0,0 +1,86 @@ +#include + +using namespace std; + +int N, M; +bool isOpen[21]; +int T[21]; + +int solve(int n, int ans){ + if(n==M) return ans; + + int x, y; + for(int i=1; i<=N; i++){ + if(isOpen[i]){ + x=i; + break; + } + } + + for(int i=x+1; i<=N; i++){ + if(isOpen[i]){ + y=i; + break; + } + } + + if(T[n] == x || T[n] == y) return solve(n+1, ans); + if(T[n] < x){ + isOpen[x] = false; + isOpen[T[n]] = true; + + int res = solve(n+1, ans + x-T[n]); + isOpen[x] = true; + isOpen[T[n]] = false; + return res; + } + if(T[n] > y){ + isOpen[y] = false; + isOpen[T[n]] = true; + + int res = solve(n+1, ans + T[n]-y); + isOpen[y] = true; + isOpen[T[n]] = false; + return res; + } + + int d1, d2; + + isOpen[x] = false; + isOpen[T[n]] = true; + d1 = solve(n+1, ans + T[n]-x); + isOpen[x] = true; + isOpen[y] = false; + d2 = solve(n+1, ans + y - T[n]); + if(d1 < d2){ + isOpen[x] = true; + isOpen[T[n]] = false; + return d1; + } + else{ + isOpen[y] = true; + isOpen[T[n]] = false; + return d2; + } +} + +int main(){ + cin >> N; + + int x; + for(int i=0; i<2; i++){ + cin >> x; + isOpen[x] = true; + } + + cin >> M; + + for(int i=0; i> T[i]; + } + + cout << solve(0, 0) << "\n"; + + + return 0; +} \ No newline at end of file From c513fa6020fe193762b9b068a3c0d94e7f2d4ab0 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 1 Jul 2021 11:30:46 +0900 Subject: [PATCH 149/203] =?UTF-8?q?K=EB=B2=88=EC=A7=B8=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1300.cpp" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "baekjoon/\354\235\264\353\266\204\355\203\220\354\203\211/1300.cpp" diff --git "a/baekjoon/\354\235\264\353\266\204\355\203\220\354\203\211/1300.cpp" "b/baekjoon/\354\235\264\353\266\204\355\203\220\354\203\211/1300.cpp" new file mode 100644 index 0000000..c538ded --- /dev/null +++ "b/baekjoon/\354\235\264\353\266\204\355\203\220\354\203\211/1300.cpp" @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int N, K; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> K; + + int left = 1, right = 1000000000; + int middle; + int ans; + + while(left <= right){ + middle = (left+right)/2; + + int count = 0; + for(int i=1; i<=N; i++){ + count += min(N, middle/i); + } + //cout << middle << " " << count << "\n"; + if(count >= K){ + ans = middle; + right = middle-1; + } + else{ + left = middle+1; + } + } + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file From 602016e56fff2a988247f32b54380bbfc1347dfd Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 2 Jul 2021 10:13:38 +0900 Subject: [PATCH 150/203] =?UTF-8?q?=EA=B2=8C=EB=A6=AC=EB=A7=A8=EB=8D=94?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../17471.cpp" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "baekjoon/\354\213\234\353\256\254\353\240\210\354\235\264\354\205\230/17471.cpp" diff --git "a/baekjoon/\354\213\234\353\256\254\353\240\210\354\235\264\354\205\230/17471.cpp" "b/baekjoon/\354\213\234\353\256\254\353\240\210\354\235\264\354\205\230/17471.cpp" new file mode 100644 index 0000000..e27ea22 --- /dev/null +++ "b/baekjoon/\354\213\234\353\256\254\353\240\210\354\235\264\354\205\230/17471.cpp" @@ -0,0 +1,81 @@ +#include +#include + +using namespace std; + +int N; +int P[11]; +vector edges[11]; +int ans = 10000; +bool visited[11]; +int check[12]; + +void dfs(int n, int k){ + visited[n] = true; + for(int next:edges[n]){ + if(!visited[next] && check[next]==k){ + dfs(next, k); + } + } +} + +void solve(int n){ + if(n==N+1){ + for(int i=1; i<=N; i++) visited[i] = false; + for(int i=1; i<=N; i++){ + if(check[i] == 0){ + dfs(i, 0); + break; + } + } + for(int i=1; i<=N; i++){ + if(check[i] == 11){ + dfs(i, 11); + break; + } + } + for(int i=1; i<=N; i++){ + if(!visited[i]) return; + } + + int p1=0, p2=0; + for(int i=1; i<=N; i++){ + if(check[i] == 0) p1+=P[i]; + else p2 += P[i]; + } + if(p1==0 || p2==0) return; + ans = min(ans, abs(p1-p2)); + return; + } + + check[n] = 0; + solve(n+1); + check[n] = 11; + solve(n+1); +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=1; i<=N; i++){ + cin >> P[i]; + } + + int x, tmp; + for(int i=1; i<=N; i++){ + cin >> x; + for(int j=0; j> tmp; + edges[i].push_back(tmp); + } + } + + solve(1); + + if(ans == 10000) cout << "-1\n"; + else cout << ans << "\n"; + + return 0; +} \ No newline at end of file From 82eb06c7aac60ca3ab81fca602b712ce299dbbc5 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 3 Jul 2021 23:04:50 +0900 Subject: [PATCH 151/203] =?UTF-8?q?=ED=95=A9=EC=9D=B4=200=EC=9D=B8=20?= =?UTF-8?q?=EB=84=A4=20=EC=A0=95=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../7453.cpp" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/7453.cpp" diff --git "a/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/7453.cpp" "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/7453.cpp" new file mode 100644 index 0000000..cce8a23 --- /dev/null +++ "b/baekjoon/\353\221\220\355\217\254\354\235\270\355\204\260/7453.cpp" @@ -0,0 +1,49 @@ +#include +#include +#include + +using namespace std; + +int N; +long long A[4000][4]; +vector B; + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + + for(int i=0; i> A[i][j]; + } + } + + for(int i=0; i::iterator low = lower_bound(B.begin(), B.end(), -now); + vector::iterator high = upper_bound(B.begin(), B.end(), -now); + + if(*low == -now){ + ans += high-low; + } + } + } + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file From 0c4b497eb819b0db5e7a735b8433ad5307628bce Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 4 Jul 2021 15:00:53 +0900 Subject: [PATCH 152/203] =?UTF-8?q?=EC=BD=94=ED=85=8C=EB=AC=B8=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- programmers/210704.java | 101 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 programmers/210704.java diff --git a/programmers/210704.java b/programmers/210704.java new file mode 100644 index 0000000..8458878 --- /dev/null +++ b/programmers/210704.java @@ -0,0 +1,101 @@ +import java.util.Arrays; + +class Solution { + public int solution(int[] prices, int[] discounts) { + int answer = 0; + int idx = discounts.length-1; + + Arrays.sort(prices); + Arrays.sort(discounts); + + for(int p=prices.length-1; p>=0; p--){ + if(idx>=0){ + answer += prices[p]*(100-discounts[idx])/100; + idx--; + } + else { + answer += prices[p]; + } + } + return answer; + } +} + + +import java.util.*; + +class Solution { + public String[] solution(String s) { + //String[] answer = {}; + List answer = new LinkedList(); + + int leftlow = 0, lefthigh = 0; + int rightlow = s.length()-1, righthigh = s.length()-1; + int now = 0; + + while(leftlow<=rightlow){ + if(s.substring(leftlow, lefthigh+1).equals(s.substring(rightlow, righthigh+1))){ + + //System.out.println(s.substring(leftlow, lefthigh+1)); + if(leftlow != rightlow){ + answer.add(now, s.substring(leftlow, lefthigh+1)); + answer.add(answer.size()-now, s.substring(leftlow, lefthigh+1)); + now++; + } + else + answer.add(now, s.substring(leftlow, lefthigh+1)); + + leftlow = lefthigh+1; + lefthigh = leftlow; + righthigh = rightlow-1; + rightlow = righthigh; + } + else{ + lefthigh++; + rightlow--; + } + } + + return answer.toArray(new String[0]); + } +} + +import java.util.*; + +public class Solution { + public int solution(String s, String t) { + int result = 0; + + Stack stack = new Stack<>(); + + for(int i=0; i Date: Tue, 6 Jul 2021 10:41:39 +0900 Subject: [PATCH 153/203] =?UTF-8?q?=ED=94=BC=EC=9E=90=20=EA=B5=BD=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/1756.cpp" | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/1756.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/1756.cpp" "b/baekjoon/\352\265\254\355\230\204/1756.cpp" new file mode 100644 index 0000000..41a40e7 --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/1756.cpp" @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int D, N; +int A[300000]; +int B[300000]; +int M[300000]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> D >> N; + + for(int i=0; i> A[i]; + } + + for(int j=0; j> B[j]; + } + + M[0] = A[0]; + + for(int i=1; i=0; i--){ + if(now= B[now]){ + now++; + last = i; + } + } + + if(now==N) cout << last+1 << "\n"; + else cout << "0\n"; + + return 0; +} \ No newline at end of file From b59dd407b9c3f8b857a99ac650aec76deea444e5 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 7 Jul 2021 16:55:15 +0900 Subject: [PATCH 154/203] =?UTF-8?q?=ED=92=8D=EC=84=A0=20=ED=84=B0=EB=9C=A8?= =?UTF-8?q?=EB=A6=AC=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2346.cpp" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/2346.cpp" diff --git "a/baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/2346.cpp" "b/baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/2346.cpp" new file mode 100644 index 0000000..e869fa9 --- /dev/null +++ "b/baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/2346.cpp" @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int N; + +struct node{ + int n; + int t; + struct node* previous; + struct node* next; +}; + +struct node list[1000]; + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> x; + list[i].t = x; + } + + struct node* now = &list[0]; + for(int i=0; in << " "; + now->previous->next = now->next; + now->next->previous = now->previous; + int k = now->t; + if(k>0){ + for(int j=0; jnext; + } + } + else{ + for(int j=0; j>k; j--){ + now = now->previous; + } + } + } + + return 0; +} \ No newline at end of file From 012b9e0c62bb1214db0e00a75489d2b4b6346c82 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 8 Jul 2021 12:05:21 +0900 Subject: [PATCH 155/203] =?UTF-8?q?=EC=96=91=ED=8C=94=EC=A0=80=EC=9A=B8(?= =?UTF-8?q?=EB=B0=B0=EB=82=AD=EB=AC=B8=EC=A0=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2629.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 baekjoon/DP/2629.cpp diff --git a/baekjoon/DP/2629.cpp b/baekjoon/DP/2629.cpp new file mode 100644 index 0000000..1abfce0 --- /dev/null +++ b/baekjoon/DP/2629.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +int N, M; +int C[30]; +int T[7]; +int D[30][15001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> C[i]; + D[i][0] = 1; + } + + D[0][C[0]]=1; + + for(int i=1; i= 0) D[i][j] = max(D[i][j], D[i-1][j-C[i]]); + } + } + + cin >> M; + for(int i=0; i> T[i]; + for(int j=0; j+T[i] <= 15000; j++){ + if(D[N-1][j] == 1 && D[N-1][j+T[i]] == 1){ + flag = true; + break; + } + } + if(flag) cout << "Y "; + else cout << "N "; + } + + return 0; +} \ No newline at end of file From 0690540aab968053b4107284cff23db6930c8faa Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 9 Jul 2021 11:47:22 +0900 Subject: [PATCH 156/203] =?UTF-8?q?=EB=B0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" new file mode 100644 index 0000000..e69de29 From 2fc9d1ce899360fe3efa4b98518aedfd96baac04 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 9 Jul 2021 11:48:08 +0900 Subject: [PATCH 157/203] =?UTF-8?q?=EB=B0=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1092.cpp" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" index e69de29..772d6e0 100644 --- "a/baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1092.cpp" @@ -0,0 +1,47 @@ +#include +#include +#include +using namespace std; + +int main() { + int m, n; + int num; + int cnt = 0; + vector v1, v2; + + cin >> m; + for (int i = 0; i < m; i++) { + cin >> num; + v1.push_back(num); + } + + cin >> n; + for (int i = 0; i < n; i++) { + cin >> num; + v2.push_back(num); + } + + sort(v1.begin(), v1.end(), greater()); + sort(v2.begin(), v2.end(), greater()); + + if (v2[0] > v1[0]) { + cout << -1 << "\n"; + } + else { + while (!v2.empty()) { + int index = 0; + for (int i = 0; i < v2.size(); i++) { + if (index == v1.size()) { + break; + } + if (v1[index] >= v2[i]) { + index++; + v2.erase(v2.begin() + i); + i = i - 1; + } + } + cnt++; + } + cout << cnt << "\n"; + } +} \ No newline at end of file From 44eecab0305b351958cb703866da0029b0220037 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 10 Jul 2021 22:00:03 +0900 Subject: [PATCH 158/203] N-Queen --- .../9663.cpp" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/9663.cpp" diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/9663.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/9663.cpp" new file mode 100644 index 0000000..d188783 --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/9663.cpp" @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int check[15]; +int N; +int ans = 0; + +void solve(int n){ + + if(n==N){ + ans++; + return; + } + + bool possible[15]; + for(int i=0; i=0) + possible[check[i]-(n-i)]=false; + } + for(int i=0; i> N; + solve(0); + cout << ans << "\n"; + + return 0; +} \ No newline at end of file From 2384021d6875a74d7c41b4bbb04e41d86c30b268 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 11 Jul 2021 13:56:20 +0900 Subject: [PATCH 159/203] =?UTF-8?q?=EC=88=9C=ED=9A=8C=EA=B0=95=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2109.cpp" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/2109.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2109.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2109.cpp" new file mode 100644 index 0000000..c2d8d1e --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2109.cpp" @@ -0,0 +1,58 @@ +#include +#include + +using namespace std; + +struct node{ + int p, d; + + bool operator<(const struct node& a) const{ + if(p == a.p){ + return d pq; +bool used[10001]; +int parent[10001]; + +int getp(int n){ + if(parent[n] == n) return n; + return parent[n] = getp(parent[n]); +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int x, y; + for(int i=0; i> x >> y; + pq.push({x,y}); + } + for(int i=1; i<=10000; i++) parent[i] = i; + + int ans = 0; + + while(!pq.empty()){ + int d = pq.top().d; + int p = pq.top().p; + pq.pop(); + + parent[d] = getp(d); + if(parent[d]!=0){ + used[parent[d]] = true; + ans+=p; + //cout << p << " " << parent[d] << "\n"; + parent[parent[d]] = getp(parent[d]-1); + } + } + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file From 409aa2af3b9b9d11e6073dcf1c6732ef10b5d3a8 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 11 Jul 2021 14:40:12 +0900 Subject: [PATCH 160/203] =?UTF-8?q?=EB=89=B4=EC=8A=A4=20=EC=A0=84=ED=95=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/1135.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 baekjoon/DP/1135.cpp diff --git a/baekjoon/DP/1135.cpp b/baekjoon/DP/1135.cpp new file mode 100644 index 0000000..c9f80fa --- /dev/null +++ b/baekjoon/DP/1135.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +using namespace std; + +vector ch[51]; +bool visited[51]; +int N; + +int dfs(int n){ + if(ch[n].size()==0) return 0; + + int time[51]; + for(int i=0; i> N; + int x; + cin >> x; + for(int i=1; i> x; + ch[x].push_back(i); + } + + cout << dfs(0) << "\n"; + return 0; +} \ No newline at end of file From 22f612ca18fd7544a60400c48ba698d31ab9b25f Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 12 Jul 2021 23:28:30 +0900 Subject: [PATCH 161/203] =?UTF-8?q?=EC=BD=94=EB=94=A9=EC=9D=80=20=EC=98=88?= =?UTF-8?q?=EC=81=98=EA=B2=8C=20[=ED=91=B8=EB=8A=94=EC=A4=91]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2879.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 baekjoon/DP/2879.cpp diff --git a/baekjoon/DP/2879.cpp b/baekjoon/DP/2879.cpp new file mode 100644 index 0000000..bc237f4 --- /dev/null +++ b/baekjoon/DP/2879.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int N; +int A[1000]; + +int main(){ + int x; + cin >> N; + for(int i=0; i> A[i]; + } + + for(int i=0; i> x; + A[i] = -A[i]+x; + cout << A[i] << "\t"; + } + + int ans = 0; + + for(int i=0; i 0){ + A[j]-=A[i]; + } + else{ + break; + } + } + } + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file From 5fdbb7c1d3b62e4c46ca4519376f0dadc5cbfd11 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 13 Jul 2021 09:28:57 +0900 Subject: [PATCH 162/203] =?UTF-8?q?=EC=BD=94=EB=94=A9=EC=9D=80=20=EC=98=88?= =?UTF-8?q?=EC=81=98=EA=B2=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2879.cpp | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/baekjoon/DP/2879.cpp b/baekjoon/DP/2879.cpp index bc237f4..3d07aa0 100644 --- a/baekjoon/DP/2879.cpp +++ b/baekjoon/DP/2879.cpp @@ -15,24 +15,37 @@ int main(){ for(int i=0; i> x; A[i] = -A[i]+x; - cout << A[i] << "\t"; + //cout << A[i] << "\t"; } int ans = 0; - for(int i=0; i 0){ - A[j]-=A[i]; + int prev = A[0]; + for(int i=1; i=0){ + if(A[i]<0){ + ans += abs(prev); + } + else if(prev < A[i]){ + ; } else{ - break; + ans += abs(prev-A[i]); + } + prev = A[i]; + }else{ + if(A[i]>=0){ + ans += abs(prev); + } + else if(prev > A[i]){ } + else{ + ans += abs(prev-A[i]); + } + prev = A[i]; } } - + ans += abs(prev); cout << ans << "\n"; return 0; From aec0f48f0945fd38b9d99dfa0fc8358de3b9809e Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 13 Jul 2021 15:57:45 +0900 Subject: [PATCH 163/203] real coding test kp-internship --- programmers/210713.cpp | 163 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 programmers/210713.cpp diff --git a/programmers/210713.cpp b/programmers/210713.cpp new file mode 100644 index 0000000..786d534 --- /dev/null +++ b/programmers/210713.cpp @@ -0,0 +1,163 @@ +#include +#include + +using namespace std; + +int solution(int money, int minratio, int maxratio, int ranksize, int threshold, int months){ + for(int i=0; i maxratio) ratio = maxratio; + money = money - tmp / 100 * ratio; + } + return money; +} + +vector solution(int rows, int columns, vector > swipes){ + vector answer; + int board[51][51]; + int k = 1; + for(int i=1; i<=rows; i++){ + for(int j=1; j<=columns; j++){ + board[i][j] = k++; + } + } + + for(vector swipe: swipes){ + int ans = 0; + int dir = swipe[0]; + if(dir == 1){ + for(int j=swipe[2]; j<=swipe[4]; j++){ + int tmp = board[swipe[3]][j]; + for(int i=swipe[3]; i>swipe[1]; i--){ + board[i][j] = board[i-1][j]; + } + board[swipe[1]][j] = tmp; + ans += tmp; + } + } + else if(dir == 2){ + for(int j=swipe[2]; j<=swipe[4]; j++){ + int tmp = board[swipe[1]][j]; + for(int i=swipe[1]; iswipe[2]; j--){ + board[i][j] = board[i][j-1]; + } + board[i][swipe[2]] = tmp; + ans += tmp; + } + } + else{ + for(int i=swipe[1]; i<=swipe[3]; i++){ + int tmp = board[i][swipe[2]]; + for(int j=swipe[2]; j + +struct node{ + long long age; + int num, type; + bool operator<(const struct node& a) const{ + if(age == a.age){ + return num > a.num; + } + return age > a.age; + } +}; + +priority_queue pq; +vector> edges[100001]; +int degree[100001]; +bool off[100001]; + +vector solution(vector ages, vector> wires){ + vector answer; + + for(int i=1; i<=ages.size(); i++){ + pq.push({ages[i-1], i, 1}); + } + + for(vector wire : wires){ + edges[wire[0]].push_back({wire[1], wire[2]}); + degree[wire[1]]++; + } + + while(!pq.empty()){ + long long age = pq.top().age; + int now = pq.top().num; + int type = pq.top().type; + pq.pop(); + + if(off[now]) continue; + if(type==1){ + off[now]=true; + answer.push_back(now); + + for(pair edge: edges[now]){ + if(!off[edge.first]){ + pq.push({age+edge.second, edge.first, 2}); + } + } + } + else{ + degree[now]--; + if(degree[now]==0){ + off[now]=true; + answer.push_back(now); + + for(pair edge: edges[now]){ + if(!off[edge.first]){ + pq.push({age+edge.second, edge.first, 2}); + } + } + } + } + } + + return answer; +} \ No newline at end of file From 3c4662e41b70f8c8b9db527c04d52768b56fdec4 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 14 Jul 2021 17:19:24 +0900 Subject: [PATCH 164/203] =?UTF-8?q?=ED=86=A0=EB=84=88=EB=A8=BC=ED=8A=B8=20?= =?UTF-8?q?=EB=A7=8C=EB=93=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2262.cpp" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/2262.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2262.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2262.cpp" new file mode 100644 index 0000000..0a0248a --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2262.cpp" @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int N; +int A[256]; +int winner[256][256]; +int DP[256][256]; + +int solve(int start, int end){ + + if(start == end) { + return 0; + } + if(start == end-1){ + return DP[start][end] = abs(A[start]-A[end]); + } + if(DP[start][end]!=0) return DP[start][end]; + + int result = 999999999; + for(int i=start; i> N; + for(int i=0; i> A[i]; + + for(int i=0; i Date: Thu, 15 Jul 2021 11:27:20 +0900 Subject: [PATCH 165/203] =?UTF-8?q?=EC=95=84=EC=9A=B0=EC=9C=BC=20=EC=9A=B0?= =?UTF-8?q?=EC=95=84=EC=9C=BC=EC=9D=B4=EC=95=BC(=EC=8A=A4=EC=9C=84?= =?UTF-8?q?=ED=95=91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../15922.cpp" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/15922.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/15922.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/15922.cpp" new file mode 100644 index 0000000..856bd69 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/15922.cpp" @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int N; +int x = -1000000000, y = -1000000000; +int ans = 0; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + int i, j; + for(int k=0; k> i >> j; + + if(i <= y && j <= y) continue; + if(i <= y){ + y=j; + } + else{ + ans += y-x; + x = i; + y = j; + } + } + + ans += y-x; + cout << ans << "\n"; + + return 0; +} \ No newline at end of file From e2ba81db279d8e9ee8ba3a61824dfe1c428ac165 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 15 Jul 2021 11:40:36 +0900 Subject: [PATCH 166/203] =?UTF-8?q?=EC=84=A0=20=EA=B8=8B=EA=B8=B0(?= =?UTF-8?q?=EC=8A=A4=EC=9C=84=ED=95=91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2170.cpp" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/2170.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2170.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2170.cpp" new file mode 100644 index 0000000..aa7154f --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2170.cpp" @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + +int N; +priority_queue< pair > pq; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + + int x, y; + for(int i=0; i> x >> y; + pq.push({-x, -y}); + } + + int ans = 0; + + int left = -1000000000; + int right = -1000000000; + + while(!pq.empty()){ + int x = -pq.top().first; + int y = -pq.top().second; + pq.pop(); + + if(y<=right) continue; + if(x<=right){ + right = y; + } + else{ + ans += right-left; + left = x; + right = y; + } + } + ans += right-left; + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file From de93dbedd56da7ac96f1ed2cf47e6cd46c4602c0 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 16 Jul 2021 11:50:55 +0900 Subject: [PATCH 167/203] =?UTF-8?q?=ED=97=9B=EA=B0=84=20=EC=88=A8=EB=B0=94?= =?UTF-8?q?=EA=BC=AD=EC=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../6118.cpp" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6118.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6118.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6118.cpp" new file mode 100644 index 0000000..03dee78 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/6118.cpp" @@ -0,0 +1,57 @@ +#include +#include +#include + +using namespace std; + +int N, M; +bool visited[20001]; +vector edges[20001]; +queue< pair > q; + + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=0; i> x >> y; + edges[x].push_back(y); + edges[y].push_back(x); + } + + q.push(make_pair(1, 0)); + visited[1] = true; + int length = 0; + int ans = 1; + int count = 0; + + while(!q.empty()){ + int n = q.front().first; + int d = q.front().second; + q.pop(); + + if(length Date: Sat, 17 Jul 2021 16:04:46 +0900 Subject: [PATCH 168/203] =?UTF-8?q?=EC=B2=A0=EB=A1=9C(=EC=8A=A4=EC=9C=84?= =?UTF-8?q?=ED=95=91&=EA=B7=B8=EB=A6=AC=EB=94=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../13334.cpp" | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 "baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/13334.cpp" diff --git "a/baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/13334.cpp" "b/baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/13334.cpp" new file mode 100644 index 0000000..38fb411 --- /dev/null +++ "b/baekjoon/\354\236\220\353\243\214\352\265\254\354\241\260/13334.cpp" @@ -0,0 +1,72 @@ +#include +#include + +using namespace std; + +struct node{ + int x, y; + + bool operator< (const node & a) const{ + if(y==a.y) return x > a.x; + return y > a.y; + } +}; + +int N, M; +priority_queue pq; +priority_queue include; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> x >> y; + if(x<=y){ + pq.push({x, y}); + } + else{ + pq.push({y, x}); + } + } + cin >> M; + + int start = -1000000001; + int end = -1000000001; + int ans = 0; + + while(!pq.empty()){ + int x = pq.top().x; + int y = pq.top().y; + pq.pop(); + + cout << "[" << x << " " << y << "]\n"; + + if(y-x > M) continue; + + if(y<=end) include.push(-x); + else{ + end = y; + start = y-M; + while(!include.empty()){ + int k = -include.top(); + if(k Date: Sun, 18 Jul 2021 17:10:25 +0900 Subject: [PATCH 169/203] Create 210718.cpp --- programmers/210718.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 programmers/210718.cpp diff --git a/programmers/210718.cpp b/programmers/210718.cpp new file mode 100644 index 0000000..6769369 --- /dev/null +++ b/programmers/210718.cpp @@ -0,0 +1,89 @@ +vector edges[200001]; + +bool dfs(int n, int des){ + if(n == des) return true; + for(unsigned int i=0; i &A, vector &B){ + + for(unsigned int i = 0; i < A.size(); i++){ + edges[A[i]].push_back(B[i]); + edges[B[i]].push_back(A[i]); + } + + return dfs(1, N); +} + +string solution2(string riddle){ + string answer = ""; + for(int i=0; i0){ + check[answer[i-1]-'a'] = false; + } + if(i numbers){ + int count[31]; + for(int i=0; i<=30; i++) count[i] = 0; + for(unsigned i = 0; i < numbers.size(); i++){ + int now = numbers[i]; + int k = 0; + + while(now > 0){ + if(now%2 == 1){ + count[k]++; + } + now = now / 2; + k++; + } + } + int ans = 0; + for(int i=0; i<=30; i++){ + ans = max(ans, count[i]) + } + return ans; +} + +vector digits_sum[51]; + +int get_sum_of_digits(int n){ + if(n<10) return n; + return get_sum_of_digits(n / 10) + n%10; +} + +int solution5(vector numbers){ + for(unsigned int i=0; i 1){ + sort(digits_sum[i].begin(), digits_sum[i].end()); + unsigned int length = digits_sum[i].size(); + ans = max(ans, digits_sum[i][length-1] + digits_sum[i][length-2]); + } + } +} From 5dc14c9a66833e9ffb36322a579e68bb2687daf1 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 20 Jul 2021 08:14:22 +0900 Subject: [PATCH 170/203] =?UTF-8?q?=EC=A4=84=EC=84=9C=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\354\212\244\355\203\235/17178.cpp" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "baekjoon/\354\212\244\355\203\235/17178.cpp" diff --git "a/baekjoon/\354\212\244\355\203\235/17178.cpp" "b/baekjoon/\354\212\244\355\203\235/17178.cpp" new file mode 100644 index 0000000..e25dd56 --- /dev/null +++ "b/baekjoon/\354\212\244\355\203\235/17178.cpp" @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +using namespace std; +list< pair > q[100]; +list< pair > total; +vector< pair > waiting; + +int N; + +pair string_strip(string s){ + pair res; + res.first = s[0] - 'A'; + int num = 0; + for(unsigned int i=2; i= '0' && s[i] <= '9'; i++){ + num = num*10 + s[i] - '0'; + } + res.second = num; + return res; +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + string tmp; + + cin >> N; + for(int i=0; i> tmp; + q[i].push_back(string_strip(tmp)); + total.push_back(string_strip(tmp)); + } + } + + total.sort(); + + bool flag = true; + + while(!total.empty() && flag){ + + // cout << (char)(total.front().first + 'A') << " " << total.front().second << "\n"; + // for(int i=0; i biggest = make_pair(0, 0); + + if(!waiting.empty() && waiting.back() == total.front()){ + waiting.pop_back(); + total.pop_front(); + flag = true; + continue; + } + + for(int i=0; i Date: Wed, 21 Jul 2021 16:37:53 +0900 Subject: [PATCH 171/203] =?UTF-8?q?=EA=B0=80=EC=9A=B4=EB=8D=B0=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EB=A7=8C=EB=82=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../21940.cpp" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/21940.cpp" diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/21940.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/21940.cpp" new file mode 100644 index 0000000..4b5d8e1 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/21940.cpp" @@ -0,0 +1,87 @@ +#include +#include + +using namespace std; + +#define INF 200001 + +int N, M, K; +int F[201][201]; +vector friends; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=1; i<=N; i++){ + for(int j=1; j<=N; j++){ + F[i][j] = INF; + } + F[i][i] = 0; + } + + + for(int i=0; i> x >> y >> z; + F[x][y] = z; + } + + cin >> K; + for(int i=0; i> x; + friends.push_back(x); + } + + for(int k=1; k<=N; k++){ + for(int i=1; i<=N; i++){ + for(int j=1; j<=N; j++){ + if(F[i][j] > F[i][k] + F[k][j]){ + F[i][j] = F[i][k] + F[k][j]; + } + } + } + } + + for(int i=1; i<=N; i++){ + for(int j=1; j<=N; j++){ + cout << F[i][j] << "\t"; + } + cout << "\n"; + } + + vector ans; + int minsum = INF * 400; + + for(int i=1; i<=N; i++){ + bool flag = true; + int tmp = 0; + for(int j:friends){ + if(F[i][j] == INF || F[j][i] == INF){ + flag = false; + break; + } + tmp = max(tmp, F[i][j] + F[j][i]); + } + + if(!flag) continue; + + if(minsum > tmp){ + ans.clear(); + ans.push_back(i); + minsum = tmp; + } + else if(minsum == tmp){ + ans.push_back(i); + } + } + + for(int a:ans){ + cout << a << " "; + } + cout << "\n"; + + return 0; +} \ No newline at end of file From 64026c19d58649863d3a703704f1e27b2107a02e Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 21 Jul 2021 17:23:25 +0900 Subject: [PATCH 172/203] =?UTF-8?q?=EC=B0=BD=EC=98=81=EC=9D=B4=EC=99=80=20?= =?UTF-8?q?=ED=87=B4=EA=B7=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../22116.cpp" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 "baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/22116.cpp" diff --git "a/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/22116.cpp" "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/22116.cpp" new file mode 100644 index 0000000..ee4909b --- /dev/null +++ "b/baekjoon/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/22116.cpp" @@ -0,0 +1,62 @@ +#include +#include +#include + +using namespace std; + +#define INF 1000000001 + +int N; +int D[1000][1000]; +vector< pair > edges[1000000]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; +priority_queue< pair > pq; +int dist[1000000]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> D[i][j]; + } + } + + for(int x=0; x= N || ny >= N) continue; + + edges[x*N+y].push_back(make_pair(nx*N+ny, abs(D[x][y]-D[nx][ny]))); + } + } + } + + for(int i=0; i<=N*N-1; i++) dist[i] = INF; + dist[0] = 0; + pq.push(make_pair(0, 0)); + + while(!pq.empty()){ + int cost = -pq.top().first; + int x = pq.top().second; + pq.pop(); + + if(dist[x] < cost) continue; + + for(pair next : edges[x]){ + if(dist[next.first] > max(next.second, cost)){ + dist[next.first] = max(next.second, cost); + pq.push(make_pair(-dist[next.first], next.first)); + } + } + } + + cout << dist[N*N-1] << "\n"; + + return 0; +} \ No newline at end of file From 74d7e1aa8328e2a7af44810b44a70289f69c8ca1 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 22 Jul 2021 20:48:13 +0900 Subject: [PATCH 173/203] =?UTF-8?q?=EA=B3=84=EB=8B=A8=20=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/1562.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 baekjoon/DP/1562.cpp diff --git a/baekjoon/DP/1562.cpp b/baekjoon/DP/1562.cpp new file mode 100644 index 0000000..3dfab6b --- /dev/null +++ b/baekjoon/DP/1562.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int N; +int DP[101][10][1024]; + +int main(){ + + cin >> N; + for(int i=1; i<10; i++){ + DP[1][i][1<0) + DP[i][j][nk] += DP[i-1][j-1][k]; + DP[i][j][nk] %= 1000000000; + if(j<9) + DP[i][j][nk] += DP[i-1][j+1][k]; + DP[i][j][nk] %= 1000000000; + } + } + } + + int ans = 0; + for(int i=0; i<10; i++){ + ans += DP[N][i][1023]; + ans %= 1000000000; + } + cout << ans << "\n"; + return 0; +} \ No newline at end of file From ddcaf73b22a8f71973c7b2d265ef2ff8b3eb82be Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 23 Jul 2021 10:35:07 +0900 Subject: [PATCH 174/203] =?UTF-8?q?=EB=B3=B4=EC=8A=A4=EB=AA=AC=EC=8A=A4?= =?UTF-8?q?=ED=84=B0=20=EC=A7=84=EB=A6=AC=ED=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20005.cpp" | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/20005.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/20005.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/20005.cpp" new file mode 100644 index 0000000..ba1b844 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/20005.cpp" @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +struct node{ + int x, y, n, c; +}; + +int N, M, P; +string board[1000]; +queue q; +int dps[26]; +int hp; +bool attack[26]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; +bool visited[1000][1000][26]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> P; + for(int i=0; i> board[i]; + for(int j=0; j= 'a' && board[i][j] <= 'z'){ + q.push({i,j,0,board[i][j]-'a'}); + visited[i][j][board[i][j]-'a']=true; + } + } + } + char tmp; + for(int i=0; i> tmp; + cin >> dps[tmp-'a']; + } + cin >> hp; + + int finaln = -1; + + while(!q.empty()){ + int x = q.front().x; + int y = q.front().y; + int n = q.front().n; + int c = q.front().c; + q.pop(); + + if(finaln != -1 && n > finaln) break; + + if(board[x][y] == 'B'){ + attack[c] = true; + hp -= dps[c]; + q.push({x,y,n+1,c}); + + if(hp <= 0){ + finaln = n; + } + continue; + } + + for(int i=0; i<4; i++){ + int nx = x+dx[i]; + int ny = y+dy[i]; + if(nx<0 || ny<0 || nx>=N || ny>=M || board[nx][ny]=='X' || visited[nx][ny][c]) continue; + q.push({nx,ny,n+1,c}); + visited[nx][ny][c]=true; + } + } + + int count = 0; + for(int i=0; i<26; i++){ + if(attack[i]) count++; + } + cout << count << "\n"; + + return 0; +} \ No newline at end of file From 8fd30e57ae3ce759cd99a3ef3ebbdf1c6820da4d Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 24 Jul 2021 20:00:05 +0900 Subject: [PATCH 175/203] =?UTF-8?q?=EA=B2=8C=EC=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1103.cpp" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1103.cpp" diff --git "a/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1103.cpp" "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1103.cpp" new file mode 100644 index 0000000..80cde86 --- /dev/null +++ "b/baekjoon/\352\267\270\353\236\230\355\224\204\355\203\220\354\203\211/1103.cpp" @@ -0,0 +1,49 @@ +#include +#include + +using namespace std; + +int N, M; +string board[50]; + +struct node{ + int x, y, n, k; +}; + +queue q; +bool visited[50][50]; +int DP[50][50]; +int dx[4] = {0,0,1,-1}; +int dy[4] = {1,-1,0,0}; + +int dfs(int x, int y){ + if(DP[x][y]!=0) return DP[x][y]; + int res = 0; + int k = board[x][y] - '0'; + visited[x][y] = true; + for(int i=0; i<4; i++){ + int nx = x+dx[i]*k; + int ny = y+dy[i]*k; + if(nx<0||ny<0||nx>=N||ny>=M||board[nx][ny]=='H') continue; + if(visited[nx][ny]) return DP[x][y] = 1000000; + res = max(res, dfs(nx, ny)); + } + visited[x][y] = false; + return DP[x][y] = res + 1; +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=0; i> board[i]; + } + + dfs(0,0); + if(DP[0][0] >= 1000000) cout << "-1\n"; + else cout << DP[0][0] << "\n"; + + return 0; +} \ No newline at end of file From 752cf93094d280449044b1f8798d10b9bd24df79 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 25 Jul 2021 21:43:16 +0900 Subject: [PATCH 176/203] =?UTF-8?q?=EB=A7=89=EB=8C=80=EB=B0=B0=EC=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/8895.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 baekjoon/DP/8895.cpp diff --git a/baekjoon/DP/8895.cpp b/baekjoon/DP/8895.cpp new file mode 100644 index 0000000..512aa9a --- /dev/null +++ b/baekjoon/DP/8895.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +long long DP[21][21][21]; +int T, N, L, R; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + DP[1][1][1] = 1; + cin >> T; + for(int t=0; t> N >> L >> R; + for(int i=2; i<=N; i++){ + for(int j=1; j<=L; j++){ + for(int k=1; k<=R; k++){ + DP[i][j][k] = DP[i-1][j-1][k] + DP[i-1][j][k-1] + (i-2)*DP[i-1][j][k]; + } + } + } + cout << DP[N][L][R] << "\n"; + } + + return 0; +} \ No newline at end of file From e0158d48099f415310caf49f7680b6aaf6669b10 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 26 Jul 2021 16:38:35 +0900 Subject: [PATCH 177/203] ABC --- baekjoon/DP/12969.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 baekjoon/DP/12969.cpp diff --git a/baekjoon/DP/12969.cpp b/baekjoon/DP/12969.cpp new file mode 100644 index 0000000..550e9fd --- /dev/null +++ b/baekjoon/DP/12969.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +int N, K; +// 30개중에 A,B가 몇개있는데 +bool DP[31][31][31][436]; +int A, B; +bool flag = false; +string ans = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + +bool solve(int n, int a, int b, int k){ + if(DP[n][a][b][k] == true) return false; + DP[n][a][b][k] = true; + + if(n==1){ + if(k==0){ + if(a+b==1){ + if(a==1) ans[n-1] = 'A'; + else ans[n-1] = 'B'; + } + else if(a+b==0){ + ans[n-1] = 'C'; + } + else return false; + return true; + } + return false; + } + + ans[n-1] = 'A'; + if(a-1>=0 && solve(n-1, a-1, b, k)) return true; + + ans[n-1] = 'B'; + if(k-a>=0 && b-1>=0 && solve(n-1, a, b-1, k-a)) return true; + + ans[n-1] = 'C'; + if(k-a-b>=0 && solve(n-1, a, b, k-a-b)) return true; + + return false; +} + +int main(){ + cin >> N >> K; + for(int a=0; a<=N && !flag; a++){ + for(int b=0; a+b<=N; b++){ + if(solve(N, a, b, K)){ + flag = true; + for(int c=0; c Date: Tue, 27 Jul 2021 00:01:13 +0900 Subject: [PATCH 178/203] =?UTF-8?q?=EC=86=8C=ED=98=95=EA=B8=B0=EA=B4=80?= =?UTF-8?q?=EC=B0=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2616.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 baekjoon/DP/2616.cpp diff --git a/baekjoon/DP/2616.cpp b/baekjoon/DP/2616.cpp new file mode 100644 index 0000000..0c418c3 --- /dev/null +++ b/baekjoon/DP/2616.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int N; +int A[50000]; +int D[50001][4]; +int S[50001]; +int T; + +int main(){ + cin >> N; + for(int i=0; i> A[i]; + cin >> T; + + S[1] = A[0]; + for(int i=2; i<=N; i++) S[i] = S[i-1]+A[i-1]; + + for(int i=1; i<=N; i++){ + for(int j=1; j<=3; j++){ + D[i][j] = D[i-1][j]; + if(i-T>=0) D[i][j] = max(D[i][j], D[i-T][j-1] + S[i]-S[i-T]); + cout << D[i][j] << " "; + } + cout << "\n"; + } + + cout << D[N][3] << "\n"; + + return 0; +} \ No newline at end of file From d08dd9c27795922eb995e1c08c1a36a7d16f3450 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 28 Jul 2021 14:21:51 +0900 Subject: [PATCH 179/203] =?UTF-8?q?=EC=84=A0=EB=B6=84=20=EA=B5=90=EC=B0=A8?= =?UTF-8?q?=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../17387.cpp" | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git "a/baekjoon/\353\213\250\352\270\260\352\260\204\354\204\261\354\236\245/17387.cpp" "b/baekjoon/\353\213\250\352\270\260\352\260\204\354\204\261\354\236\245/17387.cpp" index 2a6a3fc..990ba1d 100644 --- "a/baekjoon/\353\213\250\352\270\260\352\260\204\354\204\261\354\236\245/17387.cpp" +++ "b/baekjoon/\353\213\250\352\270\260\352\260\204\354\204\261\354\236\245/17387.cpp" @@ -25,37 +25,43 @@ int main() int res3 = CCW(x3, y3, x4, y4, x1, y_1); int res4 = CCW(x3, y3, x4, y4, x2, y2); - if (res1 * res2 < 0) - { - if (res3 * res4 <= 0) - { - cout << "1\n"; - return 0; + if(res1==0 && res2==0 && res3==0 && res4==0){ + if(x1>x2){ + swap(x1, x2); + swap(y_1, y2); } - } - else if (res1 == 0 && res2 == 0) - { - if ((x3 >= x1 && x3 <= x2) || (x3 >= x2 && x3 <= x1)) - { - if ((y3 >= y_1 && y3 <= y2) || (y3 >= y2 && y3 <= y_1)) - { + if(x3>x4){ + swap(x3, x4); + swap(y3, y4); + } + if(x1>x3){ + swap(x1, x3); swap(y_1, y3); swap(x2, x4); swap(y2, y4); + } + + if(x1==x2 && x2==x3 &&x3==x4){ + if(y_1>y2) swap(y_1, y2); + if(y3>y4) swap(y3, y4); + if(y_1>y3){ + swap(y_1, y3); + swap(y2, y4); + } + if(y3>=y_1&&y3<=y2){ cout << "1\n"; return 0; } - } - if ((x4 >= x1 && x4 <= x2) || (x4 >= x2 && x4 <= x1)) - { - if ((y4 >= y_1 && y4 <= y2) || (y4 >= y2 && y4 <= y_1)) - { - cout << "1\n"; + else{ + cout << "0\n"; return 0; } } + + if(x3>=x1&&x3<=x2){ + cout << "1\n"; + return 0; + } } - else if (res1 == 0 || res2 == 0) - { - if (res3 * res4 <= 0) - { + else{ + if(res1*res2<=0 && res3*res4<=0){ cout << "1\n"; return 0; } From acaa0ecb5a6f28724d57b7d078e71e7e0fe15667 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 29 Jul 2021 15:02:01 +0900 Subject: [PATCH 180/203] LCS 3 --- .../1958.cpp" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/1958.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/1958.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/1958.cpp" new file mode 100644 index 0000000..aabefc7 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/1958.cpp" @@ -0,0 +1,33 @@ +#include + +using namespace std; + +string A,B,C; +int DP[101][101][101]; +int ans = 0; + +int solve(int a, int b, int c){ + if(a>=A.length() || b>=B.length() || c>=C.length()) return 0; + if(DP[a][b][c] != -1) return DP[a][b][c]; + + int result = 0; + result = max(max(solve(a+1, b, c), solve(a, b+1, c)), solve(a,b,c+1)); + result = max(result, max(max(solve(a+1, b+1, c), solve(a+1, b, c+1)), solve(a, b+1, c+1))); + bool check = (A[a] == B[b] && B[b] == C[c]); + result = max(result, check + solve(a+1, b+1, c+1)); + return DP[a][b][c] = result; +} + +int main(){ + cin >> A >> B >> C; + + for(int i=0; i Date: Fri, 30 Jul 2021 11:23:58 +0900 Subject: [PATCH 181/203] =?UTF-8?q?=EC=9E=A0=EC=88=98=ED=95=A8=EC=8B=9D?= =?UTF-8?q?=EB=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2671.cpp" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/2671.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/2671.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/2671.cpp" new file mode 100644 index 0000000..36324cb --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/2671.cpp" @@ -0,0 +1,51 @@ +#include + +using namespace std; + +string str; + +int main(){ + + cin >> str; + char pre = '?'; + bool start = false; + bool end = false; + str += "??"; + for(int i=0; i Date: Sat, 31 Jul 2021 19:34:27 +0900 Subject: [PATCH 182/203] =?UTF-8?q?=EC=9C=A0=EC=A0=84=EC=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2306.cpp" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/2306.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/2306.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/2306.cpp" new file mode 100644 index 0000000..4b2bc5f --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/2306.cpp" @@ -0,0 +1,31 @@ +#include + +using namespace std; + +string str; +int DP[500][500]; + +int main(){ + cin >> str; + + for(int j=1; j=0; i--){ + DP[i][j] = max(DP[i+1][j], DP[i][j-1]); + + if(str[i] == 'a' && str[j] == 't'){ + DP[i][j] = max(DP[i][j], DP[i+1][j-1] + 2); + } + else if(str[i] == 'g' && str[j] == 'c'){ + DP[i][j] = max(DP[i][j], DP[i+1][j-1] + 2); + } + + for(int k=i+1; k+1 Date: Sun, 1 Aug 2021 18:56:43 +0900 Subject: [PATCH 183/203] =?UTF-8?q?=EB=8B=A8=EC=96=B4=20=EB=A7=9E=EC=B6=94?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../9081.cpp" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/9081.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/9081.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/9081.cpp" new file mode 100644 index 0000000..5cf88d9 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/9081.cpp" @@ -0,0 +1,71 @@ +#include +#include + +using namespace std; + +vector stack; +int T; +string word[10]; +int alpha[26]; +int total = 0; +bool final = false; + +int solve(int now, int n, string s, bool flag){ + + if(final) return 0; + + if(n == word[now].length()){ + if(flag){ + stack.push_back(s); + total++; + final = true; + return 1; + } + return 0; + } + + int res = 0; + for(int i=0; i<26; i++){ + if(alpha[i] > 0){ + if(!flag && word[now][n] - 'A' <= i){ + alpha[i]--; + res += solve(now, n+1, s+char('A'+i), word[now][n]-'A' != i); + alpha[i]++; + } + else if(flag){ + alpha[i]--; + res += solve(now, n+1, s+char('A'+i), true); + alpha[i]++; + } + } + } + + return res; +} + +int main(){ + + cin >> T; + for(int i=0; i> word[i]; + for(int j=0; j<26; j++){ + alpha[j] = 0; + } + for(unsigned int j=0; j0){ + cout << stack[0] << "\n"; + } + else{ + cout << word[i] << "\n"; + } + } + + return 0; +} \ No newline at end of file From 168e8ad352f55357259e4213a47bfd46ef659179 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 2 Aug 2021 12:32:15 +0900 Subject: [PATCH 184/203] =?UTF-8?q?=EB=82=98=EB=8A=94=20=EC=B9=9C=EA=B5=AC?= =?UTF-8?q?=EA=B0=80=20=EC=A0=81=EB=8B=A4(KMP)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16172.cpp" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/16172.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/16172.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/16172.cpp" new file mode 100644 index 0000000..9ef5905 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/16172.cpp" @@ -0,0 +1,47 @@ +#include + +using namespace std; + +string S, K; +int kmp[200000]; +int D[200000]; + +int main(){ + cin >> S >> K; + for(int i=0; i 0){ + j = kmp[j-1]; + } + + if(K[j]==K[i]){ + kmp[i] = j++; + } + } + + if(S[0] == K[0]) D[0] = 1; + + j = 0; + for(int i=0; i= '0' && S[i] <= '9'){ + D[i] = D[i-1]; + continue; + } + + while(K[j] != S[i] && j > 0){ + j = kmp[j-1] + 1; + } + if(K[j] == S[i]) D[i]=++j; + + if(D[i] == K.length()){ + cout << "1\n"; + return 0; + } + } + + cout << "0\n"; + + return 0; +} \ No newline at end of file From 4e8e178b19598d13f5cd2a544b460fdcdbec67d0 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 3 Aug 2021 00:10:22 +0900 Subject: [PATCH 185/203] =?UTF-8?q?=ED=9A=8C=EB=AC=B8=EC=9D=80=ED=9A=8C?= =?UTF-8?q?=EB=AC=B8=EC=95=84=EB=8B=88=EC=95=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../15927.cpp" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/15927.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/15927.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/15927.cpp" new file mode 100644 index 0000000..bd5b999 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/15927.cpp" @@ -0,0 +1,35 @@ +#include + +using namespace std; + +string str; + +int main(){ + + cin >> str; + + if(str[0] != str[str.length()-1]){ + cout << str.length() << "\n"; + return 0; + } + + bool pelindrome = true; + bool all_same = true; + + for(int i=0; i Date: Wed, 4 Aug 2021 20:24:16 +0900 Subject: [PATCH 186/203] =?UTF-8?q?=EC=8B=9C=EC=A0=80=20=EC=95=94=ED=98=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1893.cpp" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/1893.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/1893.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/1893.cpp" new file mode 100644 index 0000000..074d8b4 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/1893.cpp" @@ -0,0 +1,83 @@ +#include +#include +#include + +using namespace std; + +int T; +string A, W, S; +int kmp[50000]; +int ans[100]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> T; + for(int t=0; t> A >> W >> S; + + for(int i=0; i cmap; + for(int i=0; i0 && W[i] != W[j]){ + j = kmp[j-1]; + } + if(W[i] == W[j]) + kmp[i] = ++j; + } + + j=0; + for(int i=0; i0 && S[i] != W[j]){ + j = kmp[j-1]; + } + + if(S[i] == W[j]){ + if(j==W.length()-1){ + ans[shift]++; + j = kmp[j]; + } + else j++; + } + } + } + + int count = 0; + vector ans_list; + + for(int i=0; i Date: Thu, 5 Aug 2021 14:43:37 +0900 Subject: [PATCH 187/203] =?UTF-8?q?=EC=95=A0=EB=84=88=EA=B7=B8=EB=9E=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../6443.cpp" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/6443.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/6443.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/6443.cpp" new file mode 100644 index 0000000..96e7878 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/6443.cpp" @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int N; +string str; +int alpha[26]; + +void solve(int n, string s){ + + if(n==str.length()){ + cout << s << "\n"; + return; + } + + for(int i=0; i<26; i++){ + if(alpha[i] > 0){ + alpha[i]--; + solve(n+1, s + (char)('a'+i)); + alpha[i]++; + } + } +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + + cin >> N; + for(int n=0; n> str; + for(int i=0; i<26; i++) + alpha[i] = 0; + + for(int i=0; i Date: Fri, 6 Aug 2021 12:57:46 +0900 Subject: [PATCH 188/203] =?UTF-8?q?=EC=86=8C=EC=88=98=EC=9D=B8=20=ED=8C=B0?= =?UTF-8?q?=EB=A6=B0=EB=93=9C=EB=A1=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "baekjoon/\352\265\254\355\230\204/1990.cpp" | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "baekjoon/\352\265\254\355\230\204/1990.cpp" diff --git "a/baekjoon/\352\265\254\355\230\204/1990.cpp" "b/baekjoon/\352\265\254\355\230\204/1990.cpp" new file mode 100644 index 0000000..fde6c4a --- /dev/null +++ "b/baekjoon/\352\265\254\355\230\204/1990.cpp" @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +using namespace std; + +int A, B; +vector pelindrome; + +void solve(int n, string str){ + if(n>0 && n<=8){ + if(str[0] == '0'){ + ; + } + else if(A <= atoi(str.c_str()) && atoi(str.c_str()) <= B){ + pelindrome.push_back(atoi(str.c_str())); + } + } + else if(n>8){ + return; + } + + if(n==0){ + for(int i=0; i<=9; i++){ + solve(n+1, str + char(i+'0')); + } + } + for(int i=0; i<=9; i++){ + solve(n+2, char(i+'0') + str + char(i+'0')); + } +} + +int main(){ + ios::sync_with_stdio(0); + cout.tie(0); + cin >> A >> B; + + solve(0, ""); + + sort(pelindrome.begin(), pelindrome.end()); + for(int p:pelindrome){ + bool prime = true; + for(int i=2; i<=floor(sqrt(p)); i++){ + if(p%i==0){ + prime = false; + break; + } + } + if(prime) cout << p << "\n"; + } + + cout << "-1\n"; + return 0; +} \ No newline at end of file From 9721179f8b8aed0e45350b57c1c9bc756269d221 Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 7 Aug 2021 09:15:33 +0900 Subject: [PATCH 189/203] =?UTF-8?q?=EC=86=8C=EC=88=98=20=EB=B6=80=EB=B6=84?= =?UTF-8?q?=20=EB=AC=B8=EC=9E=90=EC=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5636.cpp" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/5636.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/5636.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/5636.cpp" new file mode 100644 index 0000000..d9c2944 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/5636.cpp" @@ -0,0 +1,58 @@ +#include + +using namespace std; + +string str; +bool notprime[100000]; + +int main(){ + + notprime[0] = true; + notprime[1] = true; + + for(int i=2; i<100000; i++){ + if(notprime[i]) continue; + + for(int j=2; i*j<100000; j++){ + if(!notprime[i*j]){ + notprime[i*j] = true; + } + } + } + + while(true){ + cin >> str; + if(str=="0") break; + + int ans = 0; + + for(int i=0; i Date: Sun, 8 Aug 2021 15:02:52 +0900 Subject: [PATCH 190/203] =?UTF-8?q?36=EC=A7=84=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1036.py" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/1036.py" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/1036.py" "b/baekjoon/\352\267\270\353\246\254\353\224\224/1036.py" new file mode 100644 index 0000000..0da0582 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/1036.py" @@ -0,0 +1,44 @@ +N = int(input()) +digit = [(i, 0) for i in range(36)] + +for i in range(N): + number = input() + for n in range(len(number)-1, -1, -1): + tmp = number[n] + if ord(tmp) >= ord('0') and ord(tmp) <= ord('9'): + tmp = int(tmp) + else: + tmp = ord(tmp) - ord('A') + 10 + + digit[tmp] = (tmp, digit[tmp][1] + pow(36, len(number)-1-n)) + +K = int(input()) + +check = [] +for i in range(36): + check.append((digit[i][1]*(35-i), i)) + +check.sort(reverse=True) + +for i in range(K): + digit[check[i][1]] = (35, digit[check[i][1]][1]) + +sum35 = 0 +for i in range(36): + sum35 += digit[i][0] * digit[i][1] + +def to36(n): + if n < 36: + if n >= 0 and n <= 9: + print(n, end='') + else: + print(chr(ord('A')+n-10), end='') + return + + to36(n//36) + if n%36 >= 0 and n%36 <= 9: + print(n%36, end='') + else: + print(chr(ord('A')+n%36-10), end='') + +to36(sum35) \ No newline at end of file From 87c633040267a9f242f8ac827003ddc347f65454 Mon Sep 17 00:00:00 2001 From: b-chae Date: Mon, 9 Aug 2021 17:29:24 +0900 Subject: [PATCH 191/203] =?UTF-8?q?=EA=B4=84=ED=98=B8=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=ED=95=98=EA=B8=B01?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16637.cpp" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/16637.cpp" diff --git "a/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/16637.cpp" "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/16637.cpp" new file mode 100644 index 0000000..9cd7a45 --- /dev/null +++ "b/baekjoon/\353\270\214\353\243\250\355\212\270\355\217\254\354\212\244/16637.cpp" @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int N; +string str; + +int op(int x, int y, char c){ + if(c=='+') return x+y; + else if(c=='-') return x-y; + else return x*y; +} + +int solve(int idx, int tmp){ + + if(idx>=N) return tmp; + + int res = -2147483647; + + int y = str[idx+1] - '0'; + res = max(res, solve(idx+2, op(tmp, y, str[idx]))); + + if(idx+4 <= N){ + int z = str[idx+3] - '0'; + res = max(res, solve(idx+4, op(tmp, op(y, z, str[idx+2]), str[idx]))); + } + + cout << res << "\n"; + return res; +} + +int main(){ + + cin >> N; + cin >> str; + cout << solve(1, str[0]-'0') << "\n"; + + return 0; +} \ No newline at end of file From 3b5a59cdc436df76d1aa4358678a328bd77c2c01 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 10 Aug 2021 22:40:25 +0900 Subject: [PATCH 192/203] =?UTF-8?q?=EA=B5=AC=EA=B0=84=20=EB=82=98=EB=88=84?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baekjoon/DP/2228.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 baekjoon/DP/2228.cpp diff --git a/baekjoon/DP/2228.cpp b/baekjoon/DP/2228.cpp new file mode 100644 index 0000000..8aaf77e --- /dev/null +++ b/baekjoon/DP/2228.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int N, M; + +int DP[101][51]; +int A[101]; + +int solve(int n, int M){ + + if(M==0) return 0; + if(n<=0) return -3276800; + if(DP[n][M] != 0) return DP[n][M]; + + DP[n][M] = solve(n-1, M); + int sum = 0; + for(int i=n; i>0; i--){ + sum += A[i]; + DP[n][M] = max(DP[n][M], solve(i-2, M-1) + sum); + } + return DP[n][M]; +} + +int main(){ + + cin >> N >> M; + + for(int i=1; i<=N; i++){ + cin >> A[i]; + } + + cout << solve(N, M); + + return 0; +} \ No newline at end of file From 97ca7cc6e31080eb281b60ef3894f7102824a9c9 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 11 Aug 2021 12:59:39 +0900 Subject: [PATCH 193/203] =?UTF-8?q?=EC=A0=95=EB=B3=B5=EC=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14950.cpp" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/14950.cpp" diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/14950.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/14950.cpp" new file mode 100644 index 0000000..a48966a --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/14950.cpp" @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; + +int N, M, T; +priority_queue< pair > > pq; +int p[10001]; + +int getp(int n){ + if(p[n] == n) return n; + return p[n] = getp(p[n]); +} + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M >> T; + + for(int i=1; i<=N; i++){ + p[i] = i; + } + + for(int i=0; i> x >> y >> z; + pq.push(make_pair(-z, make_pair(x,y))); + } + + int selected = 0; + int offset = 0; + long long ans = 0; + while(!pq.empty() && selected < N-1){ + int c = -pq.top().first; + int x = pq.top().second.first; + int y = pq.top().second.second; + pq.pop(); + + if(getp(x) != getp(y)){ + p[p[x]] = p[y]; + ans += c + offset; + offset += T; + selected++; + } + } + + cout << ans << "\n"; + + return 0; +} \ No newline at end of file From daa55a161661bd823ccda807c62cf97c8b23b444 Mon Sep 17 00:00:00 2001 From: b-chae Date: Wed, 11 Aug 2021 13:49:32 +0900 Subject: [PATCH 194/203] =?UTF-8?q?=EC=95=88=EC=A0=95=EC=A0=81=EC=9D=B8=20?= =?UTF-8?q?=EB=84=A4=ED=8A=B8=EC=9B=8C=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2406.cpp" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/2406.cpp" diff --git "a/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/2406.cpp" "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/2406.cpp" new file mode 100644 index 0000000..087bb56 --- /dev/null +++ "b/baekjoon/\354\265\234\354\206\214\354\212\244\355\214\250\353\213\235\355\212\270\353\246\254/2406.cpp" @@ -0,0 +1,67 @@ +#include +#include +#include + +using namespace std; + +int N, M; +int p[1001]; +priority_queue< pair > > pq; +int cost[1001][1001]; +vector< pair > ans; + +int getp(int n){ + if(n == p[n]) return n; + return p[n] = getp(p[n]); +} + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + + for(int i=1; i<=N; i++) p[i] = i; + + int selected = 0; + + for(int i=0; i> x >> y; + if(getp(x) != getp(y)){ + selected++; + p[p[x]] = p[y]; + } + } + + for(int i=1; i<=N; i++){ + for(int j=1; j<=N; j++){ + cin >> cost[i][j]; + if(i!=1 && i a:ans){ + cout << a.first << " " << a.second << "\n"; + } + + return 0; +} \ No newline at end of file From c18907cc06839549aa93bca29efc93dddb867937 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 12 Aug 2021 13:03:22 +0900 Subject: [PATCH 195/203] =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=A0=95=ED=95=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16900.cpp" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/16900.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/16900.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/16900.cpp" new file mode 100644 index 0000000..26afd93 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/16900.cpp" @@ -0,0 +1,27 @@ +#include + +using namespace std; + +string S; +int K; +int fail[500000]; + +int main(){ + + cin >> S >> K; + + int j = 0; + + for(int i=1; i0 && S[i]!=S[j]){ + j = fail[j-1]; + } + if(S[i]==S[j]){ + fail[i] = ++j; + } + } + + cout << S.length() + (S.length()-fail[S.length()-1])*(K-1) << "\n"; + + return 0; +} \ No newline at end of file From 159d98c79ebe9d337ddf28fe9d240890117bc0a2 Mon Sep 17 00:00:00 2001 From: b-chae Date: Fri, 13 Aug 2021 22:20:00 +0900 Subject: [PATCH 196/203] =?UTF-8?q?BFS=20=EC=8A=A4=ED=8E=98=EC=85=9C=20?= =?UTF-8?q?=EC=A0=80=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16940.cpp" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/16940.cpp" diff --git "a/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/16940.cpp" "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/16940.cpp" new file mode 100644 index 0000000..44f1401 --- /dev/null +++ "b/baekjoon/\353\204\210\353\271\204\354\232\260\354\204\240\355\203\220\354\203\211/16940.cpp" @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +int N; +bool visited[100001]; +vector nextv[100001]; +queue< pair > q; +int check_ans[100001]; +int p[100001]; +int order[100001]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i=0; i> x >> y; + nextv[x].push_back(y); + nextv[y].push_back(x); + } + + for(int i=0; i> check_ans[i]; + } + + q.push(make_pair(1, 1)); + visited[1] = true; + + while(!q.empty()){ + int x = q.front().first; + int l = q.front().second; + q.pop(); + + for(int next:nextv[x]){ + if(!visited[next]){ + q.push(make_pair(next, l+1)); + visited[next] = true; + p[next] = x; + } + } + } + + for(int i=0; i<=N; i++){ + visited[i] = false; + } + + visited[0] = true; + int porder = -1; + for(int i=0; i order[p[check_ans[i]]]){ + cout << "0\n"; + return 0; + } + + porder = order[p[check_ans[i]]]; + visited[check_ans[i]] = true; + order[check_ans[i]] = i; + } + + cout << "1\n"; + + return 0; +} \ No newline at end of file From a0973ef909997578a91761b38aa5eeff874f637b Mon Sep 17 00:00:00 2001 From: b-chae Date: Sat, 14 Aug 2021 17:34:03 +0900 Subject: [PATCH 197/203] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EC=9E=98?= =?UTF-8?q?=EB=9D=BC=EB=82=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2866.cpp" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/2866.cpp" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/2866.cpp" "b/baekjoon/\353\254\270\354\236\220\354\227\264/2866.cpp" new file mode 100644 index 0000000..a53e62a --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/2866.cpp" @@ -0,0 +1,57 @@ +#include +#include + +using namespace std; + +int N, M; +string tmp[1000]; +string board[1000]; +bool DP[1000][1000]; + + +int main(){ + + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N >> M; + for(int i=0; i> tmp[i]; + } + + for(int i=0; i=0; j--){ + board[i] += tmp[j][i]; + } + } + + sort(board, board+M); + + int ans = 0; + bool flag = true; + + for(int i=1; i Date: Sun, 15 Aug 2021 22:28:15 +0900 Subject: [PATCH 198/203] =?UTF-8?q?=ED=96=89=EB=B3=B5=EC=9C=A0=EC=B9=98?= =?UTF-8?q?=EC=9B=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../13164.cpp" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/13164.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/13164.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/13164.cpp" new file mode 100644 index 0000000..1f9fd7f --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/13164.cpp" @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +int N, K; +int A[300000]; +int diff[300000]; + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> K; + + for(int i=0; i> A[i]; + } + + for(int i=0; i Date: Mon, 16 Aug 2021 15:40:55 +0900 Subject: [PATCH 199/203] =?UTF-8?q?=EB=B9=84=EB=93=9C=EB=A7=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../19590.cpp" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/19590.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/19590.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/19590.cpp" new file mode 100644 index 0000000..7f80a7e --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/19590.cpp" @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +int N, maxv; +long long total; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + for (int i = 0; i < N; i++) { + int a; + cin >> a; + total += a; + maxv = max(maxv, a); + } + if (total - maxv > maxv) { + cout << (total & 1); + } else { + cout << maxv - (total - maxv); + } + + return 0; +} From eae86546eb45335e193124bf1736a13f96296101 Mon Sep 17 00:00:00 2001 From: b-chae Date: Tue, 17 Aug 2021 00:27:55 +0900 Subject: [PATCH 200/203] =?UTF-8?q?=EB=B9=84=EC=8A=B7=ED=95=9C=20=EC=88=9C?= =?UTF-8?q?=EC=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2413.cpp" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "baekjoon/\352\267\270\353\246\254\353\224\224/2413.cpp" diff --git "a/baekjoon/\352\267\270\353\246\254\353\224\224/2413.cpp" "b/baekjoon/\352\267\270\353\246\254\353\224\224/2413.cpp" new file mode 100644 index 0000000..466b764 --- /dev/null +++ "b/baekjoon/\352\267\270\353\246\254\353\224\224/2413.cpp" @@ -0,0 +1,39 @@ +#include + +using namespace std; + +bool used[50001]; +int check[50001]; +int A[50000]; +int N; + +int main(){ + cin >> N; + + for(int i=0; i> A[i]; + } + + for(int i=1; i<=N; i++){ + check[i] = i; + } + + for(int i=0; i Date: Wed, 18 Aug 2021 17:30:14 +0900 Subject: [PATCH 201/203] Create ls --- baekjoon/DP/ls | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 baekjoon/DP/ls diff --git a/baekjoon/DP/ls b/baekjoon/DP/ls new file mode 100644 index 0000000..da82615 --- /dev/null +++ b/baekjoon/DP/ls @@ -0,0 +1,56 @@ +#include +#include + +using namespace std; + +string target; +string files[100]; +int N; +vector ans; +int DP[100][105][105]; + +bool solve(int n, int i, int j){ + bool res = false; + if(DP[n][i][j]!=-1) return DP[n][i][j]; + if(i==files[n].length() && j==target.length()) return DP[n][i][j] = true; + if(i>files[n].length()) return DP[n][i][j] = false; + if(j>=target.length()) return DP[n][i][j] = false; + + if(target[j] == '*'){ + res = res || solve(n, i, j+1); + if(i<=files[n].length()) + res = res || solve(n, i+1, j); + if(i<=files[n].length()) + res = res || solve(n, i+1, j+1); + } + else if(i> target >> N; + + for(int i=0; i> files[i]; + if(solve(i, 0, 0)){ + ans.push_back(files[i]); + } + } + + for(string s:ans){ + cout << s << "\n"; + } + + return 0; +} From 25937992ee43c329bb4aa76d4bf5cec71f7f9386 Mon Sep 17 00:00:00 2001 From: b-chae Date: Thu, 19 Aug 2021 16:16:49 +0900 Subject: [PATCH 202/203] IPv6 --- .../3107.py" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 "baekjoon/\353\254\270\354\236\220\354\227\264/3107.py" diff --git "a/baekjoon/\353\254\270\354\236\220\354\227\264/3107.py" "b/baekjoon/\353\254\270\354\236\220\354\227\264/3107.py" new file mode 100644 index 0000000..e9defa2 --- /dev/null +++ "b/baekjoon/\353\254\270\354\236\220\354\227\264/3107.py" @@ -0,0 +1,20 @@ +ipv6 = input() + +if ipv6.startswith("::"): + ipv6 = ipv6[1:] +elif ipv6.endswith("::"): + ipv6 = ipv6[:-1] + +ipv6 = ipv6.split(":") + +for i in range(8): + if len(ipv6[i]) >= 1 and len(ipv6[i]) <= 3: + ipv6[i] = "0" * (4-len(ipv6[i])) + ipv6[i] + elif len(ipv6[i]) == 0: + ipv6.pop(i) + for j in range(8-len(ipv6)): + ipv6.insert(i, "0000") + +for i in range(7): + print(ipv6[i], end=':') +print(ipv6[7]) From d5d2f01bcfbff56f23c05b1238d3b060169b732f Mon Sep 17 00:00:00 2001 From: b-chae Date: Sun, 12 Sep 2021 09:15:49 +0900 Subject: [PATCH 203/203] kakao blind coding test --- programmers/210912.py | 161 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 programmers/210912.py diff --git a/programmers/210912.py b/programmers/210912.py new file mode 100644 index 0000000..ae47416 --- /dev/null +++ b/programmers/210912.py @@ -0,0 +1,161 @@ +import math + +def solution(id_list, reports, k): + answer = [] + + cross_check = {} + count = {} + for i in range(len(id_list)): + cross_check[id_list[i]] = {} + cross_check[id_list[i]]['#'] = 0 + count[id_list[i]] = [] + + for report in reports: + reporter = report.split()[0] + reported = report.split()[1] + + if reported not in cross_check[reporter]: + cross_check[reporter][reported] = True + count[reported].append(reporter) + + answer = [] + + for id in id_list: + if len(count[id]) >= k: + for reporter in count[id]: + cross_check[reporter]['#'] += 1 + + for id in id_list: + answer.append(cross_check[id]['#']) + + return answer + +##################################### + +def dectok(n, k): + if n < k: + return str(n) + + return dectok(n//k, k) + str(n%k) + +def solve(n, left, right): + + if left >= right: + return 0 + + flag = False + idx = 0 + + for i in range(left, right): + if n[i] == "0": + flag = True + idx = i + break + + if flag: + return solve(n, left, idx) + solve(n, idx+1, right) + + flag = False + for i in range(2, int(math.sqrt(int(n[left:right])))+1): + if int(n[left:right]) % i == 0: + flag = True + break + + if int(n[left:right]) != 1 and flag == False: + print(int(n[left:right])) + return 1 + return 0 + +def solution2(n, k): + n = dectok(n, k) + + result = solve(n, 0, len(n)) + + return result + +def get_time(in_time, out_time): + in_time = int(in_time.split(":")[0])*60 + int(in_time.split(":")[1]) + + out_time = int(out_time.split(":")[0])*60 + int(out_time.split(":")[1]) + + return out_time - in_time + +############################### + +def get_fee(fees, park_time): + + if park_time <= fees[0]: + return fees[1] + + redunt = park_time - fees[0] + if redunt % fees[2] == 0: + return fees[1] + redunt // fees[2] * fees[3] + + return fees[1] + (redunt // fees[2] + 1) * fees[3] + +def solution3(fees, records): + + car_timer = {} + car_total_time = {} + car_fees = {} + + for record in records: + timer, car_num, inout = map(str, record.split()) + if inout == "IN": + car_timer[car_num] = timer + if car_num not in car_total_time: + car_total_time[car_num] = 0 + else: + car_total_time[car_num] += get_time(car_timer[car_num], timer) + car_timer[car_num] = "23:59" + + for car_num in car_timer.keys(): + car_total_time[car_num] += get_time(car_timer[car_num], "23:59") + car_fees[car_num] = get_fee(fees, car_total_time[car_num]) + + return [y[1] for y in sorted(car_fees.items(), key=lambda x : x[0])] + +print(solution3([1, 461, 1, 10],["00:00 1234 IN"])) + +############################### + +def solve4(n, info, arrow, score): + + arrow = list(arrow) + + if sum(arrow) > n: + return -1000000, [0,0,0,0,0,0,0,0,0,0,0] + + if score > 10: + return 0, tuple(arrow) + + origin = arrow[score] + arrow[score] += info[score]+1 + total1, res1 = solve4(n, info, tuple(arrow), score+1) + if total1 != -1000000: + total1 += 10-score + + arrow[score] = origin + total2, res2 = solve4(n, info, tuple(arrow), score+1) + if total2 != -1000000: + if info[score]>0: + total2 -= 10-score + + if total1 == -1000000 and total2 == -1000000: + return -1000000, [0,0,0,0,0,0,0,0,0,0,0] + if total1 > total2: + return total1, res1 + return total2, res2 + +def solution4(n, info): + answer = solve4(n, info, [0,0,0,0,0,0,0,0,0,0,0], 0) + if answer[0] < 0: + return [-1] + + answer = list(answer[1]) + if sum(answer) < n: + answer[10] += n-sum(answer) + return answer + +print(solution4(5, [2,1,1,1,0,0,0,0,0,0,0])) +