//****************************************************************************************************
//
// 求一个数组的最长递减子序列 - C++ - by Chimomo
//
// 题目: 求一个数组的最长递减子序列,比方{8, 14, 6, 2, 8, 14, 3, 2, 7, 4, 7, 2, 8, 101, 23, 6, 1, 2, 1, 1}的最长递减子序列为{14。8,3。2,1}。
//
// Answer: Scan from left to right, maintain a decreasing sequence. For each number, binary search in the decreasing sequence to see whether it can be substituted.
//
//**************************************************************************************************** #include <iostream>
#include <cassert>
#include <stack> using namespace std ; int BinarySearch(int *A, int nTarget, int nLen); // Find the longest decreasing sequence in array A of length nLen.
void FindLongestDecreasingSequence(int *A, int nLen)
{
int *index = new int[nLen];
int *LDS = new int[nLen];
index[0] = A[0];
LDS[0] = 1;
int indexLen = 1; for (int i = 1; i < nLen; i++)
{
int pos = BinarySearch(index, A[i], indexLen);
index[pos] = A[i];
LDS[i] = pos + 1;
if(pos >= indexLen)
{
indexLen++;
}
} int ResultLen = indexLen; for (int i = nLen; i >= 0; i--)
{
if(LDS[i] == ResultLen)
{
index[ResultLen - 1] = A[i];
ResultLen--;
}
} for (int i = 0; i < indexLen; i++)
{
cout << index[i] << " ";
} delete [] index;
} // Binary search nTarget in array A of length nLen.
int BinarySearch(int *A, int nTarget, int nLen)
{
assert(A != NULL && nLen > 0);
int start = 0;
int end = nLen - 1; while (start <= end)
{
int mid = (start + end) / 2; if(nTarget > A[mid])
{
end=mid-1;
}
else if(nTarget<A[mid])
{
start=mid+1;
}
else
{
return mid;
}
} return start;
} int main()
{
int A[] = {8, 14, 6, 2, 8, 14, 3, 2, 7, 4, 7, 2, 8, 101, 23, 6, 1, 2, 1, 1};
int nLen = sizeof(A) / sizeof(int);
FindLongestDecreasingSequence(A, nLen);
return 0;
} // Output:
/*
14 8 7 6 2 1
*/

算法 - 求一个数组的最长递减子序列(C++)的更多相关文章

  1. php怎样求一个数组中最长的

    <?php $arr = array( 0 => 'd', 1 => '68b3', 2 => 'a86', 3 => 'c9aa97b23b71d5c', 4 => ...

  2. 求一个数组的最大k个数(java)

    问题描写叙述:求一个数组的最大k个数.如,{1,5,8,9,11,2,3}的最大三个数应该是,8,9,11 问题分析: 1.解法一:最直观的做法是将数组从大到小排序,然后选出当中最大的K个数.可是这种 ...

  3. 用递归的方法求一个数组的前n项和

    用递归的方法求一个数组的前n项和 public class Demo1 { /* * 用递归的方法求一个数组的前n项和 */ public static void main(String[] args ...

  4. JS求一个数组元素的最小公倍数

    求几个数的最小公倍数就是先求出前两个数的最小公倍数,然后再把这个最小公倍数跟第三个数放在一起来求最小公倍数,如此类推... var dbList = []; //两个数的最小公倍数 function ...

  5. POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)

    题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...

  6. HOJ Recoup Traveling Expenses(最长递减子序列变形)

    A person wants to travel around some places. The welfare in his company can cover some of the airfar ...

  7. 最长递减子序列(nlogn)(个人模版)

    最长递减子序列(nlogn): int find(int n,int key) { ; int right=n; while(left<=right) { ; if(res[mid]>ke ...

  8. 求一个数组的最大子数组(C/C++实现)

    最大子数组:要求相连,加起来的和最大的子数组就是一个数组的最大子数组.编译环境:VS2012,顺便说句其实我是C#程序员,我只是喜欢学C++. 其实这是个半成品,还有些BUG在里面,不过总体的思路是这 ...

  9. 求一个数组中最小的K个数

    方法1:先对数组进行排序,然后遍历前K个数,此时时间复杂度为O(nlgn); 方法2:维护一个容量为K的最大堆(<算法导论>第6章),然后从第K+1个元素开始遍历,和堆中的最大元素比较,如 ...

随机推荐

  1. JavaScript 中对变量和函数声明提前的演示样例

    如题所看到的,看以下的演示样例(能够使用Chrome浏览器,然后F12/或者右键,审查元素.调出开发人员工具,进入控制台console输入)(使用技巧: 控制台输入时Shift+Enter能够中途代码 ...

  2. MyEclipse改动内存大小

    方式一网上说的(没有測试过): 找到MyEclipse的安装文件夹,一般假设不改动的话默觉得C:\MyEclipse10.1\Genuitec\MyEclipse 10.1有一个myeclipse.i ...

  3. SecureCRT学习之道:SecureCRT 经常使用技巧

    快捷键: 1. ctrl + a :  移动光标到行首 2. ctrl + e :移动光标到行尾 3. ctrl + d :删除光标之后的一个字符 4. ctrl + w : 删除行首到当前光标所在位 ...

  4. oracle得到建表语句

    第一种方法是使用工具,如:pl/sql developer,在[工具]--[导出用户对象]出现就可以得到建表脚本. 第二种方法是,sql语句. DBMS_METADATA.GET_DDL包可以得到数据 ...

  5. Caused by: java.lang.NoClassDefFoundError: org/apache/neethi/AssertionBuilderFactory

    转自:https://blog.csdn.net/iteye_8264/article/details/82641058 1.错误描述 严重: StandardWrapper.Throwable or ...

  6. Windows远程桌面和360

    Windows的远程桌面输错了一次密码, 然后就怎么都连接不上了, 查了半天发现 傻缺360会默认屏蔽Windows的远程桌面和数据库连接..... 大家没事都卸载了360吧

  7. Spark基本运行流程

    不多说,直接上干货! Spark基本运行流程 Application program的组成 Job : 包含多个Task 组成的并行计算,跟Spark action对应. Stage : Job 的调 ...

  8. poj 2135 Farm Tour【 最小费用最大流 】

    第一道费用流的题目--- 其实---还是不是很懂,只知道沿着最短路找增广路 建图 源点到1连一条容量为2(因为要来回),费用为0的边 n到汇点连一条容量为2,费用为0的边 另外的就是题目中输入的了 另 ...

  9. 阿里云大学Linux学习路线图(学+测)重磅上线!

    推荐:阿里云大学—Linux运维学习路线(点击获取免费课程) 全新“学+测”模式 每阶段包含初.中.高三个难度等级考试,学完即测,找准短板,助您全方位自测掌握程度 课程系统全面 课程体系涵盖从Linu ...

  10. PHP的soap 之 nusoap 的使用

    今天不知道为什么想起了soap 这个东西,然后就弄了下,在php上使用的是nusoap. 一些基本的使用,高深的麻烦您自己看手册去 这个软件的下载在http://sourceforge.net/pro ...