#include<stdio.h>
int a[] , temp[] ;
int n , top ; int binary_search (int x) {
int fir = ;
int last = top ;
int mid ;
while (fir <= last ) {
mid = (fir + last) / ;
if ( x <= temp[mid] ) {
last = mid - ;
}
else {
if (x <= temp[mid + ] )
return mid + ;
else
fir = mid + ;
}
}
} int main () {
// freopen ("a.txt" ,"r" , stdin) ;
while ( scanf ("%d" , &n ) != EOF ) {
for (int i = ; i < n ; i++ ) {
scanf ("%d" , &a[i]) ;
} top = ;//目前最长不下降子序列的长度
temp[top] = a[] ;////temp[i]为长度为i的上升子序列末尾元素的最小值
for (int i = ; i < n ; i++ ) {
if ( a[i] >= temp[top] ) {
temp[++top] = a[i] ;
}
else {
if ( a[i] < temp[] ) {
temp[] = a[i] ;
}
else {
temp[binary_search(a[i])] = a[i] ;
}
}
}
printf ("%d\n" , top + ) ;
}
return ;
}

用二分查找法

 #include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
const int inf = 0x3f3f3f3f ;
std::vector <int> g[ + ] ;
int a[] ;
int dp[] ;
int n ; int dfs (int id , int dep )
{
if (dp[id] != ) return dp[id] ;
if (id == n - ) return dp[id] ;
bool flag = ;
for (int i = id + ; i < n ; i ++) {
if (a[i] > a[id]) {
g[dep].push_back (dfs ( i , dep + ) ) ;
flag = ;
}
}
if (flag) {
int t = max_element (g[dep].begin () , g[dep].end () ) - g[dep].begin () ;
dp[id] += g[dep][t] ;
g[dep].clear () ;
}
return dp[id] ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while ( ~scanf ("%d" , &n) ) {
memset (dp , , sizeof(dp)) ;
for (int i = ; i < n ; i ++) scanf ("%d" , &a[i]) ;
for (int i = ; i < n ; i ++) dp[i] = ;
for (int i = n - ; i >= ; i --) { dfs (i , ) ; }
int len = -inf ;
for (int i = ; i < n ; i ++) len = std::max (len , dp[i]) ;
printf ("%d\n" , len ) ;
}
return ;
}

记忆化搜索

最长不下降子序列 O(nlogn) || 记忆化搜索的更多相关文章

  1. HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...

  2. 求最长不下降子序列(nlogn)

    最长递增子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]<a[j],这样最长的子序列称为最长递增子序列. 设dp[i]表示以i为结尾的最长 ...

  3. [Usaco2008 Feb]Eating Together麻烦的聚餐[最长不下降子序列]

    Description 为了避免餐厅过分拥挤,FJ要求奶牛们分3批就餐.每天晚饭前,奶牛们都会在餐厅前排队入内,按FJ的设想所有第3批就餐的奶牛排在队尾,队伍的前端由设定为第1批就餐的奶牛占据,中间的 ...

  4. 【动态规划】【二分】【最长不下降子序列】洛谷 P1020 导弹拦截

    最长不下降子序列的nlogn算法 见 http://www.cnblogs.com/mengxm-lincf/archive/2011/07/12/2104745.html 这题是最长不上升子序列,倒 ...

  5. tyvj 1049 最长不下降子序列 n^2/nlogn

    P1049 最长不下降子序列 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数第二行n个数 输出格式 ...

  6. 最长不下降子序列的O(n^2)算法和O(nlogn)算法

    一.简单的O(n^2)的算法 很容易想到用动态规划做.设lis[]用于保存第1~i元素元素中最长不下降序列的长度,则lis[i]=max(lis[j])+1,且num[i]>num[j],i&g ...

  7. 最长不下降子序列nlogn算法详解

    今天花了很长时间终于弄懂了这个算法……毕竟找一个好的讲解真的太难了,所以励志我要自己写一个好的讲解QAQ 这篇文章是在懂了这个问题n^2解决方案的基础上学习. 解决的问题:给定一个序列,求最长不下降子 ...

  8. hdu1025 最长不下降子序列nlogn算法

    C - DP Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit I ...

  9. 最长不下降子序列 nlogn && 输出序列

    最长不下降子序列实现: 利用序列的单调性. 对于任意一个单调序列,如 1 2 3 4 5(是单增的),若这时向序列尾部增添一个数 x,我们只会在意 x 和 5 的大小,若 x>5,增添成功,反之 ...

随机推荐

  1. dll,lib文件的导入

    这里介绍了两种方式调用,不过我一般用的是第一种,比较方便. 1动态库函数的调用,可以采用静态链接的方式 ,主要步骤如下: 1) 包含DLL中导出的头文件. 2) 采用#pragma comment(l ...

  2. HoloLens开发手记 - Unity之语音输入

    对于HoloLens,语音输入是三大基本输入方式之一,广泛地运用在各种交互中.HoloLens上语音输入有三种形式,分别是: 语音命令 Voice Command 听写 Diction 语法识别 Gr ...

  3. 最新app store 应用提交经验分享

    由于之前提交实在3月份的时候,后来长时间没有提交了,最近又需要提交,发现苹果已经发生翻天覆地的变化了,真是跟不上时代了啊.... 之前提交的基本也是从网上看的,前面的证书安装部分其实基本是一样的没什么 ...

  4. [bzoj 1027][JSOI2007]合金(解析几何+最小环)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1027 分析: 首先因为一个合金的和为1,所以考虑2个材料合金能否合成一个需求合金的时候 ...

  5. android之显示数据库信息

    关键字 ListView adapter MVC 在android开发中也使用到了MVC架构,其中的xml布局文件就是V,M就是我们定义好的javabean类,而控制器就是就是适配器类adapter ...

  6. 第三十四课:jQuery Deferred详解2

    上一课主要分析了jQuery1.51版本的jQuery Deferred.在jQuery1.6中,jQuery Deferred添加了两个方法,always,pipe. always用来添加回调,无论 ...

  7. org.hibernate.PropertyValueException: not-null property references a null or transient value:

    org.hibernate.PropertyValueException: not-null property references a null or transient value: com.bj ...

  8. oracle 分区表

    分区表用途 分区表通过对分区列的判断,把分区列不同的记录,放到不同的分区中.分区完全对应用透明.Oracle的分区表可以包括多个分区,每个分区都是一个独立的段(SEGMENT),可以存放到不同的表空间 ...

  9. 【BZOJ 3223】文艺平衡树 模板题

    就是打个翻转标记,下推标记时记得交换左右孩子指针,查询kth和中序遍历输出时也记得要下推标记同时交换指针,二者不可缺!←这是易错点 仿陈竞潇学长模板的代码: #include<cctype> ...

  10. 【转】】CTO、技术总监、首席架构师的区别

    经常有创业公司老板来拜访我,常常会拜托给我一句话:帮我找一个CTO. 我解释的多了,所以想把这个写下来,看看你到底需要的应该是啥. 一.高级程序员 如果你是一个刚刚创业的公司,公司没有专职产品经理和项 ...