您的位置:首页 > 大数据 > 人工智能

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

2016-04-27 13:58 363 查看
题目链接: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: