UVA - 10534

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)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 int n, a[10100], len1[10100], len2[10100], b[10100];
 7 void L(int len[],int a[])
 8 {
 9     int dp[10100];
10     int t=0;
11     dp[t]=-1;
12     for(int i=1; i<=n; i++){
13         if(a[i]>dp[t]){//如果a[i]>栈顶部元素,则压栈
14             dp[++t]=a[i];
15             len[i]=t;
16         }
17         else{//如果a[i]不大于栈顶部元素,则二分查找第一个比a[i]大的元素
18             int l=1,r=t;
19             while(l<=r){
20                 int m=(l+r)/2;
21                 if(a[i]>dp[m])
22                     l=m+1;
23                 else
24                     r=m-1;
25             }
26             //替换a[i]
27             dp[l]=a[i];
28             len[i]=l;
29         }
30     }
31 //    for(int i=1; i<=n; i++)
32 //        cout<<dp[i]<<" ";
33 //    cout<<endl;
34 }
35 int main(){
36     int i, j, s, mmax, ans;
37     while(~scanf("%d",&n)){
38         for(i=1; i<=n; i++){
39             scanf("%d",&a[i]);
40             b[n-i+1] = a[i];
41             len1[i] = 0;
42             len2[i] = 0;
43         }
44         L(len1,a);
45         L(len2,b);
46 //        for(i=1; i<=n; i++)
47 //            cout<<len1[i]<<" ";
48 //        cout<<endl;
49 //        for(i=1; i<=n; i++)
50 //            cout<<len2[i]<<" ";
51         mmax = -1;
52         ans = 0;
53         for(i=1; i<=n; i++){
54             ans = min(len1[i],len2[n-i+1])*2-1;
55             mmax = max(mmax, ans);
56         }
57         printf("%d\n",mmax);
58     }
59     return 0;
60 }

 

转载于:https://www.cnblogs.com/wudi-accept/p/5547414.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值