首页 > 代码库 > POJ - 1631 Bridging signals(最长上升子序列---LIS)

POJ - 1631 Bridging signals(最长上升子序列---LIS)

题意:左右各n个端口,已知n组线路,要求切除最少的线路,使剩下的线路各不相交,按照左端口递增的顺序输入。

分析:

1、设左端口为l,右端口为r,因为左端口递增输入,l[i] < l[j](i < j),因此若要不相交,r[i] < r[j],由此可以得出,只要求出对应的右端口序列的最长上升子序列的长度即可。

2、最长上升子序列:

dp[i]---长度为i+1的上升子序列中末尾元素的最小值(若不存在,则为INT_INF)。

如果子序列长度相同,那么最末位元素较小的在之后会更加有优势。

#pragma comment(linker, "/STACK:102400000, 102400000")#include<cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<set>#include<map>#include<stack>#include<deque>#include<queue>#include<list>#define Min(a, b) ((a < b) ? a : b)#define Max(a, b) ((a < b) ? b : a)const double eps = 1e-8;inline int dcmp(double a, double b){    if(fabs(a - b) < eps) return 0;    return a > b ? 1 : -1;}typedef long long LL;typedef unsigned long long ULL;const int INT_INF = 0x3f3f3f3f;const int INT_M_INF = 0x7f7f7f7f;const LL LL_INF = 0x3f3f3f3f3f3f3f3f;const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;const int dr[] = {0, 0, 0, -1, 1, -1, -1, 1, 1};const int dc[] = {0, -1, 1, 0, 0, -1, 1, -1, 1};const int MOD = 1e9 + 7;const double pi = acos(-1.0);const int MAXN = 40000 + 10;const int MAXT = 10000 + 10;using namespace std;int dp[MAXN];int a[MAXN];int main(){    int T;    scanf("%d", &T);    while(T--){        int n;        scanf("%d", &n);        for(int i = 0; i < n; ++i){            scanf("%d", &a[i]);        }        memset(dp, INT_INF, sizeof dp);        for(int i = 0; i < n; ++i){            *lower_bound(dp, dp + n, a[i]) = a[i];        }        printf("%d\n", lower_bound(dp, dp + n, INT_INF) - dp);    }    return 0;}

  

POJ - 1631 Bridging signals(最长上升子序列---LIS)