[cpp]
/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
Copyright (c) 2012 panyanyany All rights reserved.
URL : http://poj.org/problem?id=1631
Name : 1631 Bridging signals
Classification : 最長上升子序列
Date : Wednesday, July 11, 2012
Time Stage : half an hour
Result:
10420085 panyanyany
1631
Accepted 472K 125MS C++
1557B 2012-07-11 07:29:31
Test Data :
Review :
//----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std ;
#define MEM(a, v) memset (a, v, sizeof (a)) // a for address, v for value
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define INF (0x3f3f3f3f)
#define MAXN (40005)
#define L(x) ((x)<<1)
#define R(x) (((x)<<1)|1)
#define M(x, y) (((x)+(y)) >> 1)
#define DB //
int a[MAXN], order[MAXN];
int LIS(int a[], int n)
{
int i, u, l, m, len;
len = 1;
MEM(order, INF); // 找上升子序列時初始化 INF,找下降子序列時初始化為0
for (i = 0; i < n; ++i)
{
u = len;
l = 1;
while (l <= u)
{
m = (l + u) >> 1;
if (order[m] < a[i])
l = m + 1;
else
u = m - 1;
}
if (order[l] > a[i])
order[l] = a[i];
if (len < l)
len = l;
// printf("^ %d ^", l);
}
return len;
}
int main()
{
int i, n, tc;
while (scanf("%d", &tc) != EOF)
{
while (tc--)
{
scanf("%d", &n);
for (i = 0; i < n; ++i)
scanf("%d", a+i);
printf("%d\n", LIS(a, n));
}
}
return 0;
}
作者:panyanyany