Codeforces 582B Once Again... 【LIS变形】

题目链接:Codeforces 582B Once Again…

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

题意:给定n个元素的序列,现在将它重复T次。问你可以得到的LIS。

思路:当n个元素各不相同时,在有n+1段的情况下会导致有一段的贡献必定为1。这样的话就好办了,我们取前n段为一个序列A[],后n段为一个序列B[]。
dp1[i]记录以A[i]结尾的LIS,dp2[i]记录以B[i]开头的LIS。我们暴力起点i、终点j,中间序列取合法的A[k](A[i] <= A[k] <= A[j])且A[k]数量最多。扫描一遍就好了。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <queue>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e4 + 10;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int g[MAXN], a[MAXN];
int dp1[MAXN], dp2[MAXN];
int cnt[400];
int main()
{
    int n, T;
    while(scanf("%d%d", &n, &T) != EOF) {
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]); g[i] = INF;
        }
        int ans = 0;
        if(T <= 2*n) {
            for(int i = n+1; i <= T * n; i++) {
                a[i] = a[i % n == 0 ? n : i % n];
                g[i] = INF;
            }
            for(int i = 1; i <= T * n; i++) {
                int k = upper_bound(g+1, g+T*n+1, a[i]) - g;
                dp1[i] = k;
                g[k] = min(g[k], a[i]);
                ans = max(ans, dp1[i]);
            }
            printf("%d\n", ans);
            continue;
        }
        for(int i = n+1; i <= n * n; i++) {
            a[i] = a[i % n == 0 ? n : i % n];
            g[i] = INF;
        }
        for(int i = 1; i <= n * n; i++) {
            int k = upper_bound(g+1, g+n*n+1, a[i]) - g;
            dp1[i] = k;
            g[k] = min(g[k], a[i]);
        }
        CLR(g, INF);
        for(int i = n * n; i >= 1; i--) {
            int k = upper_bound(g+1, g+n*n+1, -a[i]) - g;
            dp2[i] = k;
            g[k] = min(g[k], -a[i]);
        }
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(a[i] <= a[j]) {
                    int Max = 0; CLR(cnt, 0);
                    for(int k = 1; k <= n; k++) {
                        if(a[k] >= a[i] && a[k] <= a[j]) {
                            cnt[a[k]]++;
                        }
                        Max = max(Max, cnt[a[k]]);
                    }
                    ans = max(ans, dp1[(n - 1) * n + i] + dp2[j] + Max * (T - 2*n));
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值