Wavio Sequence

Wavio is a sequence of integers. It has some interesting properties.

·  Wavio is of odd length i.e. L = 2*n + 1.

·  The first (n+1) integers of Wavio sequence makes a strictly increasing sequence.

·  The last (n+1) integers of Wavio sequence makes a strictly decreasing sequence.

·  No two adjacent integers are same in a Wavio sequence.

For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is not a valid wavio sequence. In this problem, you will be given a sequence of integers. You have to find out the length of the longest Wavio sequence which is a subsequence of the given sequence. Consider, the given sequence as :

1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1.


Here the longest Wavio sequence is : 1 2 3 4 5 4 3 2 1. So, the output will be 9.

 

Input

The input file contains less than 75 test cases. The description of each test case is given below: Input is terminated by end of file.

 

Each set starts with a postive integer, N(1<=N<=10000). In next few lines there will be N integers.

 

Output

For each set of input print the length of longest wavio sequence in a line.


我自己写的代码:思路和大多数人一样,从两边找到到每一个位置的最长递增序列O(n^2),最后进行一遍扫描O(n),这样用时太长了。

int dp(const int n)
{
int i, k;
inc[0] = 1;
for(i = 1; i < n; i++)
{
inc[i] = 1;
for(k = 0; k < i; k++)
{
if(s[i] > s[k])
{
inc[i] = max(inc[i], 1 + inc[k]);
}
}
}
dec[n-1] = 1;
for(i = n - 2; i >= 0; i--)
{
dec[i] = 1;
for(k = n - 1; k > i; k--)
{
if(s[i] > s[k])
{
dec[i] = max(dec[i], 1 + dec[k]);
}
}
}

int best = 1;
for(i = 1; i < n; i++)
{
best = max(best, min(dec[i], inc[i]));
}
return 2 * best - 1;
}


网上找到的答案:

int stack[10000]; //the elem at this stack is some index
int top;

int b_search(int v) //[x, y)
{
int x = 0, y = top;
while(x < y)
{
int m = (x+y) / 2;
if(s[stack[m]] >= v) y = m;
else x = m + 1;
}

return x;
}

int dp(int n)
{
top = stack[0] = 0;
inc[0] = 1;

for(int i = 1; i < n; i++)
{
if(s[i] > s[stack[top]])
{
inc[i] = inc[stack[top]] + 1;
stack[++top] = i;
}
else
{
int m = b_search(s[i]);
inc[i] = inc[stack[m]];
stack[m] = i;
}
}

dec[n-1] = 1;
stack[top = 0] = n - 1;

for(int k = n - 2; k >= 0; k--)
{
if(s[k] > s[stack[top]])
{
dec[k] = dec[stack[top]] + 1;
stack[++top] = k;
}
else
{
int m = b_search(s[k]);
dec[k] = dec[stack[m]];
stack[m] = k;
}
}

int best = 1;
for(int t = 1; t < n; t++)
{
best = max(best, min(inc[t], dec[t]));
}

return 2 * best - 1;
}

这里用了一个不一般的“栈”,栈中存放的是合适的数列下标。

在扫描查找到 i 位置的最长递增序列时分情况进行处理:

if(s[i] > s[stack[top]]) d[i] = d[stack[top]] + 1;

else 由于在栈中依次保存的下标对应的数列元素是递增的,现在用二分查找找到这样一个栈下标u,使得s[stack[u]]恰好不小于s[i],再用 i 替换stack[u],并且inc[i] = inc[stack[u]],可以用数学归纳法证明这样的替换以后可以使得以后得到尽可能长的递增序列,并且当前 i 位置的最长递增子序列长度也是正确的,这里的时间复杂度是O(nlgn)。


评价:

①在有序的情况下应该想到二分查找,并且本来就要一个O(nlgn)的算法;

②不要思维太固定了,虽然后来自己想到了一个不那么完美的方案,但一开始总是受到最长公共子序列的影响。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值