Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列

时间:2023-03-09 22:42:44
Codeforces Round #323 (Div. 2)  D. Once Again... 暴力+最长非递减子序列
                                                                              D. 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: nT (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.

Sample test(s)
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的最长非递减子序列长度

题解:由于是T个n排列,中间段必定是相同的,也必定是n排列中数最多的,在T小于100是暴力dp,大雨100时计算就好了

///
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mod 1000000007
#define inf 100000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** #define maxn 100+5
int a[maxn];
int dp[];
int hashs[];
int main()
{ int n=read();
int T=read();
FOR(i,,n)
{
scanf("%d",&a[i]);
}
FOR(i,,n*)dp[i]=;
if(T<=)
{
FOR(i,,T)
{
FOR(j,,n)
{
for(int k=;k<j+n*(i-);k++)
{
int tmp=k%n;
if(tmp==)tmp=n;
if(a[j]>=a[tmp])
dp[j+n*(i-)]=max(dp[j+n*(i-)],dp[k]+);
}
}
}
int mm=-;
for(int i=;i<=n*T;i++)mm=max(dp[i],mm);
cout<<mm<<endl;
}
else { FOR(i,,)
{
FOR(j,,n)
{
for(int k=;k<j+n*(i-);k++)
{
int tmp=k%n;
if(tmp==)tmp=n;
if(a[j]>=a[tmp])
dp[j+n*(i-)]=max(dp[j+n*(i-)],dp[k]+);
}
}
}
int mm=-,flag,ans=;
for(int i=;i<=n*;i++)mm=max(dp[i],mm);
for(int i=;i<=n;i++)
{
hashs[a[i]]++;
if(hashs[a[i]]>ans)ans=hashs[a[i]];
}
cout<<mm+(T-)*ans<<endl;
}
return ;
}

代码