Codeforces Round #323 (Div. 2) D. Once Again...

D. Once Again...
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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: nT (1 ≤ n ≤ 1001 ≤ 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.

Examples
input
Copy
4 3
3 1 4 2
output
5
Note

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


求n*t这么长的最长不递减子序列

数据太大  直接跑肯定GG  但是 序列如果是n*n已经包含了所有的可能,所以只需要求出来n*n的最长+(t-n)*一个n中出现最多的即可。

可以想象n*n最长中加塞了很多一样的--->(t-n)*最多的

long long a[10005]; 
int main ()
{
   	long long n,t;
	scanf("%lld%lld",&n,&t);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	long long ans=0;
	for(int i=1;i<=n;i++)
	{
		long long sum=1;
		for(int j=i+1;j<=n;j++)
		{
			if(a[i]==a[j])
				sum++;
		}
		ans=max(ans,sum);
	}
	long long len[10005];
	memset(len,0,sizeof(len));
	int l;
	if(t>=n)
		l=n*n;
	else
		l=t*n;
	for(int i=n+1;i<=l;i++)
	{
		a[i]=a[i-n];
	}
	for(int i=1;i<=l;i++)
	{
		len[i]=1;
		for(int j=1;j<i;j++)
		{
			if(a[i]>=a[j])
				len[i]=max(len[i],len[j]+1); 
		}
	}
	long long ans1=0;
	if(t<n)
	{
		for(int i=1;i<=l;i++)
		{
		//	printf("%d %lld\n",i,ans1);
			ans1=max(ans1,len[i]);
		}
	} 
	else
	{
		ans1=(t-n)*ans;
		long long temp=0;
		for(int i=1;i<=n*n;i++)
			temp=max(temp,len[i]);
		ans1=ans1+temp;
	}
	printf("%lld\n",ans1);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/passer__/article/details/79713959