您的位置:首页 > 其它

最长递增子序列延伸

2017-08-18 19:25 155 查看

Once again

You are given an array of positive integers a1, a2, …, an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of the longest non-decreasing sequence of the given array.

Input

The first line contains two space-separated integers: n, T (1 ≤ n ≤ 100, 1 ≤ T ≤ 107). The second line contains n space-separated integers a1, a2, …, an (1 ≤ ai ≤ 300).

Output

Print a single number — the length of a sought sequence.

Example

Input

4 3

3 1 4 2

Output

5

Note

The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.

题意:给你含 n*t 个元素的数组,数组中的元素有一个特点,按数组的1–n 循环下去,循环 t 次,(a[i]=a[i+n])。求该数组中的最长递增子序列。

分析: t 的范围较大,不适宜直接dp。对于每一节,在DP时只需要对本节自己之前以及上一节所有的数字比较(每个循环节至少会增加一个数字)。若T小于100,那么在T个循环内必然会出现结果,直接DP即可。在T较大时,只有在每个循环节增加数字大于1时才会出现选择的情况。在T等于100时必然会成为稳定状态,之后为将数字最大化,把循环中出现次数最多的数字插入在剩余的循环节内。

#include<iostream>
#include<cstdio>

using namespace std;
int a[105],b[305];
long long int dp[105][105];
int main()
{
int n,t,maxn=0;
scanf("%d%d",&n,&t);
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
b[a[i]]++;
}
for(int i=0; i<305; i++)
maxn=max(maxn,b[i]);
int m=min(n*n,n*t);

for(int i=0; i<m/n; i++)
{
for(int j=0; j<n; j++)
{
for(int k=0; k<n; k++)
{
if(i==0)
{
dp[i][j]=1;
break;
}
if(a[j]>=a[k])
dp[i][j]=max(dp[i][j],dp[i-1][k]+1);
}
for(int p=0; p<j; p++)
if(a[j]>=a[p])
dp[i][j]=max(dp[i][j],dp[i][p]+1);
}
}
static long long ans=0;
for(int i=0; i<n; i++)
ans=max(ans,dp[m/n-1][i]);
ans=ans+maxn*(t-m/n);
printf("%lld\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp