結果

問題 No.390 最長の数列
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-09 01:25:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 63 ms / 5,000 ms
コード長 864 bytes
コンパイル時間 450 ms
コンパイル使用メモリ 61,220 KB
実行使用メモリ 8,180 KB
最終ジャッジ日時 2024-04-10 08:55:21
合計ジャッジ時間 1,588 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,088 KB
testcase_01 AC 4 ms
7,280 KB
testcase_02 AC 4 ms
7,400 KB
testcase_03 AC 4 ms
7,624 KB
testcase_04 AC 4 ms
7,208 KB
testcase_05 AC 38 ms
8,084 KB
testcase_06 AC 63 ms
8,180 KB
testcase_07 AC 4 ms
7,336 KB
testcase_08 AC 3 ms
7,332 KB
testcase_09 AC 3 ms
7,336 KB
testcase_10 AC 42 ms
7,940 KB
testcase_11 AC 42 ms
8,088 KB
testcase_12 AC 42 ms
8,032 KB
testcase_13 AC 32 ms
7,912 KB
testcase_14 AC 61 ms
7,972 KB
testcase_15 AC 3 ms
7,344 KB
testcase_16 AC 2 ms
7,332 KB
testcase_17 AC 4 ms
7,628 KB
testcase_18 AC 5 ms
7,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int kMAX_N = 100010;
const int kMAX_X = 1000010;

int N;
int a[kMAX_N], val_index[kMAX_X];
int dp[kMAX_N];

void Solve() {
    sort(a, a + N);
    memset(val_index, -1, sizeof(val_index));

    for (int i = 0; i < N; i++) {
        if (val_index[a[i]] < 0) {
            val_index[a[i]] = i;
        }
    }
    int ans = 0;
    for (int i = 0; i < N; i++) {
        dp[i] = max(dp[i], 1);
        for (int k = 2; a[i] * k < kMAX_X; k++) {
            if (val_index[a[i] * k] >= 0) {
                dp[val_index[a[i] * k]] = max(dp[val_index[a[i] * k]], dp[i] + 1);
            }
        }
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}

int main() {
    cin >> N;

    for (int i = 0; i < N; i++) {
        cin >> a[i];
    }

    Solve();

    return 0;
}
0