-
[Algo Rhythm๐บ๐] BOJ 9095 - 1, 2, 3 ๋ํ๊ธฐAlgo Rhythm๐บ๐/BOJ 2021. 6. 29. 00:06
๐ซ๋ฌธ์ ๋ถ์
ํ ์คํธ ์ผ์ด์ค์ ๊ฐ์๋ฅผ t, ๊ฐ ํ ์คํธ ์ผ์ด์ค๋ง๋ค ์ฃผ์ด์ง๋ ์ ์๋ฅผ n(1โคn<11)์ด๋ผ๊ณ ํ์. ๊ทธ๋ฆฌ๊ณ n์ 1, 2, 3์ ํฉ์ผ๋ก ๋ํ๋ด๋ ๋ฐฉ๋ฒ์ ์๋ฅผ f(n)์ด๋ผ๊ณ ํ์.
๊ฐ ํ ์คํธ์ผ์ด์ค๋ง๋ค f(n)์ ์ถ๋ ฅํด์ผ ํ๋ฉฐ, ํฉ์ ๋ํ๋ผ ๋๋ ์๋ฅผ 1๊ฐ ์ด์ ์ฌ์ฉํด์ผ ํ๋ค.
f(n)์ 1๋ถํฐ ์ฐจ๋ก๋๋ก ์๋์ ๊ฐ๋ค. ์ด๋ ํ๋์ ๋๊ทธ๋ผ๋ฏธ๋ก ํ์๋ ์๋ค์ ๊ด์ฐฐํ๋ฉด ๋ถ๋ช ํ ์ฐ์์ ์ธ ๊ด๊ณ๊ฐ ์๋ค๋ ๊ฒ์ ์ ์ ์๋ค. ๋ฐ๋ผ์ ์ด ๋ฌธ์ ๋ ๊ทธ '์ฐ์์ ์ธ ๊ด๊ณ'๋ฅผ ์ด์ฉํ์ฌ ํด๊ฒฐํ ์ ์๋ค.
๐ฅ๋ฌธ์ ํ์ด
์์ ์ธ๊ธํ ์ฐ์์ ์ธ ๊ด๊ณ๋ ๋ค์๊ณผ ๊ฐ๋ค.
f(i)=โ3j=1g(i,j)(1โคi<11,iโZ)g(i,j)={0,iโj<01,iโj==0f(iโj),iโj>0
f(i)๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด์ ๋ง๋ ๋ค ์ด ์์ ์ด์ฉํ์ฌ bottom-up ๋ฐฉ์์ผ๋ก ๊ณ์ฐํ ๋ค ์ ์ฅํ๋ฉด ๋๋ค.
โจ๋ณต์ก๋ ๋ถ์
ํ ์คํธ ์ผ์ด์ค์ ๊ฐ์๋ฅผ t, ๊ฐ ํ ์คํธ ์ผ์ด์ค๋ง๋ค ์ฃผ์ด์ง๋ ์ ์๋ฅผ n(1โคnโค11)์ด๋ผ๊ณ ํ ๋, ์๊ฐ ๋ณต์ก๋์ ๊ณต๊ฐ ๋ณต์ก๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
โฐ์๊ฐ ๋ณต์ก๋ : O(t+n)
f(n)์ ์ ์ฅํ๋ ๋ฐฐ์ด์ ๋ง๋๋๋ฐ O(n)์ด ์์๋จ.
ํ ์คํธ ์ผ์ด์ค์ ๊ฐ์๋งํผ f(n)์ ์ถ๋ ฅํด์ผ ํ๊ธฐ ๋๋ฌธ์ O(t)๊ฐ ์์๋จ.
๋ฐ๋ผ์, ์ ์ฒด์ ์ธ ์๊ฐ ๋ณต์ก๋๋ O(t+n)์.
๐ ๊ณต๊ฐ ๋ณต์ก๋ : O(n)
f(n)์ ์ ์ฅํ๋ ๋ฐฐ์ด์ ๋ง๋๋๋ฐ O(n)์ด ์์๋จ.
๐์ฝ๋
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <cstdio> int f[11]; int main() { int t, n, tmp; scanf("%d", &t); for(int i = 1; i < 11; i++) { tmp = 0; for(int j = 1; j <= 3; j++) { if(i - j < 0) break; if(i - j) tmp += f[i - j]; else tmp += 1; } f[i] = tmp; } for(int i = 0; i < t; i++) { scanf("%d", &n); printf("%d\n", f[n]); } return 0; } ๐๋ฌธ์ ์ถ์ฒ
https://www.acmicpc.net/problem/9095
9095๋ฒ: 1, 2, 3 ๋ํ๊ธฐ
๊ฐ ํ ์คํธ ์ผ์ด์ค๋ง๋ค, n์ 1, 2, 3์ ํฉ์ผ๋ก ๋ํ๋ด๋ ๋ฐฉ๋ฒ์ ์๋ฅผ ์ถ๋ ฅํ๋ค.
www.acmicpc.net
'Algo Rhythm๐บ๐ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algo Rhythm๐บ๐] BOJ 1759 - ์ํธ ๋ง๋ค๊ธฐ (0) 2021.06.30 [Algo Rhythm๐บ๐] BOJ 18290 - NM๊ณผ K(1) (0) 2021.06.30 [Algo Rhythm๐บ๐] BOJ 1748 - ์ ์ด์ด์ฐ๊ธฐ 1 (0) 2021.06.28 [Algo Rhythm๐บ๐] BOJ 17425 - ์ฝ์์ ํฉ (0) 2021.06.24 [Algo Rhythm๐บ๐] BOJ 17427 - ์ฝ์์ ํฉ 2 (0) 2021.06.24