算法 - 求一个数组的最长递减子序列(C++)
//****************************************************************************************************
//
// 求一个数组的最长递减子序列 - 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++)的更多相关文章
- php怎样求一个数组中最长的
<?php $arr = array( 0 => 'd', 1 => '68b3', 2 => 'a86', 3 => 'c9aa97b23b71d5c', 4 => ...
- 求一个数组的最大k个数(java)
问题描写叙述:求一个数组的最大k个数.如,{1,5,8,9,11,2,3}的最大三个数应该是,8,9,11 问题分析: 1.解法一:最直观的做法是将数组从大到小排序,然后选出当中最大的K个数.可是这种 ...
- 用递归的方法求一个数组的前n项和
用递归的方法求一个数组的前n项和 public class Demo1 { /* * 用递归的方法求一个数组的前n项和 */ public static void main(String[] args ...
- JS求一个数组元素的最小公倍数
求几个数的最小公倍数就是先求出前两个数的最小公倍数,然后再把这个最小公倍数跟第三个数放在一起来求最小公倍数,如此类推... var dbList = []; //两个数的最小公倍数 function ...
- POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)
题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...
- HOJ Recoup Traveling Expenses(最长递减子序列变形)
A person wants to travel around some places. The welfare in his company can cover some of the airfar ...
- 最长递减子序列(nlogn)(个人模版)
最长递减子序列(nlogn): int find(int n,int key) { ; int right=n; while(left<=right) { ; if(res[mid]>ke ...
- 求一个数组的最大子数组(C/C++实现)
最大子数组:要求相连,加起来的和最大的子数组就是一个数组的最大子数组.编译环境:VS2012,顺便说句其实我是C#程序员,我只是喜欢学C++. 其实这是个半成品,还有些BUG在里面,不过总体的思路是这 ...
- 求一个数组中最小的K个数
方法1:先对数组进行排序,然后遍历前K个数,此时时间复杂度为O(nlgn); 方法2:维护一个容量为K的最大堆(<算法导论>第6章),然后从第K+1个元素开始遍历,和堆中的最大元素比较,如 ...
随机推荐
- sedna载入xml文件
如果有一个xml文件a.xml.须要把它载入到sedna数据库xml_db里. sedna是通过se_term把xml载入到数据库的.有两种方法: 1.通过se_term的-query參数. se_t ...
- Ant批量处理jmeter脚本
Ant是一个可以把代码从某个地方拿来,编译,再拷贝到某个地方去的构建工具.一时冲动学习一下,顺便王婆卖瓜尝试着处理jmeter的脚本,于是,采坑之旅也从此开始.本文省略ant安装步骤和ant脚本说明, ...
- Llama-impala on yarn的中间协调服务
本文基于CDH发行版下的Hadoop Yarn和Impala 早期的Impala版本号中.为了使用Impala.我们一般会在以Client/Server的结构在各个集群节点启动impala-serve ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 拓扑排序
C. Mail Stamps One day Bob got a letter in an envelope. Bob knows that when Berland's post offic ...
- android WebViewClient和WebChromeClient
一.Android之WebViewClient与WebChromeClient的区别 ANDROID应用开发的时候可能会用到WEBVIEW这个组件,使用过程中可能会接触到WEBVIEWCLIENT与W ...
- display:block jquery.sort()
对所有的块元素都没有意义,块元素的dispaly属性默认值为block,没必要再显式定义——除非你之前对块元素的display属性重新定义过.===========================多罗 ...
- UESTC--1269--ZhangYu Speech(模拟)
ZhangYu Speech Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit ...
- Maven 学习笔记(一)
定义 Maven 是基于项目对象模型(POM)的软件项目管理工具,它采用纯 java 编写,用于管理项目的构建,最早在 Jakata Turbine 项目中开始被使用.它包含了一个项目对象模型(Pro ...
- SQLiteHelp
using System; using System.Collections.Generic; using System.Text; using System.Data.SQLite; using S ...
- stackoverflow 加载特慢解决方案,配置 hosts 屏蔽速度慢的第三方 API
127.0.0.1 ajax.googleapis.com www.googletagservices.com www.gravatar.com 127.0.0.1 securepubads.g.do ...