您的位置:首页 > 其它

POJ 2533 最长递增子序列

2013-11-02 22:37 381 查看
//POJ 2533  n^2 版
#include "stdio.h"
#include "queue"
#include "math.h"
using namespace std;
const int maxn = 1005;
int n;
int num[maxn],d[maxn];
int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&n);
for( int i = 1; i <= n; i ++ )
{
scanf("%d",&num[i]);
}
int maxs = 0;
for( int i = 1; i <= n; i ++ )
{
d[i] = 1;
for( int j = 1; j <= i-1; j ++ )
{
if( num[j] < num[i] && d[i] < d[j] + 1 )
{
d[i] = d[j] + 1;
}
}
if( d[i] > maxs )
maxs = d[i];
}
printf("%d\n",maxs);
return 0;
}


#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int maxn = 1005;
int n;
int stack[maxn];
int main()
{
int temp,top = 0;
scanf("%d",&n);
stack[0] = -1;
for( int i = 1; i <= n; i ++ )
{
scanf("%d",&temp);
if( temp > stack[top] )
stack[++top] = temp;
else
{
int ld = 1, rd = top, mid;
while( ld <= rd )
{
mid = ( ld+rd )/2;
if( temp > stack[mid] )
ld = mid + 1;
else
rd = mid - 1;
}
stack[ld] = temp;
}
}
printf("%d\n",top);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: