您的位置:首页 > 其它

Codeforces Round #323

2015-10-06 16:13 232 查看

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

题意

T组数字,每组n个数字,满足a[i] = a[i-n] ,求LIS

解法

注意到n的范围是100

LIS的转移方程是 dp[i] = max(dp[j]+1) (0 < j < i && A[i] >= A[j])

即第i个dp值 由 前 i - 1 个dp值转移过来,考虑序列具有周期性,有以下结论:

dp值只由前n个值转移而来

证明:

假设存在 j ( 0 < j < i - n )使得任意k (i - n <= k < n) 有 dp[j] > dp[k] && A[i] >= A[j] && A[i] >= A[k]

令 k = j + n

那么 dp[k] 一定可以由 dp[j] 转移而变成 dp[k] + 1, 矛盾。

得证。

这样复杂度由 O(n^2*T^2) 变为 O(n^2*T)

T是1e7,还是比较虚。

在N < T的情况下, 最多转移 N 次即可, 剩下的只需要找到序列里面出现次数那个数,出现次数*(T-N) + 前面的结果即可

因为LIS最多上升N次,不上升的情况当然是贪心的选择上升次数最多的那个数即可。

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int N, T;
int A[105];

int cnt[305];
ll res = 0;
int maxcnt = 0;

ll dp[105];

int main(){
scanf("%d %d",&N, &T);
for(int i = 1; i <= N; i++){
scanf("%d",&A[i]);
cnt[A[i]]++;
if(cnt[A[i]] > maxcnt) maxcnt = cnt[A[i]];
}

for(int i = 1; i <= N && i <= T; i++){
for(int p = 1; p <= N; p++){
dp[p]++;
for(int j = 1; j <= N; j++){
if(j != p && A[p] >= A[j])
dp[p] = max(dp[p], dp[j] + 1);
}
}
//for(int p = 1; p <= N; p++) printf("dp=%d\n",dp[p]);
}
for(int i = 1; i <= N; i++) if(dp[i] > res) res = dp[i];
ll tt = T - N;
//printf("%I64d\n",res);
if(tt > 0){
res += tt * maxcnt;
}
printf("%I64d\n",res);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: