码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-10-05 18:12:54      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:

                                                                              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时计算就好了

技术分享
///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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!