您的位置:首页 > 其它

poj 1631 LIS nlogn算法

2015-08-09 14:48 323 查看
题意:给出n个点,分别和1~n相连构成相交线段,现在要去几条线段,要求剩下的线段不想交,且不相交的线段数量最多。

因为p<40000,所以不能用n^2的dp。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#pragma warning(disable :4996)
using namespace std;

const int Max = 40005;
int c[Max];

int Binary(int l, int r, int k)
{
while (l < r)
{
int mid = l + r >> 1;
if (c[mid] >= k)
r = mid;
else l = mid + 1;
}
return l;
}

int main()
{
int T, n, m;
scanf("%d", &T);
while (T--)
{
int len = 0;
c[0] = -1;
scanf("%d", &n);
while (n--)
{
scanf("%d", &m);
if (c[len] < m)
c[++len] = m;
else
{
int pos = Binary(1, len, m);
c[pos] = m;
}
}
printf("%d\n", len);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: