#include<stdio.h>#include<stdlib.h>#include<time.h>#define ARRAY_SIZE 1000int buf [ARRAY_SIZE];int main(){ srand((unsigned int )time(0)); int i,j,n; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++)buf[i]=rand()%100-10; for…
导言 第一次了解到逆序数是在高等代数课程上.当时想计算一个数列的逆序数直觉就是用两重循环O(n^2)暴力求解.现在渐渐对归并算法有了一定的认识,因此决定自己用C++代码小试牛刀. 逆序数简介 由自然数1,2…,n组成的不重复的每一种有确定次序的排列,称为一个n级排列(简称为排列):或者一般的,n个互不同元素排成一列称为“一个n级排列”.例如,1234和4312都是4级排列,而24315是一个5级排列. 在一个n级排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一…
一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS. 选择算法也就是求一个无序数组中第K大(小)的元素的值的算法,同通常的Top K等算法密切相关. 在CLRS中提到了一种最坏情况线性运行时间的选择算法,在书中给出了如下的文字描述(没有直接给出伪代码). 1.Divide n elements into groups of 5 2.Find median of each group (Ho…
其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话,很快就读完了.你所说的证明和推导大概也都是在第三章介绍了,可以回过头再认真看几遍. 至于课后题,比较难,我只做了前几章,如果要做完需要更多时间和精力.这可以通过之后做算法题来弥补,可以去leetcode等网站找一些经典的算法题做一做,加深理解. Facebook的工程师写的攻略,介绍了用算法导论来…
冒泡.插入.选择排序的时间复杂度为O(n2) Arrays.sort()时间复杂度为nlgn 具体算法实现代码: package recursion; import java.util.Arrays; /** * @author zsh * @company wlgzs * @create 2019-02-17 9:46 * @Describe 冒泡.插入.选择排序与Arrays.sort()的性能差别 */ public class Main3 { /** * 冒泡排序 * @param ar…
算法导论(CLRS)答案 Chapter Section I 1 2 p II 1 2 3 p III 1 2 p IV 1 2 3 4 p V 1 2 3 4 p VI 1 2 3 4 5 p VII 1 2 3 4 p VIII 1 2 3 4 p IX 1 2 3 p XII 1 2 3 XIII 1 2 3 4 p 假设你有兴趣,能够选一个章节去完毕. 欢迎watch/fork/star/contribute我的项目. 欢迎挑错. Follow @louis1992 on github…
本题就是一题LIS(最长递增子序列)的问题.本题要求求最长递增子序列和最长递减子序列. dp的解法是O(n*n),这个应该大家都知道.只是本题应该超时了. 由于有O(nlgn)的解法. 可是因为本题的数据特殊性.故此本题能够利用这个特殊性加速到O(n)的解法.当中的底层思想是counting sort分段的思想.就是假设你不会counting sort的话,就非常难想出这样的优化的算法了. O(nlgn)的利用单调队列性质的解法,利用二分加速是有代表性的,无数据特殊的时候也能够使用.故此这里先给…
题目大意 给定正整数 $n$ 和一个由 $m$ 个正整数构成的可重集合 $A$,满足 $\forall a\in A, a\le n$ 且 $m\le n$ . 定义 $N_t(A) = |\{a\in A\colon a\le t\}|$ .请在 $O(m)$ 的时间内判断 $\forall t = 0, 2, \dots, n, N_t(A)\le t$ 是否成立. 多组询问. 解法 用一个长为 $n$ 的数组 $\mathrm{cnt}[1..n]$ 记录集合 $A$ 中每个数出现的次数,…
本文是一篇笔记,大部分内容取自 CLRS 第三版,第 24.1 节. Exercise 24.1-4 Modify the Bellman-Ford algorithm so that it sets $v.d$ to $-\infty$ for all vertices $v$ for which there is a negative-weight cycle on some path from the source to $v$. Theorem 24.4 (Correctness of…
Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12251   Accepted: 6687 Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. Once more the routing designers have screwed up c…
//maximum and minimum     暴力遍历 O(n) //i-th element dicide and conquer random_selected_partition     k(all the element samller than value[k] put left of value[k],all tht elenment larger than value[k] put right) recurrence test if(i=k)return value[k] i…
//intput array A,output array result.   count array count . //all the elements is in te range of 0~k. //if k=O(n),the complexity is Θ(n) //counting sort is stable for(i=0;i<n;i++)result[i]=0; for(i=1;i<=n;i++)count[a[i]]++; for(i=1;i<=n;i++)count…
//max_heap heap_maximum:return A[1]    O(1); Extract_Heap_maximum:swap(A[1],A[heap.size])    adjust up to down from A[1] to hold the max_heap character   O(lgn)   every stepfind max child increase_Heap_max(i,key):[key>=A[i]],adjust down to up from A[…
//用满二叉树存储,从n/2处开始递归向上调整(n/2后均为叶子节点,无需调整)使得根最大 //满二叉树顺序存储,左子2i,右子2i+1: #include<stdio.h>#include<stdlib.h>#include<time.h>#define ARRAY_SIZE 1000#define RANDOM_SIZE 100int buf [ARRAY_SIZE];int n;max_heap(int x){ int l=2*x,r=l+1,large=x; i…
//the first k elements interviewed and rejected, //for  the latter n-k elements ,if value >max,return value,  else return n'elements. #include<stdio.h>#include<stdlib.h>#include<time.h>#define ARRAY_SIZE 1000#define RANDOM_SIZE 100int…
T(n)=aT(n/b)+f(n); where we can interpret n/b to mean either floor(b/n) or ceil(b/n), Then T (n) has the following asymptotic bounds: 1. If f (n)= O(nlogb a-c) for some constant c> 0, then T (n)=Θ(nlogb a)2.If f (n)= Θ(nlogb a), then T (n)=Θ(nlogb a…
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#define ARRAY_SIZE 1000int buf[ARRAY_SIZE];int main(){ int i,j,n; srand((unsigned int)time(0)); memset(buf,0,sizeof(buf[0])); while(scanf("%d",&n)!=…
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <vector> #include <…
O(n^2)过不了.必须要用一个额外的数组保存当前长度最小值,然后lgn查表 AC代码 #include<cstdio> #include<algorithm> using namespace std; const int maxn=100000+5; const int INF=1<<30; int dp[maxn],a[maxn],ans[maxn]; int main(){ int T,n; scanf("%d",&T); while(…
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20404 Accepted Submission(s): 12231 Problem Description The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj…
http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意: 求LIS,其中的0可以看做任何数. 思路: 因为0可以看做任何数,所以我们可以先不管0,先求一遍LIS,最后再加上0的个数就可以了.当然,每个数需要减去它前面0的个数. 还有这题如果用dp求LIS是要超时的,从别人那里学习了更快的求LIS的方法. 假设存在一个序列d[..] = ,可以看出来它的LIS长度为5.n 下面一步一步试着找出它. 我们定义一个序列B,然后令 i = to 逐个考察这个序…
public class MergeSort { public void mergeSort(int[]data, int left, int right) { if(left >= right) return; int mid = (left + right)/2; mergeSort(data, left, mid); mergeSort(data, mid+1, right); merge(data, left, mid, right); } public void merge(int[]…
今天我们一起来看一下关于最大子数组的一些问题.最大子数组的应用场景可以是这样的:有一天,你搞了一场投资开始炒股,这时你就会想,我怎样才能获得最大的利润呢,最简单的想法就是我在股票的最低价时买入,然后在最高价时卖出,这样利润必然最大.但冷静下来想想这往往是不可能的,你不能保证最高价出现在最低价后面.为了达到这一目的,我们建立了最大子数组模型.我们以一年为时间期限,每个月的股价假设是这样的13 12 15 18 19 18 20 16 13 9 11 10,为了获得最大利益,我们要寻找差值最大的两个…
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 10562    Accepted Submission(s): 2449 Problem Description We are all familiar with sorting algorithms: quick sort, merge sor…
定义及特点: 每个结点有n个关键字和n+1个指向子结点的指针,即有n+1个孩子结点. n个关键字按非递减的顺序存储. 最小度数t>=2,除了根结点的所有内部结点(非叶结点)的孩子数>=t且<=2t,即相应的关键字数>=t-1且<=2t-1. 孩子数2t时为满结点. 搜索操作: 和BST差不多,只不过因为每个结点中的关键字增多,所以先要在结点中找到小于目标元素中的最大的一个关键字,然后在该关键字后的子树中继续递归寻找. 插入操作: 只插入到叶结点中,如果满了就把该叶结点分裂(把…
def mergesort(nums,le,ri): if le>ri-2: return 0 mi=le+(ri-le)//2 a=mergesort(nums,le,mi) b=mergesort(nums,mi,ri) c=merge(nums,le,mi,ri) return a+b+c def merge(nums,le,mi,ri): i,j=le,mi data=[] count=0 while i<mi and j<ri: if nums[i]<nums[j]: d…
前言 临近毕业,开始找工作,近期一直在看算法导论(CLRS)同时各种刷题.希望以后有时间把所有学习心得和刷题心得记录下来. 堆 堆排序和合并排序一样,是一种时间复杂度为O(nlgn)的算法,同时和插入排序一样,是一种就地排序算法(不需要额外的存储空间).堆排序需要用到一种被称为最大堆的数据结构,与java或者lisp的gc不一样,这里的堆是一种数据结构,他可以被视为一种完全二叉树,即树里面除了最后一层其他层都是填满的.也正是因为这样,树里面每个节点的子女和双亲节点的序号都可以根据当前节点的序号直…
在刷LeetCode-1TwoSum的时候,有个人在论坛留言,大致意思如下: 我的算法击败了90%的人,O(nlgn)算法比O(n)算法快. 我觉得这个人是不懂算法的.让我一步一步解释. # O的含义 通俗的说,O表示忽略系数的复杂度上限,常常用一个量级表示,比如n,nlgn. # 忽略的系数重要吗 重要.我觉得<算法>比<算法导论>优秀的原因之一是,作者用实例证明,在不少情况下,复杂度之前的系数很重要.比如,系数可以导致O(n^2)的插入排序可以比O(nlgn)快速排序快. 可以…
# 题目 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: , , , ], target = , Because nums[] + nums[] = + = , , ]…
1. 比较排序算法的下界 (1) 比较排序     到目前为止,我们已经介绍了几种能在O(nlgn)时间内排序n个数的算法:归并排序和堆排序达到了最坏情况下的上界:快速排序在平均情况下达到该上界.     如果仔细观察,我们会发现:在排序的最终结果中,各元素之间的次序依赖于它们之间的比较.我们把这类排序算法统称为比较排序.到目前为止我们介绍的排序算法都是比较排序.下面我们来论证一个事实:任何比较排序算法在最坏情况下都要经过Ω(n lgn)次比较. (2) 决策树模型     在证明之前,我们先介…