Codeforces Round #323 (Div. 2) D LIS 最長上升子序列

CrossDolphin發表於2016-07-08



連結:戳這裡


D. Once Again...
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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: n, T (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.


題意:

給你長度為n的序列,然後再複製加長T倍,問最長上升子序列


思路:

給出的長度為n的子序列求LIS肯定很好算,那麼我們分析一下,什麼情況下會答案很大呢

如果給你的n個數是唯一的,(n=100)那麼我算最長的也就是n*n了,為什麼?

你能拿到的最長的數也就到第n*n,因為假設你每個n塊裡面取一個出來(至少有一個滿足答案)

也就是n*n個,所以我們求到n*n也才10000,那麼多出來的(T-n)怎麼辦

直接拿出現次數最多的那個數就行了


程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int s[1000100];
int LIS(int *a,int n){
    int len=0;
    s[++len]=a[1];
    for(int i=2;i<=n;i++){
        if(a[i]>=s[len]) s[++len]=a[i];
        else {
            int x=upper_bound(s+1,s+len+1,a[i])-s;
            /// s中>a[i]的第一個元素。
            s[x]=a[i];
        }
    }
    return len;
}
int n,T;
int a[1000100],b[1000100],vis[1000100];
int main(){
    scanf("%d%d",&n,&T);
    int num=0;
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        vis[a[i]]++;
        num=max(vis[a[i]],num);
    }
    int cnt=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            b[++cnt]=a[j];
        }
    }
    ll ans=0;
    if(T<=n){
        ans=LIS(b,n*T);
    } else {
        ans=LIS(b,n*n)+(ll)num*(T-n);
    }
    cout<<ans<<endl;
    return 0;
}


相關文章