uva10534---Wavio Sequence
发布日期:2022-02-02 02:58:14 浏览次数:23 分类:技术文章

本文共 2466 字,大约阅读时间需要 8 分钟。

Wavio is a sequence of integers. It has some interestingproperties.

·  Wavio is of odd length i.e. L = 2*n+ 1.

·  The first (n+1) integers ofWavio sequence makes a strictly increasing sequence.

·  The last (n+1) integers of Waviosequence makes a strictly decreasing sequence.

·  No two adjacent integers are same in aWavio sequence.

For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Waviosequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is not avalid wavio sequence. In this problem, you will be given a sequence ofintegers. You have to find out the length of the longest Wavio sequence whichis a subsequence of the given sequence. Consider, the given sequence as :

1 2 3 2 1 2 3 4 32 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 outputwill be 9.

 

Input

The input filecontains less than 75 test cases. The description of each test case isgiven below: Input is terminated by end of file.

 

Each set starts with a postiveinteger, N(1<=N<=10000). In next few lines there will be Nintegers.

 

Output

For each set of input print the length of longest waviosequence in a line.

Sample Input                                  Output for 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
 
9
9
1

 

这道题主要是LIS的应用,把串正反走一遍就行,代码中LIS()函数是精华。

代码实现:

#include 
#include
using namespace std;#define N 10001int dp1[10001],dp2[10001],a[10001],b[10001];int t;void LIS(int dp[],int a[]){ int stack[N]; int top=0; stack[top]=-99999999; for(int i=1; i<=t; i++) { //如果a[i]>栈顶部元素,则压栈 if(a[i]>stack[top]) { stack[++top]=a[i]; dp[i]=top; } //如果a[i]不大于栈顶部元素,则二分查找第一个比a[i]大的元素 else { int l=1,r=top; while(l<=r) { int mid=(l+r)>>1; if(a[i]>stack[mid]) { l=mid+1; } else r=mid-1; } //替换a[i] stack[l]=a[i]; dp[i]=l; } }}int main(){ while(cin>>t) { for(int i=1;i<=t;i++) { cin>>a[i]; b[t-i+1]=a[i]; } memset(dp1,0,sizeof(dp1)); memset(dp2,0,sizeof(dp2)); LIS(dp1,a); LIS(dp2,b); int maxx=-1,ans=0; for(int i=1;i<=t;i++) { ans=min(dp1[i],dp2[t-i+1])*2-1; if(ans>maxx) { maxx=ans; } } cout<
<

转载地址:https://blog.csdn.net/u010368749/article/details/22100453 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:hdu1087---Super Jumping! Jumping! Jumping!
下一篇:JVM原理

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年03月11日 10时30分28秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

oracle12c order by,oracle 数据库中order by 的一些高级用法 2019-04-21
oracle8i substr,Oracle中的INSTR,NVL和SUBSTR函数的用法详解 2019-04-21
导出oracle11g的空表,轻松解决oracle11g 空表不能 exp 导出 的问题。 2019-04-21
php把整数拆分成数组,数组拆分处理(整数时的处理),该怎么处理 2019-04-21
oracle numlist,oracle sql str2numlist numtabletype 2019-04-21
php红包平均分配,红包平均分配算法 2019-04-21
linux磁盘的命令是,linux磁盘相关的命令 2019-04-21
linux服务器 缓存,Linux服务器内存使用分析及内存缓存 2019-04-21
linux查进程内存问题,关于linux 查看服务进程内存,cpu,内存占用的一些基础命令... 2019-04-21
linux英文包安装教程视频,Linux源码包安装过程讲解 2019-04-21
linux 关闭rsync服务器,linux下配置rsync服务器和实时同步 2019-04-21
linux初始化TCP服务失败,深入Linux系统追踪TCP初始化 2019-04-21
arch Linux添加源,在Arch Linux系统中使用Archlinuxcn源(清华源)的方法 2019-04-21
私人linux远程连接,Linux远程连接 - osc_5g1gl9wp的个人空间 - OSCHINA - 中文开源技术交流社区... 2019-04-21
windows文件迁移到linux,从Windows到Linux迁移之文件服务器(Samba和AD完美结合) 2019-04-21
linux下vi编辑器的命令大全,linux下VI编辑器命令大全(超级完整版) 2019-04-21
linux结构体数组的定义数组,task_struct结构体中的run_list和array域 2019-04-21
C语言极坐标转直角坐标,C语言实现直角坐标转换为极坐标的方法 2019-04-21
16F877A和24C02通信汇编语言,PIC16f877A读写24c02程序 2019-04-21
用c语言编写小于n的所有素数,关于求N以内素数的一点小问题(N小于一亿) 2019-04-21