• Codeforces Round #323 (Div. 2) D. Once Again... 乱搞+LIS


    D. Once Again...
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    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.

     题目链接:http://codeforces.com/contest/583/problem/D

    题意:给你一个序列n,有T个连续这样的序列,求最长不下降子序列的长度;

    思路:发现T很大,n很小对吧,

       显然中间肯定是有一段是连续的相同的数对吧;

       开始想前面取一个LIS,最后取一个LIS,中间相同,数量最多的那个数;

         这个做法错误,可能后面的前一个还能多取;

       如果T<=n比较小,我们只需要暴力跑LIS即可;

       现在讨论T>n的情况;

       然后发现只有n个数,也就是说最后的LIS最多只有n段不同的数;

       也就是说我们只需要拿出n段序列出来,跑LIS即可;

       因为最多n段,然后取完n段,多余的可以从中间插入;

       

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define mk make_pair
    #define eps 1e-7
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=1e5+10,M=1e6+10,inf=2147483647;
    const ll INF=1e18+10,mod=1000000007;
    
    ///   数组大小
    int arr[N],ans[N],len;
    int binary_search(int i)
    {
        int left,right,mid;
        left=0,right=len;
        while(left<right)
        {
            mid = left+(right-left)/2;
            if(ans[mid]>arr[i]) right=mid;
            else left=mid+1;
        }
        return left;
    }
    int LIS(int p)
    {
        ans[1] = arr[1];
        len=1;
        for(int i=2; i<=p; ++i)
        {
            if(arr[i]>=ans[len])
                ans[++len]=arr[i];
            else
            {
                int pos=binary_search(i);
                ans[pos] = arr[i];
            }
        }
        return len;
    }
    int flag[N];
    int main()
    {
        int n,T,maxx=0;
        scanf("%d%d",&n,&T);
        for(int i=1;i<=n;i++)
            scanf("%d",&arr[i]),flag[arr[i]]++,maxx=max(maxx,flag[arr[i]]);
        for(int i=1;i<n;i++)
            for(int j=1;j<=n;j++)
                arr[i*n+j]=arr[j];
        if(n>=T)printf("%d
    ",LIS(n*T));
        else printf("%d
    ",LIS(n*n)+maxx*(T-n));
        return 0;
    }
  • 相关阅读:
    java:数组操作工具类 java.util.Arrays包 主要方法详解
    java:接口特性 接口与抽象类/普通类的区别
    mybatis教程:入门>>精通>>实战
    Java :内部类基础详解
    Java swing: 实现ActionListener监听器的三种途径
    Java:final、static关键字 详解+两者结合使用
    Java:双括号初始化 /匿名内部类初始化法
    Java:静态内部类的使用目的、使用限制、与非静态内部类的对比
    域名解析>>"记录类型" 说明
    Java:接口继承接口 (多继承机制)
  • 原文地址:https://www.cnblogs.com/jhz033/p/6726984.html
Copyright © 2020-2023  润新知