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

本文介绍了解决Codeforces582B问题的方法,该问题是寻找给定序列重复多次后形成的数组中最长的非递减子序列。通过分析,提出了将序列分为两部分并分别计算最长非递减子序列长度的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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;
}

根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值