Orac and Models CodeForces - 1350B(最长上升子序列变形)

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.

Input
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≤109): the sizes of models.

It is guaranteed that the total sum of n is at most 100000.

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

Example
Input
4
4
5 3 4 6
7
1 4 2 3 6 4 9
5
5 4 3 2 1
1
9
Output
2
3
1
1
Note
In the first query, for example, Orac can buy models with indices 2 and 4, the arrangement will be beautiful because 4 is divisible by 2 and 6 is more than 3. 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 1, 3, and 6. 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一如既往垃圾的我果然倒在了这个题目上。
思路:根据题意,我们需要考虑的是i位,对后面那些位置是有影响的?答案是i+i,i+i+i,i+i+i+i…,这样的话,就转化成了求最长上升子序列,但是呢,不是n * n的,而是n*logn的。
代码如下:

#include
#define ll long long
using namespace std;

const int maxx=1e5+100;
int a[maxx];
int dp[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		cin>>n;
		for(int i=0;i<=n+1;i++) dp[i]=0;
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=n;i++) dp[i]=1;//初始化
		for(int i=1;i<=n;i++)
		{
			for(int j=i+i;j<=n;j+=i) if(a[j]>a[i]) dp[j]=max(dp[j],dp[i]+1);
		}
		int _max=0;
		for(int i=1;i<=n;i++) _max=max(_max,dp[i]);
		cout<<_max<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

你可能感兴趣的:(dp)