BOJ : https://www.acmicpc.net/problem/1003

github : https://github.com/junho0956/Algorithm/blob/master/1003/1003/%EC%86%8C%EC%8A%A4.cpp

 

dp[n] 의 의미는 n 일때 0 과 1 의 호출 횟수를 담는 pair 배열입니다.

다음 재귀문과 같이 동적계획법을 이용하여 문제를 해결합니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
 
typedef pair<long longlong long> ll;
 
ll dp[41];
 
ll solve(int num) {
    if (num == 0return { 1,0 };
    if (num == 1return { 0,1 };
 
    if (num < 0return0,0 };
 
    ll& res = dp[num];
    if (res.first || res.second) return res;
 
    ll ans = solve(num - 1);
    ll ans2 = solve(num - 2);
    return res = { ans.first + ans2.first, ans.second + ans2.second };
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    int T; cin >> T;
    while (T--) {
        int n; cin >> n;
        ll ans = solve(n);
        cout << ans.first << " " << ans.second << "\n";
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 1914번 하노이 탑  (0) 2020.02.10
BOJ 11722번 가장 긴 감소하는 부분수열  (0) 2020.02.09
BOJ 2193번 이친수  (0) 2020.02.09
BOJ 2302번 극장 좌석  (0) 2020.02.09
BOJ 2579번 계단 오르기  (0) 2020.02.08

BOJ : https://www.acmicpc.net/problem/2193

 

2193번: 이친수

0과 1로만 이루어진 수를 이진수라 한다. 이러한 이진수 중 특별한 성질을 갖는 것들이 있는데, 이들을 이친수(pinary number)라 한다. 이친수는 다음의 성질을 만족한다. 이친수는 0으로 시작하지 않는다. 이친수에서는 1이 두 번 연속으로 나타나지 않는다. 즉, 11을 부분 문자열로 갖지 않는다. 예를 들면 1, 10, 100, 101, 1000, 1001 등이 이친수가 된다. 하지만 0010101이나 101101은 각각 1, 2번 규칙에 위배되

www.acmicpc.net

 

피보나치 수열로 해결할 수 있는 문제였습니다.

N이 40을 넘기때문에 long long 을 사용하시면 됩니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
 
long long dp[91= { 0,1,1,2, };
 
int main() {
    ios::sync_with_stdio(false);
 
    int N;
    cin >> N;
    for (int i = 4; i <= N; i++)
        dp[i] = dp[i - 2+ dp[i - 1];
 
    cout << dp[N];
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

'algorithm > BOJ' 카테고리의 다른 글

BOJ 11722번 가장 긴 감소하는 부분수열  (0) 2020.02.09
BOJ 1003번 피보나치 함수  (0) 2020.02.09
BOJ 2302번 극장 좌석  (0) 2020.02.09
BOJ 2579번 계단 오르기  (0) 2020.02.08
BOJ 9095번 1, 2, 3 더하기  (0) 2020.02.08

BOJ : https://www.acmicpc.net/problem/2302

 

2302번: 극장 좌석

주어진 조건을 만족하면서 사람들이 좌석에 앉을 수 있는 방법의 가짓수를 출력한다. 방법의 가짓수는 2,000,000,000을 넘지 않는다. (2,000,000,000 < 231-1)

www.acmicpc.net

github : https://github.com/junho0956/Algorithm/tree/master/2302/2302

 

재귀로 풀어보고 싶어서 dfs로 여러번 시도해봤는데

역시 실력이 안되는 것 같다..

그냥 간단하게 vip 석이 아닌 부분의 모든 경우를 곱해서 해결하였다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
 
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
 
    int n, m, sum = 1;
    int arr[41];
    int dp[41= { 112, };
    cin >> n >> m;
    if (m == 0) {
        for (int i = 3; i <= n; i++) dp[i] = dp[i - 1+ dp[i - 2];
        cout << dp[n];
    }
    else {
        int start = 1;
        while (m--) {
            int idx; cin >> idx;
            for (int i = 3; i <= idx - start + 1; i++) dp[i] = dp[i - 1+ dp[i - 2];
            sum *= dp[idx - start];
            start = idx + 1;
        }
        if (start < n) {
            for (int i = 3; i <= n - start + 1; i++) dp[i] = dp[i - 1+ dp[i - 2];
            sum *= dp[n - start + 1];
        }
        cout << sum;
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 1003번 피보나치 함수  (0) 2020.02.09
BOJ 2193번 이친수  (0) 2020.02.09
BOJ 2579번 계단 오르기  (0) 2020.02.08
BOJ 9095번 1, 2, 3 더하기  (0) 2020.02.08
BOJ 15486번 퇴사 2  (0) 2020.02.08

BOJ : https://www.acmicpc.net/problem/2579

github : https://github.com/junho0956/Algorithm/blob/master/2579/2579/%EC%86%8C%EC%8A%A4.cpp

 

사실 문제의 점화식을 세우는 과정은 어렵지 않은데

.. 재귀로 짜볼려다가 계속 틀려버렸다

포스팅한후에 다시 고민해봐야겠다 ..

 

점화식은 다음과 같다

현재 계단을 올 수 있는 경우는

1. 전 계단을 밟고 온 경우

2. 전 계단을 밟지 않은 경우

 

1. 전 계단을 밟고 온 경우 전전은 못 밟았기 때문에 전전전 까지의 최댓값을 알고 있으면 된다.

2. 전 계단을 밟지 않은 경우 전전까지는 밟고 온 것이기 때문에 전전 계단에 대한 최댓값을 알고 있으면 된다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstring>
using namespace std;
typedef long long ll;
 
int dp[301];
int arr[301];
int N;
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
 
    cin >> N;
    for (int i = 1; i <= N; i++cin >> arr[i];
 
    dp[1= arr[1];
    dp[2= arr[1+ arr[2];
    dp[3= max(arr[1+ arr[3], arr[2+ arr[3]);
    for (int i = 4; i <= N; i++) {
        dp[i] = max(dp[i - 3+ arr[i - 2+ arr[i], dp[i - 3+ arr[i - 1+ arr[i]);
    }
    
    cout << dp[N];
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 2193번 이친수  (0) 2020.02.09
BOJ 2302번 극장 좌석  (0) 2020.02.09
BOJ 9095번 1, 2, 3 더하기  (0) 2020.02.08
BOJ 15486번 퇴사 2  (0) 2020.02.08
BOJ 1010번 다리 놓기  (0) 2020.02.08

+ Recent posts