UVA - 10534

来源:互联网 发布:游禧科技有限公司知乎 编辑:程序博客网 时间:2024/06/05 14:24

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1475

10534 - Wavio Sequence
Time limit: 3.000 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
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
Sample Output
9
9
1

这道题的意思是让我们求一个上升子序列和一个下降字序列,且两边的长度是相等的,由于用正常的 dp算法O(n2) 算法会TLE,
所以这里用二分法求最长上升子序列,
二分法求最长上升子序列:复杂度为O(n×logn):
它是通过一个栈来实现的,我们遍历一个母串,如果当前值大于栈顶元素的值,我们将其压入栈,而当前位置i的最长上升子序列的长度
就是栈顶指针的值(或+1),如果当前值等于栈顶元素的值,不压入栈,同样当前位置 i 的最长上升子序列的值就是栈顶指针的值
(或+1),如果当前值小于栈顶元素的值,不压入栈,但是我们要用二分法找出恰好不小于当前值的那个位置,这个位置我们这里定义
为x,并将x位置的值替换为当前值,而当前位置 i 的最长上升子序列的长度就是x这个指针的值(或+1)

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int n, a[10100], len1[10100], len2[10100], b[10100];void L(int len[],int a[]){    int dp[10100];    int t=0;    dp[t]=-1;    for(int i=1; i<=n; i++){        if(a[i]>dp[t]){//如果a[i]>栈顶部元素,则压栈            dp[++t]=a[i];            len[i]=t;        }        else{//如果a[i]不大于栈顶部元素,则二分查找第一个比a[i]大的元素            int l=1,r=t;            while(l<=r){                int m=(l+r)/2;                if(a[i]>dp[m])                    l=m+1;                else                    r=m-1;            }            //替换a[i]            dp[l]=a[i];            len[i]=l;        }    }//    for(int i=1; i<=n; i++)//        cout<<dp[i]<<" ";//    cout<<endl;}int main(){    int i, j, s, mmax, ans;    while(~scanf("%d",&n)){        for(i=1; i<=n; i++){            scanf("%d",&a[i]);            b[n-i+1] = a[i];            len1[i] = 0;            len2[i] = 0;        }        L(len1,a);        L(len2,b);//        for(i=1; i<=n; i++)//            cout<<len1[i]<<" ";//        cout<<endl;//        for(i=1; i<=n; i++)//            cout<<len2[i]<<" ";        mmax = -1;        ans = 0;        for(i=1; i<=n; i++){            ans = min(len1[i],len2[n-i+1])*2-1;            mmax = max(mmax, ans);        }        printf("%d\n",mmax);    }    return 0;}


0 0
原创粉丝点击