O(nlogn)的算法关键是它建立了一个数组temp[],temp[i]表示长度为i的不下降序列中结尾元素的最小值,用top表示数组目前的长度,算法完成后top的值即为最长不下降子序列的长度。 设当前的以求出的长度为top,则判断num[i]和temp[top]: 1.如果num[i]>=temp[top],即num[i]大于长度为top的序列中的最后一个元素,这样就可以使序列的长度增加1,即top++,然后现在的temp[top]=num[i]; 2.如果num[i]<temp[top],那么就在temp[1]...temp[top]中找到最大的j,使得temp[j]<nu Read More
posted @ 2011-11-20 22:25 Because Of You Views(3597) Comments(0) Diggs(1) Edit
返回的是RMQ的下标,有个地方要注意,就是在rmq模板比较大小的地方要改成<=,要不然如果有一连串的相同数字的话本来是要取的,结果没取#include<string.h>#include<stdio.h>#include<math.h>const int M=1010;int min(int a,int b){return a<b?a:b;}int dp[20][M],LOG[M];void Make_Rmqindex(int n,char b[]){ int i,j; for(i=1;i<=n;i++) dp[0][i]=i; for(i Read More
posted @ 2011-11-20 12:02 Because Of You Views(760) Comments(2) Diggs(0) Edit