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

    ///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
    ");
    #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;
    }
    代码
  • 相关阅读:
    php lesson1
    微信红包算法java版本
    spring注解
    Java时间戳
    集成环信 服务器端遇见的坑:关于Request body is invalid.解决
    环信java后台发送消息时提示msg 应该为JSONObject 的坑
    jboss设置允许外网访问
    redis实现单点登录
    qq在线客服
    图片纯css控制宽高比
  • 原文地址:https://www.cnblogs.com/zxhl/p/4855985.html
Copyright © 2020-2023  润新知