Maximum Non-Overlapping Increasing Subsequences

Given a list of integers, we can find an increasing subsequence from that list, i.e., select specific elements from the list where the selected numbers are in increasing order. There is a classic problem that asks to find the longest increasing subsequence, i.e., increasing subsequence with the maximum number of elements. In our problem, we are going to do a modified version of that problem. Instead of finding one increasing subsequence, we will find multiple increasing subsequences with the following two constraints:

  1. The length of each subsequence should be at least of a given size k.

  2. If a subsequence starts at index i and ends at index j, no other subsequence can start or end in the range i to j (inclusive), i.e., no other subsequence can have any elements in between index i and j (inclusive), i.e., the subsequences cannot overlap.

The objective is to find subsequences (satisfying the above two conditions) resulting in the maximum number of elements being selected, i.e., the total number of elements in all these subsequences combined is the maximum.

For example, consider the list 2 1 9 3 4 4 5 6. If k = 2, we can get three non-overlapping increasing subsequences with maximum of 7 elements: [2, 9], [ 3, 4], [4, 5, 6].

If k = 3, we can get two non-overlapping increasing subsequences with maximum of 6 elements: [2, 3, 4], [ 4, 5, 6].

The Problem:

Given a list of n integers, determine the maximum number of integers you can include in the non-overlapping increasing subsequences for all k where 1 ≤ k ≤ n.

The Input:

The first input line contains an integer, t (1 ≤ t ≤ 50), indicating the number of test cases to process. Each test case starts with an integer, n (1 ≤ n ≤ 100), indicating the number of integers in the list. The second line of each test case contains the list of n integers (each in the range of -106 to 106, inclusive).

The Output:

For each test case, output n integers (on a single line) indicating, respectively, the maximum number of integers in the non-overlapping subsequences for all k where 1 ≤ k ≤ n.

样例输入
3
8
2 1 9 3 4 4 5 6
2
1 1
3
1 2 3
样例输出
8 7 6 5 5 0 0 0
2 0
3 3 3
#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define vi vector<int>
#define vc vector<char>
#define pii pair<int,int>
#define pll pair<long,long>
#define pil pair<int,long>
#define pli pair<long,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 105;
int a[N], T, n;
int len[N][N], f[N][N];
vi seq;

int main() {
    T = qr();
    while (T--) {
        n = qr();
        repi(i, 1, n)a[i] = qr();
        repi(i, 1, n) {
            seq.clear();
            repi(j, i, n) {
                if (seq.empty() || a[j] > seq.back())seq.pb(a[j]);
                else {
                    auto t = lower_bound(seq.begin(), seq.end(), a[j]);
                    *t = a[j];
                }
                len[i][j] = seq.size();
            }
        }
        repi(k, 1, n)repi(i, 1, n) {
                int j = 1;
                f[k][i] = 0;
                while (len[j][i] >= k)
                    f[k][i] = max(f[k][i], f[k][j - 1] + len[j][i]), j++;
            }
        repi(i, 1, n)printf("%d%c", f[i][n], ce(i, n));
    }
    return 0;
}
发布了360 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45323960/article/details/105375724
今日推荐