//****************************************************************************************************
//
// 求一个数组的最长递减子序列 - 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. JAVA学习第五十一课 — IO流(五)流的操作基本规律

    转换流: InputStreamReader:字节到字符的桥梁.解码 OutputStreamWriter:字符到字节的桥梁.编码 流的基本规律 1.明白源和汇 源:InputStream.Reade ...

  2. C++中 pair 的使用方法

    #include<iostream> #include<string> #include<map> using namespace std; // pair简单讲就 ...

  3. numpy的scale就是 x-mean/std

    >>> from sklearn import preprocessing >>> import numpy as np >>> a=np.arr ...

  4. ROS-Rviz-turtlebot3仿真信息查看

    前言:Rviz是ROS自带的一种3D可视化工具. 一.安装turtlebot3功能包 1.1 安装依赖包 sudo apt-get install ros-kinetic-joy ros-kineti ...

  5. Ubuntu16.04下沙盒数据导入到 Neo4j 数据库(图文详解)

    不多说,直接上干货! 参考博客 http://blog.csdn.net/u012318074/article/details/72793914   (表示感谢)  前期博客 Neo4j沙盒实验申请过 ...

  6. Combox程序中自定义数据源

    有combox控件,命名为cbxPutStatus,在程序中为其手工定义数据源,并绑定 private void POrderSplitFrm_Load(object sender, EventArg ...

  7. composer的一些操作

    版本更新 命令行下:composer self-update 设置中国镜像 composer config -g repo.packagist composer https://packagist.p ...

  8. html5 好用功能总结

    1.表格元素 a.<caption>设置表格标题 b.<colgroup> . <col> 设置列 //style span 2.分组元素 a.<blockq ...

  9. Zxing实现在线二维码生成程序

    关于zxing的使用请参考笔者的另外一篇博文:Java二维码生成与解码工具Zxing使用 首先我们来看看效果: 在文本框中输入内容后点击生成二维码按钮,应用自动对文本框中的内容进行编码,生成二维码图片 ...

  10. 自定义一个简单的web框架

    from wsgiref.simple_server import make_server def book(request):     #视图函数 return [b'<h1> book ...