這題在最長上升子序列的基礎上加上了一個限制條件,就是這個上升子序列的原數組下標當前能被前一個整除。
思想還是 dp[i] 表示以 i 結尾的滿足條件的最長上升子序列,普通的lst,dp[i]需要從1~ i - 1來得到更新,也可以理解成用1 ~i - 1的dp值來更新dp[i],那麼對於這裏,只需要枚舉滿足條件的下標去更新dp[i],即 i 這個下標的因子下標,用埃氏篩的思想。
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int man = 2e5+10;
#define IOS ios::sync_with_stdio(0)
typedef long long ll;
const ll mod = 1e9+7;
int a[man],cnt[man];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
int t;
cin >> t;
while(t--){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
cnt[i] = 1;
}
int maxx = 1;
for(int i = 1;i <= n;i++){
for(int j = 2 * i;j <= n;j += i){
if(a[j]>a[i])cnt[j] = max(cnt[j],cnt[i]+1);
}
maxx = max(maxx,cnt[i]);
}
cout << maxx << endl;
}
return 0;
}