UVA

来源:互联网 发布:js获取鼠标当前位置 编辑:程序博客网 时间:2024/06/06 03:25

原题:

      Wavio is a sequence of integers. It has some interesting prop erties.
Wavio is of o dd length i.e.L = 2n+ 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 beN
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
91


题意:  

       在一串整数序列中,找出一个子序列长度为2*n+1,严格前n+1项递增,后n+1项递减,求满足条件的最长子序列长度。


思路:

       正向反向分别求LIS,则ans=max( min(d1[i],d2[i])*2-1),d1[i]为正向LIS,d2[i]为反向LIS。


#include <iostream>#include <iomanip>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <deque>#include <string>#include <cmath>#include <vector>#include <utility>#include <set>#include <map>#include <sstream>#include <climits>//#pragma comment(linker, "/STACK:1024000000,1024000000")#define pi acos(-1.0)#define INF 2147483647using namespace std;typedef long long ll;typedef pair <int,int > PP;int n;int s[10005];int d1[10005],d2[10005];int g[10005];int main(){    while(scanf("%d",&n)!=EOF)    {        memset(s,0,sizeof(s));        for(int i=0; i<n; i++)            scanf("%d",&s[i]);        int ans;        memset(d1,0,sizeof(d1));        for(int i=1; i<=n; i++)            g[i]=INF;        for(int i=0; i<n; i++)        {            int k=lower_bound(g+1,g+n+1,s[i])-g;            d1[i]=k;            g[k]=s[i];        }        reverse(s, s+n);        memset(d2,0,sizeof(d2));        for(int i=1; i<=n; i++)            g[i]=INF;        for(int i=0; i<n; i++)        {            int k=lower_bound(g+1,g+n+1,s[i])-g;            d2[i]=k;            g[k]=s[i];        }        reverse(d2,d2+n);        ans=-1;        for(int i=0;i<n;i++)            ans=max(ans,(min(d1[i],d2[i])*2-1));        printf("%d\n",ans);    }    return 0;}


原创粉丝点击