标签:
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.
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).
Print a single number — the length of a sought sequence.
4 3
3 1 4 2
5
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时计算就好了
///1085422276 #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=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘)f=-1; ch=getchar(); } while(ch>=‘0‘&&ch<=‘9‘) { x=x*10+ch-‘0‘; ch=getchar(); } return x*f; } //**************************************** #define maxn 100+5 int a[maxn]; int dp[30005]; int hashs[400]; int main() { int n=read(); int T=read(); FOR(i,1,n) { scanf("%d",&a[i]); } FOR(i,0,n*102)dp[i]=1; if(T<=100) { FOR(i,1,T) { FOR(j,1,n) { for(int k=1;k<j+n*(i-1);k++) { int tmp=k%n; if(tmp==0)tmp=n; if(a[j]>=a[tmp]) dp[j+n*(i-1)]=max(dp[j+n*(i-1)],dp[k]+1); } } } int mm=-1; for(int i=1;i<=n*T;i++)mm=max(dp[i],mm); cout<<mm<<endl; } else { FOR(i,1,100) { FOR(j,1,n) { for(int k=1;k<j+n*(i-1);k++) { int tmp=k%n; if(tmp==0)tmp=n; if(a[j]>=a[tmp]) dp[j+n*(i-1)]=max(dp[j+n*(i-1)],dp[k]+1); } } } int mm=-1,flag,ans=0; for(int i=1;i<=n*100;i++)mm=max(dp[i],mm); for(int i=1;i<=n;i++) { hashs[a[i]]++; if(hashs[a[i]]>ans)ans=hashs[a[i]]; } cout<<mm+(T-100)*ans<<endl; } return 0; }
Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列
标签:
原文地址:http://www.cnblogs.com/zxhl/p/4855985.html