重复T次的LIS的dp Codeforces Round #323 (Div. 2) D

http://codeforces.com/contest/583/problem/D

原题: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.

题目大意:有长度为n的数组a(n <= 100),其中a[i] <= 300,这个a数组可以重复T次,问他的最长上升子序列是多少?

思路:我们可以发现,这个数组如果要全部都算上的,那么在t<=n的情况下,他的最长上升子序列一定会遍历一次a数组。所以我们就只需要把原来的数组扩大n倍,然后求他的LIS。

这样以后我们发现,后面的重复的次数一定是原来数组里面出现次数(假定重复次数k为最多)最多的数值,所以ans = Lis的长度 + k * T - min(n, T);

复杂度 O(n*n*logn)

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha
")
const int maxn = 100 + 5;
int a[maxn * maxn];
int n, T;
vector<int> ve;

int solve(int t){
    int len = t * n;
    for (int i = 1; i < t; i++){
        for (int j = 1; j <= n; j++){
            a[n * i + j] = a[j];
        }
    }
    for (int i = 1; i <= n * t; i++){
        int pos = upper_bound(ve.begin(), ve.end(), a[i]) - ve.begin();
        if (pos == ve.size()) ve.push_back(a[i]);
        else ve[pos] = a[i];
    }
    return ve.size();
}

int cnt[maxn * maxn];
int main(){
    cin >> n >> T;
    int maxval = 0, k = 0;
    for (int i = 1; i <= n; i++){
        scanf("%d", a + i);
        cnt[a[i]]++;
        k = max(cnt[a[i]], k);
    }
    int ans = solve(min(n, T));
    //printf("ans = %d k = %d T - min(n, T) = %d
", ans, k, T - min(n, T));
    printf("%d
", ans + k * (T - min(n, T)));
    return 0;
}
原文地址:https://www.cnblogs.com/heimao5027/p/6166054.html