1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1475
2、题目大意:
给定一串数字,求一个子串满足一下要求:子串的长度是L=2*n+1,前n+1个数字是严格的递增序列,后n+1个数字是严格的递减序列,例如123454321就是满足要求的一个子串,输出满足要求的最长的L,
3、正着走一遍LIS,再倒着走一遍LIS,dp[i]表示前i个满足要求的数字的个数,那么dp1[i]和dp2[i]中最小的一个就是L的一半
4、题目:
Wavio Sequence
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
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.
Sample Input Output for Sample Input
10 1 2 3 4 5 4 3 2 1 10 19 1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1 5 1 2 3 4 5 | 9 9 1
|
4、代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 10005
int a[N],b[N],dp1[N],dp2[N];
int n;
void LIS(int dp[],int a[])
{
int stack[N];
int top=0;
stack[top]=-99999999;
for(int i=1; i<=n; i++)
{
//如果a[i]>栈顶部元素,则压栈
if(a[i]>stack[top])
{
stack[++top]=a[i];
dp[i]=top;
}
//如果a[i]不大于栈顶部元素,则二分查找第一个比a[i]大的元素
else
{
int l=1,r=top;
while(l<=r)
{
int mid=(l+r)>>1;
if(a[i]>stack[mid])
{
l=mid+1;
}
else
r=mid-1;
}
//替换a[i]
stack[l]=a[i];
dp[i]=l;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
b[n-i+1]=a[i];
dp1[i]=0;
dp2[i]=0;
}
LIS(dp1,a);
LIS(dp2,b);
int ans=0,maxx=-1;
for(int i=1; i<=n; i++)
{
ans=min(dp1[i],dp2[n-i+1])*2-1;//因为要求递增序列与递减序列的个数相等,则取正反较小的一个
if(ans>maxx)
maxx=ans;
}
printf("%d\n",maxx);
}
return 0;
}