๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š ์ „๊ณต ๊ณต๋ถ€/๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•

(11)
[๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•] 11. ๋™์  ๊ณ„ํš๋ฒ• ๋™์  ๊ณ„ํš๋ฒ• (DP) ์ตœ์ ํ™” ๋ฌธ์ œ, ๋ถ„ํ•  ์ •๋ณต ํ•œ ๋ฌธ์ œ๋ฅผ ๋˜‘๊ฐ™์€ ๋ฌธ์ œ์ด๋ฉด์„œ ํฌ๊ธฐ๋งŒ ์ž‘์€ ๊ฒƒ์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์„์ง€ ์ƒ๊ฐํ•ด๋ณธ๋‹ค. ๊ธˆํ™” ๋ชจ์œผ๊ธฐ ๋ฌธ์ œ D[i][j] : (i, j)๊นŒ์ง€ ์˜ค๋Š” ๋™์•ˆ ๋ชจ์„ ์ˆ˜ ์žˆ๋Š” ๊ธˆํ™”์˜ ์ตœ๋Œ“๊ฐ’ D[i][j] = max( D[i-1],[j] , D[i][j-1] ) + map[i][j]; ์ตœ๋Œ€ ๊ณต๋ฐฑ ์ •์‚ฌ๊ฐํ˜• (ํฐ์ƒ‰์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•) mm ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์ด ์žˆ๋‹ค๋ฉด, 4๊ฐœ์˜ (m-1)(m-1) ํฌ๊ธฐ ์ •์‚ฌ๊ฐํ˜•์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์—์„œ ์ฐฉ์•ˆํ•œ๋‹ค. D[x][y]๋Š” (x,y)๊ฐ€ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ๊ผญ์ง€์ ์ธ ์ •์‚ฌ๊ฐํ˜•์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. (x,y)๊ฐ€ ๊ฒ€์€์ƒ‰์ด๋ฉด, D[x][y] = 0 ํฐ์ƒ‰์ด๋ฉด, x==1 && y==1 : D[x][y] = 1 else : D[x][y] = min(..
[๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•] 10. ๊ณ„์‚ฐ ๊ธฐํ•˜ ๊ณ„์‚ฐ ๊ธฐํ•˜ 2, 3์ฐจ์› ๊ณต๊ฐ„์ƒ์—์„œ ์ , ์„ , ๋„ํ˜• ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฌธ์ œ ๊ธฐ๋ณธ ๊ฐ€์ • : 2์ฐจ์› ๊ณต๊ฐ„, ์ •์ˆ˜์ขŒํ‘œ๋งŒ ๊ณ ๋ คํ•จ. ์‹ค์ˆ˜ ์—ฐ์‚ฐ์€ ์ง€์–‘ polygon : ์„ ๋ถ„๋“ค๋กœ ์ด๋ค„์ง„ ๋‹ซํžŒ ๋„ํ˜•. ๋‘ ์„ ๋ถ„์ด ๋งŒ๋‚˜๋Š” ์ ์€ ํ•˜๋‚˜๋ฟ์ด๋‹ค. ๋ชจ๋“  ์ ์„ ์ง€๋‚˜๋Š” ๊ฒฝ๋กœ n๊ฐœ์˜ ์ ์ด ์ฃผ์–ด์ง€๋ฉด, ์ด ์ ๋“ค์„ ๋ชจ๋‘ ์ง€๋‚˜๊ณ  ์‹œ์ž‘์ ์œผ๋กœ ๋Œ์•„์˜ค๋Š” ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜์‹œ์˜ค. ๋‹จ, ๊ต์ฐจํ•˜์ง€ ์•Š๊ฒŒ ํ’€์ด ๋ฐฉ๋ฒ• y์ขŒํ‘œ๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ์ ์„ ๊ธฐ์ค€์ ์œผ๋กœ ์žก์Œ. O(n) ์ด ์ ์„ ์ง€๋‚˜๋Š” ์ง์„ ๊ณผ ๋‹ค๋ฅธ ์ ๋“ค์„ ์ž‡๋Š” ์ง์„ ์„ ๋ชจ๋‘ ๊ตฌํ•˜๊ณ , ๊ฐ์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ์ •๋ ฌํ•œ๋‹ค. O(n log n). ๊ทธ๋ฆฌ๊ณ  ์ด ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•˜๋ฉด ๋จ ๊ฐ์˜ ๊ณ„์‚ฐ : arctan ํ•จ์ˆ˜์™€ ๋น„์Šทํ•œ ์„ฑ์งˆ์„ ๊ฐ€์ง€๊ณ , ๋ถ„๋ชจ๊ฐ€ 0์ธ ๊ฒฝ์šฐ๊ฐ€ ์—†๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ๊ณ„์‚ฐํ•œ๋‹ค. ์ ๊ณผ ํด๋ฆฌ๊ฑด์˜ ํฌํ•จ ๊ด€๊ณ„ ์ ์˜ ์ขŒ..
[๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•] 9. Flow Networks Flow Networks ๊ฐ€์ค‘๊ทธ๋ž˜ํ”„ G (๋ชจ๋“  ๊ฐ€์ค‘์น˜๋Š” ์–‘์ˆ˜) ์—์ง€์˜ ๊ฐ€์ค‘์น˜ = c(e) ์‹œ์ž‘์  s์—๋Š” ๋“ค์–ด์˜ค๋Š” ์—์ง€ ์—†๊ณ , ๋„์ฐฉ์  t์—๋Š” ๋‚˜๊ฐ€๋Š” ์—์ง€๊ฐ€ ์—†๋‹ค Flow : ์ด์šฉ๊ฐ€๋Šฅํ•œ ์šฉ๋Ÿ‰์„ ๊ธฐ๋ฐ˜์œผ๋กœ, ๊ฐ„์„ ์„ ๋”ฐ๋ผ ์ด๋™ํ•˜๋Š” ๊ฐ’ ๋ชจ๋“  ์—์ง€์— ๋Œ€ํ•ด์„œ, 0 ≤ f(e) ≤ c(e) flow ๊ฐ’ ( |f| )์€ s์—์„œ ๋‚˜๊ฐ€๋Š” ํ”Œ๋กœ์šฐ ์ด๋Ÿ‰ = t๋กœ ๋“ค์–ด์˜ค๋Š” ํ”Œ๋กœ์šฐ ์ด๋Ÿ‰ ์ตœ๋Œ€ ํ”Œ๋กœ์šฐ = ์ตœ์†Œ cut ์ปท(cut) ์ฃผ์–ด์ง„ ๋…ธ๋“œ V๋ฅผ ๋‘ ์ง‘ํ•ฉ์œผ๋กœ ๋ถ„ํ•  ์ปท X์— ๋Œ€ํ•ด์„œ, f(X)๋Š” X๋ฅผ ์ง€๋‚˜๋Š” flow ์ด๋Ÿ‰ c(X)๋Š” X๋ฅผ ์ง€๋‚˜๋Š” ์—์ง€์˜ c๊ฐ’ ์ด๋Ÿ‰ ์ตœ๋Œ€ ํ”Œ๋กœ์šฐ ๊ตฌํ•˜๊ธฐ Ford-Fulkerson algorithm s→t ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š”๋‹ค. BFS ์ง„ํ–‰ ๊ฐ ๊ฒฝ๋กœ์˜ c(e) ์ตœ์†Ÿ๊ฐ’์„ m์ด๋ผ๊ณ  ํ•˜์ž ๊ฐ ๊ฒฝ๋กœ์˜ ์—์ง€๋งˆ๋‹ค, c(e) -=..
[๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•] 8. ์„œ๋กœ์†Œ์ธ ์ง‘ํ•ฉ์˜ ํ‘œํ˜„ (Disjoint Sets) ์„œ๋กœ์†Œ์ธ ์ง‘ํ•ฉ์˜ ํ‘œํ˜„ (Disjoint Sets) ์„œ๋กœ์†Œ์ธ ์ง‘ํ•ฉ : ์ „์ฒด์ง‘ํ•ฉ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ๋“ค ์ค‘, ๋‘˜์„ ๊ณ ๋ฅด๋ฉด ๊ต์ง‘ํ•ฉ=๊ณต์ง‘ํ•ฉ, ์ „์ฒด ํ•ฉ์ง‘ํ•ฉ=U U์˜ ๋‘ ์›์†Œ๊ฐ€ ๊ฐ™์€ ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ์›์†Œ์ธ์ง€ ์•„๋‹Œ์ง€ ์–ด๋–ป๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ? ์‹ ์žฅํŠธ๋ฆฌ : ๊ทธ๋ž˜ํ”„ G์˜ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•˜๋Š”, E์— ์†ํ•˜๋Š” ์—์ง€๋กœ ๋งŒ๋“  ํŠธ๋ฆฌ ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ : ํŠธ๋ฆฌ์— ์†ํ•œ ๊ฐ€์ค‘์น˜์˜ ํ•ฉ์ด ๊ฐ€์žฅ ์ž‘์€ ์‹ ์žฅ ํŠธ๋ฆฌ. ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ (MST) ๋งŒ๋“ค๊ธฐ Prim’s algorithm : ํ˜„์žฌ ์ง‘ํ•ฉ(๋…ธ๋“œ๋“ค)๊ณผ ์—ฐ๊ฒฐ๋œ ์—์ง€ ์ค‘ ๊ฐ€์ค‘์น˜๊ฐ€ ์ตœ์†Œ์ธ ๊ฒƒ์„ MST ์ง‘ํ•ฉ์— ํฌํ•จ์‹œํ‚ด. ๊ตฌํ˜„๋ฒ•์— ๋”ฐ๋ผ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ฐจ์ด๋‚จ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ = ํŠน์ • ์‹œ์ž‘๋…ธ๋“œ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ๋…ธ๋“œ๋“ค๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•จ. ์œ /๋ฌดํ–ฅ ๊ทธ๋ž˜ํ”„ ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ = ๋ชจ๋“  ๋…ธ๋“œ๋“ค์„ ์ตœ์†Œ๋น„์šฉ์œผ๋กœ ์—ฐ๊ฒฐ / ๋‘ ๋…ธ๋“œ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋Š” ์ตœ..
[๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•] 7. ์ด๋ถ„ ํƒ์ƒ‰ 1. ํƒ์ƒ‰ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ N๊ฐœ์˜ ์ •์ˆ˜ ๋ฐฐ์—ด A[0], ..., A[N-1]์ด ์žˆ๋‹ค. X๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, A[i] = X๋ผ๋ฉด i๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ find(N, X)๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ด ํ•จ์ˆ˜๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ compare(i, val)์„ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ด๋Š” A[i]=val์ด๋ฉด 0, A[i] val์ด๋ฉด -1์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. N์€ ์ตœ๋Œ€ 100์ด๋‹ค. int find(int sub, int N, int X) { int start = 0, end = N-1, t; while (start 0) start = mid + 1; // ๊ธฐ์ค€๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด start๋ฅผ ๋‹น๊ธฐ๊ธฐ else if (v) end = mid - 1; // ๊ธฐ์ค€๊ฐ’๋ณด๋‹ค ํฌ๋ฉด end๋ฅผ ๋‹น๊ธฐ๊ธฐ else return mid; }..
[๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•] 6. ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ 1. Segment Tree ๋ถ€๋ถ„ํ•ฉ ๋ฌธ์ œ์—์„œ ์‘์šฉ ๊ฐ€๋Šฅ ์ƒ์„ฑ ์ฝ”๋“œ 2. ํ•ฉ์„ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ [start, end]์˜ ํ•ฉ์„ ๊ตฌํ•˜๋ ค๋ฉด ๋ฃจํŠธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๋‹ค์Œ์„ ์ ๊ฒ€ํ•œ๋‹ค. ๋…ธ๋“œ๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ๊ตฌ๊ฐ„์ด [left, right]๋ผ๋ฉด ๋‹ค์Œ์˜ 4๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฐ ๊ฒฝ์šฐ๋งˆ๋‹ค ์ฒ˜๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (1) [start, end]์™€ [left, right]๊ฐ€ ๊ฒน์น˜์ง€ ์•Š์Œ -> ํ•  ์ผ์ด ์—†๋‹ค (2) [start, end]๊ฐ€ [left, right]๋ฅผ ํฌํ•จ -> ๋ฏธ๋ฆฌ ๊ตฌํ•ด๋‘” [left, right]์˜ ํ•ฉ์„ ์‚ฌ์šฉํ•œ๋‹ค. (3) [start, end]๋ฅผ [left, right]๊ฐ€ ํฌํ•จ -> ๋‘ ์ž์‹ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ ์ •ํ™•ํžˆ ์–ด๋Š ๋…ธ๋“œ๊ฐ€ [left, right]๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ์ฐพ๋Š”๋‹ค. (4) [start, end]๊ฐ€ [left, right..
[๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•] 5. ์š•์‹ฌ์Ÿ์ด ๊ธฐ๋ฒ• 1. ์š•์‹ฌ์Ÿ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ : Greedy algorithm ๋งค ๋‹จ๊ณ„๋งˆ๋‹ค ๊ฐ ๋‹จ๊ณ„์—์„œ ์ตœ์„ ์˜ ์„ ํƒ์„ ํ•œ๋‹ค. (ํ˜„์žฌ ์ˆœ๊ฐ„๋งˆ๋‹ค) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‚˜, ๊ตฌํ•œ ๋‹ต์ด ์ •๋‹ต์ธ์ง€ ๊ฒ€์ฆ์ด ํ•„์š”ํ•˜๋‹ค. ๊ธฐ๋ณธ ํ˜•ํƒœ 2. ํšŒ์˜์‹ค ๋ฐฐ์ • ๋ฌธ์ œ ํ•œ ๊ฐœ์˜ ํšŒ์˜์‹ค์ด ์žˆ๋Š”๋ฐ, n๊ฐœ์˜ ํšŒ์˜ ์ผ์ •์ด ์žˆ๊ณ  ์ด ํšŒ์˜์‹ค์„ ์ด์šฉํ•˜์—ฌ ํšŒ์˜ํ•œ๋‹ค. ๊ฐ ํšŒ์˜ i์— ๋Œ€ํ•ด์„œ ์‹œ์ž‘ ์‹œ๊ฐ„ Si์™€ ๋ ์‹œ๊ฐ„ Fi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํšŒ์˜๊ฐ€ ๊ฒน์น˜์ง€ ์•Š๊ฒŒ ํ•˜๋ฉด์„œ ๊ฐ€์žฅ ๋งŽ์€ ํšŸ์ˆ˜์˜ ํšŒ์˜๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ผ์ •์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. โ– ์ž…๋ ฅ โžข ์ฒซ ์ค„์—๋Š” ํšŒ์˜์˜ ์ˆ˜ n โžข ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ n+1์ค„๊นŒ์ง€ ๋‘ ์ •์ˆ˜ Si์™€ Fi โ– ์ถœ๋ ฅ โžข ์žก์€ ํšŒ์˜๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅ(๋‘˜ ์ด์ƒ์˜ ๋‹ต์ด ์žˆ์„ ์ˆ˜ ์žˆ์Œ) ๊ฐ€๋Šฅํ•œ ์ ‘๊ทผ ๋ฐฉ๋ฒ• - ํšŒ์˜์‹œ๊ฐ„์ด ์งง์€ ๊ฒƒ ๋ถ€ํ„ฐ ๋„ฃ๋Š”๋‹ค -> ๋ฐ˜๋ก€ ์กด์žฌ - ๋‹ค..
[๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•] 4. ์ž๋ฃŒ๊ตฌ์กฐ2 1. ํŠธ๋ฆฌ ์ง€๋ฆ„ ํŠธ๋ฆฌ์˜ ๋‘ ์ •์  u, v์˜ ๊ฑฐ๋ฆฌ์˜ ์ตœ๋Œ“๊ฐ’ (1) O(n^3) : brute force (2) O(n^2) : u๋ฅผ ๊ณ ์ •ํ•˜๊ณ  bfsํ•œ๋‹ค. (3) O(n) ํŠธ๋ฆฌ์—์„œ ์ž„์˜์˜ ์ •์  x๋ฅผ ์„ ํƒํ•˜๊ณ  x์—์„œ ๊ฐ€์žฅ ๋จผ ์ •์  y๋ฅผ ์ฐพ๋Š”๋‹ค.(bfs) ์ดํ›„ y์—์„œ ๊ฐ€์žฅ ๋จผ ์ •์  z๋ฅผ ์ฐพ๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋ฉด y์™€ z๊ฐ€ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ์žˆ๋Š” ์ •์  ์Œ์ด๋‹ค. ์ฆ๋ช… ์›ํ•˜๋Š” ๋‹ต์ด ๋‘ ์ •์  u์™€ v์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด 3๊ฐ€์ง€ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š”๋ฐ, x์—์„œ ๊ฐ€์žฅ ๋จผ ์ ์ด y๋ผ๊ณ  ํ•˜๋ฉด (1) x = u / x = v (2) y = u / y = v (3) x,y,u,v ๋ชจ๋‘ ๋‹ค๋ฅธ ๊ฒฝ์šฐ (1)๊ณผ (2)๋Š” ์œ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋‹ต์ด ๋งž๋‹ค๋Š” ๊ฒƒ์ด ์ž๋ช…ํ•˜๋‹ค. (3)์˜ ๊ฒฝ์šฐ๋Š” u~y ๊ฒฝ๋กœ์™€ u~v ๊ฒฝ๋กœ๊ฐ€ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ ์ด์™ธ์—๋Š” ์—†์–ด์•ผ ํ•œ๋‹ค. x,y,u,..

728x90