Input
Input starts with an integer T (≤ 12), denoting the number of test cases.
Output
For each case, print the case number and the length of the maximum possible Wavio sequence.Sample Input
3Case 3: 1
题目分析:从左到右,从右到左分别求一次最长上升子序列,求的时候用nlogn的解法记录到当前数字所能组成的上升子序列的最大长度,比如第i个点左到右记录的为ldp[i],右到左记录的为rdp[i],答案就是2 *min(ldp[i], rdp[i]) - 1,减1是因为中间那个数字重复了一次
#include
#include
#include
using namespace std;
int const MAX = 1e5 + 5;
int n, a[MAX];
int lstk[MAX], rstk[MAX];
int ldp[MAX], rdp[MAX];
int ltop, rtop;
int main() {
int T;
scanf("%d", &T);
for(int ca = 1; ca <= T; ca ++) {
printf("Case %d: ", ca);
scanf("%d", &n);
for(int i = 0; i < n; i ++) {
scanf("%d", &a[i]);
}
ltop = rtop = 0;
for(int i = 0; i < n; i ++) {
ldp[i] = 1;
if(ltop == 0 || lstk[ltop - 1] < a[i]) {
lstk[ltop ++] = a[i];
}
else {
int pos = lower_bound(lstk, lstk + ltop, a[i]) - lstk;
lstk[pos] = a[i];
}
ldp[i] = ltop;
}
for(int i = n - 1; i >= 0; i --) {
rdp[i] = 1;
if(rtop == 0 || rstk[rtop - 1] < a[i]) {
rstk[rtop ++] = a[i];
}
else {
int pos = lower_bound(rstk, rstk + rtop, a[i]) - rstk;
rstk[pos] = a[i];
}
rdp[i] = rtop;
}
int ans = 1;
for(int i = 0; i < n; i ++) {
ans = max(ans, 2 * (min(rdp[i], ldp[i])) - 1);
}
printf("%d\n", ans);
}
}