题目链接
There are n models in the shop numbered from 1 to n, with sizes s1,s2,…,sn.
Orac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes).
Orac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indices ij and ij+1 (note that ij
For example, for 6 models with sizes {3,6,7,7,7,7}, he can buy models with indices 1, 2, and 6, and the obtained arrangement will be beautiful. Also, note that the arrangement with exactly one model is also considered beautiful.
Orac wants to know the maximum number of models that he can buy, and he may ask you these queries many times.
The first line contains one integer t (1≤t≤100): the number of queries.
Each query contains two lines. The first line contains one integer n (1≤n≤100000): the number of models in the shop, and the second line contains n integers s1,…,sn (1≤si≤1e9): the sizes of models.
It is guaranteed that the total sum of n is at most 100000.
Print t lines, the i-th of them should contain the maximum number of models that Orac can buy for the i-th query.
4
4
5 3 4 6
7
1 4 2 3 6 4 9
5
5 4 3 2 1
1
9
2
3
1
1
对每一个位置,找它的因子数并更新答案即可,简单 DP,AC代码如下:
#include
using namespace std;
typedef long long ll;
main()
{
int t,n;
cin>>t;
while(t--){
cin>>n;
int s[n+1],ans[n+1],p=1;
fill(ans,ans+n+1,1);
for(int i=1;i<=n;i++) cin>>s[i];
for(int i=1;i<=n;i++){
for(int j=1;j<=sqrt(i);j++)
if(i%j==0){
if(s[i]>s[j]) ans[i]=max(ans[i],ans[j]+1);
if(s[i]>s[i/j]) ans[i]=max(ans[i],ans[i/j]+1);
}
p=max(p,ans[i]);
}
cout<<p<<endl;
}
}