B. Orac and Models

题目连接:https://codeforces.com/contest/1350/problem/B

B. Orac and Models

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn models in the shop numbered from 11 to nn, with sizes s1,s2,…,sns1,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 ijij and ij+1ij+1 (note that ij

For example, for 66 models with sizes {3,6,7,7,7,7}{3,6,7,7,7,7}, he can buy models with indices 11, 22, and 66, 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.

Input

The first line contains one integer t (1≤t≤100)t (1≤t≤100): the number of queries.

Each query contains two lines. The first line contains one integer n (1≤n≤100000)n (1≤n≤100000): the number of models in the shop, and the second line contains nn integers s1,…,sn (1≤si≤109)s1,…,sn (1≤si≤109): the sizes of models.

It is guaranteed that the total sum of nn is at most 100000100000.

Output

Print tt lines, the ii-th of them should contain the maximum number of models that Orac can buy for the ii-th query.

Example

input

Copy

4
4
5 3 4 6
7
1 4 2 3 6 4 9
5
5 4 3 2 1
1
9

output

Copy

2
3
1
1

Note

In the first query, for example, Orac can buy models with indices 22 and 44, the arrangement will be beautiful because 44 is divisible by 22 and 66 is more than 33. By enumerating, we can easily find that there are no beautiful arrangements with more than two models.

In the second query, Orac can buy models with indices 11, 33, and 66. By enumerating, we can easily find that there are no beautiful arrangements with more than three models.

In the third query, there are no beautiful arrangements with more than one model.

 

写的时候超时了

思路:这是个dp题 建立dp数组f[],f[i]表示以a[i]结尾的满足条件的最长上升子序列的长度,条件是:i可以被前面的下标除尽i-j .....1

状态转移方程:f[j]=max(f[j],f[i]+1),这样写的话避免了不符合的下标的问题

code:

#include
#include
#include

using namespace std;

const int N=1e5+19;
int a[N];
int f[N];
int ans=1;
int main()
{
	int T;
	cin>>T;
	while(T--){
		int n;
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		for(int i=1;i<=n;i++){
			f[i]=1;
		}
//		for(int i=2;i<=n;i++){
//			for(int j=1;ja[j])
//					f[i]=max(f[i],f[j]+1);
//			}
//		}
		for(int i=1;i<=n;i++){
			for(int j=i*2;j<=n;j+=i){
				if(a[j]>a[i]) f[j]=max(f[j],f[i]+1);
			}
		}
		ans=1;
		for(int i=1;i<=n;i++){
			ans=max(ans,f[i]);	
		}
		cout<

 

你可能感兴趣的:(dp)