poj - 1631 - Bridging signals(LIS)
2014-09-30 09:48
302 查看
题意:两列数,每列p(p < 40000)个,两列都按1, 2, 3...排列,初始时左右两列一对一匹配,求这些匹配中不交叉的最大匹配数。
题目链接:http://poj.org/problem?id=1631
——>>求出输入序列的LIS,左边已是升序,所以LIS对应的左边序号也是升序,一一匹配后不会交叉,LIS的长度就是答案。因为p达40000个,O(n^2)的算法不能满足需要,应使用O(nlongn)以下的算法。。
#include <cstdio>
#include <algorithm>
#include <cstring>
using std::max;
using std::lower_bound;
const int MAXN = 40000 + 10;
const int INF = 0x3f3f3f3f;
int P;
int p[MAXN], arrLis[MAXN];
int dp[MAXN];
int ReadInt()
{
int nRet= 0;
char chIn;
while ((chIn = getchar()) >= '0' && chIn <= '9')
{
nRet = nRet * 10 + chIn - '0';
}
return nRet;
}
void Read()
{
P = ReadInt();
for (int i = 1; i <= P; ++i)
{
p[i] = ReadInt();
}
}
void Dp()
{
int nRet = -1;
for(int i = 1; i <= P; i++) arrLis[i] = INF;
for(int i = 1; i <= P; i++)
{
int k = lower_bound(arrLis + 1, arrLis + 1 + P, p[i]) - arrLis;
arrLis[k] = p[i];
nRet = max(nRet, k);
}
printf("%d\n", nRet);
}
int main()
{
int n;
scanf("%d", &n);
getchar();
while (n--)
{
Read();
Dp();
}
return 0;
}
题目链接:http://poj.org/problem?id=1631
——>>求出输入序列的LIS,左边已是升序,所以LIS对应的左边序号也是升序,一一匹配后不会交叉,LIS的长度就是答案。因为p达40000个,O(n^2)的算法不能满足需要,应使用O(nlongn)以下的算法。。
#include <cstdio>
#include <algorithm>
#include <cstring>
using std::max;
using std::lower_bound;
const int MAXN = 40000 + 10;
const int INF = 0x3f3f3f3f;
int P;
int p[MAXN], arrLis[MAXN];
int dp[MAXN];
int ReadInt()
{
int nRet= 0;
char chIn;
while ((chIn = getchar()) >= '0' && chIn <= '9')
{
nRet = nRet * 10 + chIn - '0';
}
return nRet;
}
void Read()
{
P = ReadInt();
for (int i = 1; i <= P; ++i)
{
p[i] = ReadInt();
}
}
void Dp()
{
int nRet = -1;
for(int i = 1; i <= P; i++) arrLis[i] = INF;
for(int i = 1; i <= P; i++)
{
int k = lower_bound(arrLis + 1, arrLis + 1 + P, p[i]) - arrLis;
arrLis[k] = p[i];
nRet = max(nRet, k);
}
printf("%d\n", nRet);
}
int main()
{
int n;
scanf("%d", &n);
getchar();
while (n--)
{
Read();
Dp();
}
return 0;
}
相关文章推荐
- 最长上升子序列(LIS)POJ 1631 Bridging Signals
- POJ 1631 Bridging signals(LIS:最长上升子序列)
- poj 1631 LIS nlogn
- POJ 1631 Bridging signals LIS(最长递增子序列) +nlogn算法+二分查找
- POJ 1631 Bridging signals LIS(最长递增子序列) +nlogn算法+二分查找
- poj1631 dp 最长上升子序列LIS
- HDU 1950 POJ 1631 Bridging signals【LIS,二分优化】
- poj 1631 最多能有多少条不交叉的线 最大非降子序列 (LIS)
- Poj 1631 Bridging signals(二分+DP 解 LIS)
- (经典)POJ-1631 LIS,偏序问题
- POJ 1631 && HDU 1950 Bridging signals(LIS)
- Poj 1631 Bridging signals(二分+DP 解 LIS)
- poj-1631-Bridging signals-最长上升序列(LIS)
- POJ 1631 LIS 模板题
- POJ 1631 / HDU 1950 / ZOJ 1986 / Northwestern Europe 2003 Bridging signals (DP&LIS)
- [POJ](1631)Bridging signals ---- LIS+O(nlogn)优化(二分)
- poj 2533 & poj 1631 Longest Ordered Subsequence( LIS果题 )
- POJ_1631_Bridging_Signals_(动态规划,LIS)
- POJ 1631 nlogn求LIS
- poj 1631 Bridging signals LIS 最长非递减子序列