题意:
给出T<=107个周期的N<=100的序列,求这个序列的LIS
分析:
因为序列a1, a2, ..., anT最多有n个不同的元素,它的任意一个不减子序列中最多有n − 1个上升元素对.
根据这个,我们可以先预处理出前n个周期(a1, ..., an2),以及后n个周期(anT − n + 1, ..., anT)的LIS.
f[i]:=以a[i]结尾的前n个周期的LIS的最大长度
g[i]:=以a[i]开头的后n个周期的LIS的最大长度
ans=max{f[i]+(T−2n)count(ai)+max{g[j],aj>=ai∩j∈[nT−n+1,nT]},i∈[1,n2]}
ps:count(ai)=cnt of ai in a1,...,an
怎么来求这个呢,嘛,这一个很明显的set贪心辣,sort一下维护满足条件的g[j]最大值,然后不断更新ans即可
代码:
//
// Created by TaoSama on 2015-10-04
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, T, a[N], f[N], g[N], h[N], cnt[305];
//f: end i g: start i
int r[N];
bool cmp(int x, int y) {
return a[x] > a[y];
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &T) == 2) {
memset(cnt, 0, sizeof cnt);
for(int i = 1; i <= n; ++i) {
scanf("%d", a + i);
++cnt[a[i]];
}
int t = min(T, n << 1);
for(int i = 1; i < t; ++i)
for(int j = 1; j <= n; ++j)
a[j + i * n] = a[j];
int ans = 0;
if(T <= n << 1) {
memset(h, 0x3f, sizeof h);
for(int i = 1; i <= n * t; ++i) {
int k = upper_bound(h + 1, h + n * t + 1, a[i]) - h;
ans = max(ans, k);
h[k] = a[i];
}
} else {
memset(h, 0x3f, sizeof h);
for(int i = 1; i <= n * n; ++i) {
int k = upper_bound(h + 1, h + n * n + 1, a[i]) - h;
f[i] = k;
h[k] = a[i];
}
memset(h, 0x3f, sizeof h);
for(int i = n * n; i; --i) {
int k = upper_bound(h + 1, h + n * n + 1, -a[i]) - h;
g[i] = k;
h[k] = -a[i];
}
for(int i = 1; i <= n * n; ++i) r[i] = i;
sort(r + 1, r + n * n + 1, cmp);
set<int> s;
for(int i = 1, j = 1; i <= n * n; ++i) {
int x = r[i];
while(j <= n * n && a[r[j]] >= a[r[i]]) s.insert(g[r[j++]]);
int tmp = f[x] + (T - 2 * n) * cnt[a[x]] + (*s.rbegin());
ans = max(ans, tmp);
}
}
printf("%d\n", ans);
}
return 0;
}
UPD:
其实暴力n段,将出现最多的插入中间也能ac,虽然正确性不是那么显然
代码:
//
// Created by TaoSama on 2015-10-04
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e6 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, T, a[N], g[N];
int cnt[305];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &T) == 2) {
memset(cnt, 0, sizeof cnt);
int maxv = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", a + i);
maxv = max(maxv, ++cnt[a[i]]);
}
int t = min(T, n);
for(int i = 1; i < t; ++i)
for(int j = 1; j <= n; ++j)
a[j + i * n] = a[j];
memset(g, 0x3f, sizeof g);
int ans = 0;
for(int i = 1; i <= n * t; ++i) {
int k = upper_bound(g + 1, g + n * t + 1, a[i]) - g;
ans = max(ans, k);
g[k] = a[i];
}
ans += maxv * (T - t);
printf("%d\n", ans);
}
return 0;
}