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

UVA - 10534相关推荐

  1. UVA 10534 - Wavio Sequence

    这道题的意思是让我们求一个上升子序列 + 一个下降字序列,且两边的长度是相等的,由于用正常的 O(n2) 算法会 TLE ,所以这里我们采用二分法求最长上升子序列,这里需要利用两个栈来储存" ...

  2. uva 10534——Wavio Sequence

    题意:给定一个序列,求一个最长的序列,使得他的前半部分是递增的,而后半部分是递减的,且两部分的长度一样. 思路:经典的LIS问题,和openjudge登山问题一样,前后各扫一遍,找到前置和倒置的LIS ...

  3. UVA 10534 Wavio Sequence DP LIS

    题意:求一个波浪子序列,就是是前一半是上升子序列,后一半是下降子序列(子序列的长度必须为奇数). 分别从左右两个方向求LIS,然后在统计最大值就行了 //#pragma comment(linker, ...

  4. UVa在线比赛单题汇总-----DP专题

    动态规划基础 例题 LA 3882 UVa 3882 - And Then There Was One 递推------------无力orz UVa 10635 10635 - Prince and ...

  5. π-Algorithmist分类题目(3)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(3) Probability ...

  6. [搜索]UVa 129 困难的串

    题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...

  7. uva 401.Palindromes

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. Uva 3767 Dynamic len(set(a[L:R])) 树套树

    Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...

  9. UVA 11752 超级幂

    UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  10. UVa 11174 - Stand in a Line

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

最新文章

  1. Thinkphp3.2学习(一)
  2. Matlab的部分文件操作
  3. Python 技术篇 - 通过代码查看文本的编码类型实例演示,如何查看文件的编码类型,文件编码查看方法
  4. java 文件读入 数组,将文本文件读入2d数组java
  5. TextView之二:常用属性
  6. IdentityServer4 之Client Credentials走起来
  7. 来领.NET Core学习资料,7天整理了30多个G(适合各阶段.Net开发者)
  8. 汇编语言中常见的标志位: CF, PF, AF, ZF, SF,TF,IF,DF, OF
  9. 汕头市队赛 SRM16 T2
  10. 关于ioremap,request_mem_region
  11. 网络 HTTP状态码大全
  12. CVE-2021-35464: ForgeRock AM远程代码执行漏洞
  13. python办公室妙用-python eval()函数的妙用和滥用
  14. 利用IP地址查询接口来查询IP归属地
  15. micro hdmi引脚定义义_系,意、义、意义,有理数的几何解释,合理存在的数(有理数)...
  16. 计算机专业设计(论文)内容及要求,计算机专业毕业设计要求.doc
  17. 知我者谓我心忧,不知我者谓我何求
  18. IT运维人员,该如何规划自己的职业路?
  19. MySQL自定义函数调用不出结果
  20. java实现大乐透彩票

热门文章

  1. BERT4GCN:利用BERT中间层特征增强GCN进行基于方面的情感分类
  2. 业界总结 | 如何改进双塔模型,才能更好的提升你的算法效果?
  3. 【模型压缩】Only Train Once:微软、浙大等研究者提出剪枝框架OTO,无需微调即可获得轻量级架构...
  4. UniDrop:一种简单而有效的Transformer提升技术
  5. 手推公式+项目实操复现!《机器学习》完整详解
  6. 推荐 | 一个超好的OpenCV4学习社区
  7. 读《scikiit-learn机器学习》黄永昌第四章
  8. 【每日算法Day 93】不用额外空间,你会旋转一个矩阵吗?
  9. 6.4 随机森林实战
  10. 不会真有人觉得聊天机器人难吧——使用BERT加载预训练模型得到中文句子向量