UVA - 10534 Wavio Sequence (LIS变形,dp)

原题:

      Wavio is a sequence of integers. It has some interesting prop erties.
Wavio is of o dd 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
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 2147483647
using 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;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值