CodeForces - 582B Once Again... (LIS变型)好题

CodeForces - 582B
Once Again...
Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

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.

Sample Input

Input
4 3 3 1 4 2 
Output
5 

Hint

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.

Source

Codeforces Round #323 (Div. 1)
//题意:输入一个n,T,接下来输入n个数
表示将这含有n个数的序列循环T次后,问这个新的序列最长的不下降的序列为多长。
//思路:
因为n很小所以可以先求出循环n次的序列的最长不下降序列,然后再拓展到循环T次的最长长度。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 10010
using namespace std;
int dp1[N]={0},dp2[N]={0},dp3[310]={0};
int a[N];
int main()
{
	int n,m,i,j,p;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(dp3,0,sizeof(dp3));
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			dp3[a[i]]++;
		}
		for(i=1;i<=n*n;i++)
			a[i]=a[(i-1)%n+1];
		int l=min(n,m);
		l*=n;
		for(i=1;i<=l;i++)
		{
			int tmp=0;
			for(j=0;j<i;j++)
				if(a[j]<=a[i])
					tmp=max(tmp,dp1[j]);
			dp1[i]=tmp+1;
		}
		for(i=l;i>=1;i--)
		{
			int tmp=0;
			for(j=l+1;j>i;j--)
				if(a[j]>=a[i])
					tmp=max(tmp,dp2[j]);
			dp2[i]=tmp+1;
		}
		int ans=0;
		if(m<=n)
		{
			for(i=1;i<=l;i++)
				ans=max(ans,dp1[i]+dp2[i]-1);
		}
		else
		{
			for(i=1;i<=l;i++)
				ans=max(ans,dp1[i]+dp2[i]-1+dp3[a[i]]*(m-n));
		}
		printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(CodeForces - 582B Once Again... (LIS变型)好题)