UVA - 10534

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ?rst (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 ?nd
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 ?le contains less than 75 test cases. The description of each test case is given below. Input
is terminated by end of ?le.
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 }

 

以上是关于UVA - 10534的主要内容,如果未能解决你的问题,请参考以下文章

UVa 10534 Wavio Sequence

UVA - 10534

c_cpp UVa 10534 - Wavio序列

2017暑假 动态规划

UVa 1442 - Cave

UVa10410代码