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;
}
因为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;
}
相关文章推荐
- Java记录 -4- 运算符 Operator
- leetcode 91: Decode Ways
- codeforces535C:Tavas and Karafs(二分)
- nil和Nil和NULL的判断
- Webform 分页
- 缓存和sd卡的路径(原)
- shell注释
- Lost Cows
- 今天起,用博客记录学习和总结,尝试写作,提高自己,加油!
- POJ 1050 To the Max && POJ 2479 Maximum sum(DP最大连续子段和)
- trustzone技术
- centos 安装ganglia监控工具
- hdu 4622 Reincarnation(后缀自动机,入门级)
- Use After Free Tutorial
- HDOJ 5361 In Touch dijkstra最短路
- LeetCode:Add Two Numbers
- Android之——清理手机SD卡缓存
- python中的break|continue|pass|return
- Android相关博客网址收集
- neuoj 1153