求一个序列中 的2*n-1个数字 ,前n+1个数字为严格升序 后n+1个为严格降序,求最长的长度

一开始还没想清楚怎么解,其实就是一个LCS问题,从头到尾以及反序求一下LCS

由于 d[i]为包含了自身的LCS,所以每次比较 min(d1[i],d2[i]),再 2*min-1即可,d1和d2分别为正序和反序的LCS。

由于时间卡的紧,要用之前学过的压栈法求LCS,时间复杂度为n*logn,中间出了一些问题,首先就是保存每个节点的LCS值的时候,如果该点是大于sta[top],那自然LCS为top+1,但是如果不是的话,我一开始写成top,原来不是,是要二分的那个位置才是。。。。还有就是二分一开始调用的是Upper_bound,错了,应该是lower_bound

再复习下 upper_bound是找大于val的第一个位置点,如果val小于整个序列,返回 0,如果val大于整个序列,返回数组的最后一位+1。

lower_bound是找大于并等于val的第一个值,如果序列中没有==val的,那跟upper是一样的效果,如果有相同的,并且同时有几个相同的,则返回第一个相同的数的下标。上界和下界是跟upper一样的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define N 10010
using namespace std;
int d1[N],d2[N],A[N];
int n,sta[N];
void init()
{
int top=;
sta[]=-;
sta[++top]=A[];
d1[]=;
for (int i=;i<=n;i++)
{
if (A[i]>sta[top]){
sta[++top]=A[i];
d1[i]=top;
}
else
{
int loc=lower_bound(sta+,sta+top+,A[i])-sta;
sta[loc]=A[i];
d1[i]=loc;
} }
top=;
sta[++top]=A[n];
d2[n]=;
for (int i=n-;i>=;i--)
{
if (A[i]>sta[top]){
sta[++top]=A[i];
d2[i]=top;
}
else
{
int loc=lower_bound(sta+,sta+top+,A[i])-sta;
sta[loc]=A[i];
d2[i]=loc;
} }
}
void test()
{
for (int i=;i<=n;i++)
{
printf("%d %d\n",d1[i],d2[i]);
}
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=;i<=n;i++)
{
scanf("%d",&A[i]);
}
init();
//test();
int ans=;
for (int i=;i<=n;i++)
{
int tmp=min(d1[i],d2[i]);
ans=max(ans,*tmp-);
}
printf("%d\n",ans);
}
return ;
}

UVA 10534 LCS变种题的更多相关文章

  1. uva 10534 Wavio Sequence LIS

    // uva 10534 Wavio Sequence // // 能够将题目转化为经典的LIS. // 从左往右LIS记作d[i],从右往左LIS记作p[i]; // 则最后当中的min(d[i], ...

  2. UVA 10534 三 Wavio Sequence

    Wavio Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  3. UVa 489 HangmanJudge --- 水题

    UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...

  4. UVa 1585 Score --- 水题

    题目大意:给出一个由O和X组成的串(长度为1-80),统计得分. 每个O的分数为目前连续出现的O的个数,例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3 解题思路:用一个变量t ...

  5. LIS UVA 10534 Wavio Sequence

    题目传送门 题意:找对称的,形如:123454321 子序列的最长长度 分析:LIS的nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理.因为是对称的,所以取较小值*2-1 ...

  6. hdu1159 LCS模板题

    题目分析 pid=1159">原题地址 最简单的最长公共子序列(LCS)问题的模板题了.不解释. ------------------------------------------- ...

  7. 解题报告:hdu1159 common consequence LCS裸题

    2017-09-02 17:07:42 writer:pprp 通过这个题温习了一下刚学的LCS 代码如下: /* @theme:hdu1159 @writer:pprp @begin:17:01 @ ...

  8. hdu 5495 LCS 水题

    LCS Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5495 Descr ...

  9. UVA 11636-Hello World!(水题,猜结论)

    UVA11636-Hello World! Time limit: 1.000 seconds When you first made the computer to print the sentenc ...

随机推荐

  1. 003.Delphi插件之QPlugins,菜单插件加强

    相比上一篇的菜单插件,这个在创建和销毁时候,做了增强,同时做了2个菜单对应的窗口 unit MenuSvc; interface uses windows, classes, SysUtils, Gr ...

  2. 比较 CEILING 和 FLOOR

    CEILING 函数返回大于或等于所给数字表达式的最小整数. FLOOR 函数返回小于或等于所给数字表达式的最大整数. 例如,对于数字表达式  12.9273,CEILING 将返回 13,FLOOR ...

  3. windows上使用git

    开始的时候同事只给了一个地址,类似这样:git@111.111.1.1:ABCDEF (1)如何在Windows上使用Git 有一篇博客不错:http://www.tuicool.com/articl ...

  4. leetcode1143 Longest Common Subsequence

    """ Given two strings text1 and text2, return the length of their longest common subs ...

  5. leetcode1 twoSum

    """ Given an array of integers, return indices of the two numbers such that they add ...

  6. 《Airbnb架构要点分享》阅读笔记

    Airbnb成立于2008年8月,总部位于加利福尼亚州旧金山市.Airbnb是一个值得信赖的社区型市场,在这里人们可以通过网站.手机或平板电脑发布.发掘和预订世界各地的独特房源,其业务已经覆盖190个 ...

  7. MyBatis: No MyBatis mapper was found in '[xx.mapper]' package. Please check your configuration

    在应用入口加入@MapperScan("com.IBM.XXXXX.dao")

  8. Robot set variable if

    ${strid} Set Variable If '${row}' =='2' LFFD_TANK_RAMP ... '${row}' =='3' LFFD_TANK_LANDING

  9. linux下安装mysql5.7(centos6.0)

    注:因为网络原因,这个mysql安装是我以前在学校的时候找到的一个安装包,不过也找到了下载的地址:http://www.itmop.com/downinfo/143061.html下载完成后,把文件上 ...

  10. 计算机操作系统学习(一) Linux常用指令(随时更新)

    1.chmod 以下转载至https://blog.csdn.net/summer_sy/article/details/70142475 chmod u+x file.sh 就表示对当前目录下的fi ...