結果

問題 No.44 DPなすごろく
ユーザー tokizotokizo
提出日時 2018-06-13 11:00:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,160 bytes
コンパイル時間 1,490 ms
コンパイル使用メモリ 165,696 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 03:52:07
合計ジャッジ時間 3,015 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)

/*
考えたこと

最初、漸化式を dp[i] = min(dp[i - 1] + 1, dp[i - 2] + 1);
としていた。
この式だとiまでにいく最小の手数となってしまう

dp[1] = 1  <-  1にいく手数は1のみの1回
dp[2] = 2  <-  1 + 1, 2
dp[i(>= 3)] = (i - 2)までの手数 + (i - 1)までの手数…

?

なんでだ?

とりあえず少し書いてみる
括弧内は個数

i
1  1(1)
2  1 + 1, 2(2)
3  1 + 1 + 1, 1 + 2, 2 + 1(3)
4  1 + 1 + 1 + 1, 1 + 1 + 2, 1 + 2 + 1, 2 + 1 + 1, 2 + 2(5)

i >= 2のときi - 1の要素に+1を不随するかそのものに+1するかの数の総数

i
1  1
2  1 + 1, 2(1 + 1)
3  1 + 1 + 1, 1 + 2, 2 + 1(2 + 1の + 1の場所の総数)
4  1 + 1 + 1 + 1, 1 + 1 + 2, 1 + 2 + 1, 2 + 1 + 1(1 + 2 + 2の2の場所の総数), 2 + 2


?
*/


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vector<long long> dp(n + 1, 1e9);
    dp[1] = 1;
    dp[2] = 2;

    for(int i = 3; i <= n; i++){
        dp[i] = dp[i - 1] + dp[i - 2];
    }
    cout << dp[n] << endl;

    return 0;
}
0