ACM题的算法

来源:百度知道 编辑:UC知道 时间:2024/05/20 17:52:04
Problem description
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input
The first line of input contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output
Output must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input
7
1 7 3 5 9 4 8

Sample Output
4

最长子序列的问题。。。。用动态划分吧。。
代码如下:
#include <stdio.h>
#include <malloc.h>
void maxlen(int L[],int n )

{
int *B=(int *)malloc(sizeof(int)*(n+1));
B[0]=-10000;
B[1]=L[0];
int maxlen = 1;
int p,r,m;
for(int i = 1;i<n;i++)
{
p=0;r=maxlen;
while(p<=r)
{
m = (p+r)/2;
if(B[m]<L[i]) p = m+1;
else r = m-1;
}
B[p] = L[i];
if(p>maxlen) maxlen++;
}
free(B);
printf("%d\n",maxlen);
}

int main()
{
int num,*a;
scanf("%d",&num);
a=(int *)malloc(sizeof(int)*(num));
for(int i=0;i<num;i++)
scanf("%d",(a+i));
maxlen(a,num);
free(a);
return 0;
}

我改好了

晕~~别什么算法你都用动态划分啊~~~!!!

我也是ACM的选手~

我是c++编的,算法肯