Codeforces 582B Once Again...

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: nT (1 ≤ n ≤ 1001 ≤ 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.

Sample test(s)
input
4 3
3 1 4 2
output
5
Note

The array given in the sample looks like that: 3, 1, 4, 23, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence.


解题思路:这道题我想到的方法是首先计算出某一段值i->值j的最长不降子序列,然后最终的最长不降子序列是由每一段的最长不降子序列递推而来的,由于T很大,然后我们可以用矩阵快速幂优化我们的状态转移,先想到这把,国庆长假给我放糊涂了,程序写的很繁琐。。。。
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[110], b[110];
int dp[110][110];

struct Matrix {
    int n;
    int mat[100][110];
    void zero() {
        memset(mat, 0, sizeof(mat));
    }
    void unit() {
        memset(mat, 0, sizeof(mat));
    }
    friend Matrix operator * (const Matrix &a, const Matrix &b) {
        Matrix c;
        c.n = a.n;
        c.zero();
        for(int i = 1; i <= c.n; ++i) {
            for(int j = 1; j <= c.n; ++j) {
                if(i > j) continue;
                for(int k = i; k <= j; ++k) {
                    for(int l = k; l <= j; ++l) {
                        c.mat[i][j] = max(c.mat[i][j], a.mat[i][k] + b.mat[l][j]);
                    }
                }
            }
        }
        return c;
    }
    friend Matrix operator ^ (Matrix a, int x) {
        Matrix c;
        c.n = a.n;
        c = a;
        x--;
        while(x) {
            if(x & 1) c = c * a;
            a = a * a;
            x >>= 1;
        }
        return c;
    }
};

int bsearch(int x, int n) {
    int l = 1, r = n, mid;
    while(l <= r) {
        mid = (l + r) / 2;
        if(b[mid] == x) {
            return mid;
        } else if(b[mid] < x) {
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    return -1;
}

int main() {

    //freopen("aa.in", "r", stdin);
    int n, T;
    Matrix m;
    scanf("%d %d", &n, &T);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    int tn = unique(b + 1, b + n + 1) - b - 1;
    for(int i = 1; i <= n; ++i) {
        a[i] = bsearch(a[i], tn);
    }
    memset(dp, 0, sizeof(dp));
    for(int i = 1; i <= n; ++i) {
        dp[i][i] = 1;
        for(int j = 1; j < i; ++j) {
            if(a[j] <= a[i]) {
                dp[j][i] = 2;
            }
        }
    }
    for(int k = 2; k <= n; ++k) {
        for(int i = 1; i + k <= n; ++i) {
            int j = i + k;
            if(a[i] > a[j]) continue;
            for(int k = i + 1; k < j; ++k) {
                if(a[k] >= a[i] && a[k] <= a[j]) {
                    dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] - 1);
                }
            }
        }
    }
    m.n = n;
    m.zero();
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            m.mat[a[i]][a[j]] = max(m.mat[a[i]][a[j]], dp[i][j]);
        }
    }
    m = m^T;
    int ans = 0;
    for(int i = 1; i <= m.n; ++i) {
        for(int j = 1; j <= m.n; ++j) {
            ans = max(ans, m.mat[i][j]);
        }
    }
    printf("%d\n", ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值