結果

問題 No.979 Longest Divisor Sequence
ユーザー noshi91
提出日時 2019-10-07 21:46:36
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 234 ms / 2,000 ms
コード長 901 bytes
コンパイル時間 709 ms
コンパイル使用メモリ 76,252 KB
実行使用メモリ 18,480 KB
最終ジャッジ日時 2024-10-14 16:58:47
合計ジャッジ時間 2,022 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  static constexpr int ALim = 300001;
  int n;
  std::cin >> n;
  std::vector<int> a(n);
  for (auto& e : a) {
    std::cin >> e;
  }
  std::vector<std::vector<int>> rev(ALim);
  std::vector<int> dp(ALim, -1);
  for (int i = 0; i != n; ++i) {
    rev[a[i]].push_back(i);
    dp[a[i]] = i;
  }
  for (int len = 1;; ++len) {
    for (int i = 1; i != ALim; ++i) {
      int max = -1;
      for (int j = i + i; j < ALim; j += i) {
        max = std::max(max, dp[j]);
      }
      int j = 0;
      while (j != static_cast<int>(rev[i].size()) && rev[i][j] < max) {
        ++j;
      }
      if (j == 0) {
        dp[i] = -1;
      }
      else {
        dp[i] = rev[i][j-1];
      }
    }
    if (std::all_of(dp.cbegin(), dp.cend(), [](int x) { return x == -1; })) {
      std::cout << len << std::endl;
      return 0;
    }
  }
}
0