POJ 1631——LIS最长上升子序列的三种解法

Description

‘Oh no, they’ve done it again’, cries the chief designer at the Waferland chip factory. Once more the routing designers have screwed up completely, making the signals on the chip connecting the ports of two functional blocks cross each other all over the place. At this late stage of the process, it is too expensive to redo the routing. Instead, the engineers have to bridge the signals, using the third dimension, so that no two signals cross. However, bridging is a complicated operation, and thus it is desirable to bridge as few signals as possible. The call for a computer program that finds the maximum number of signals which may be connected on the silicon surface without crossing each other, is imminent. Bearing in mind that there may be thousands of signal ports at the boundary of a functional block, the problem asks quite a lot of the programmer. Are you up to the task?


A typical situation is schematically depicted in figure 1. The ports of the two functional blocks are numbered from 1 to p, from top to bottom. The signal mapping is described by a permutation of the numbers 1 to p in the form of a list of p unique numbers in the range 1 to p, in which the i:th number specifies which port on the right side should be connected to the i:th port on the left side.Two signals cross if and only if the straight lines connecting the two ports of each pair do.

Input

On the first line of the input, there is a single positive integer n, telling the number of test scenarios to follow. Each test scenario begins with a line containing a single positive integer p < 40000, the number of ports on the two functional blocks. Then follow p lines, describing the signal mapping:On the i:th line is the port number of the block on the right side which should be connected to the i:th port of the block on the left side.

Output

For each test scenario, output one line containing the maximum number of signals which may be routed on the silicon surface without crossing each other.

Sample Input

4
6
4
2
6
3
1
5
10
2
3
4
5
6
7
8
9
10
1
8
8
7
6
5
4
3
2
1
9
5
8
9
2
3
1
7
4
6

Sample Output

3
9
1
4

题目大意

咱也甭管他到底什么应用场景了,直接蹦主题,意思就是给你一串数字,让你求最长上升子序列的最长长度。

题目分析

方法一(超时的方法,但最简单易懂):

  1. 思路简单,就是dp[i]记录的是第i个数字他在能实现的最大长度。
  2. 两个循环就可以搞定,但是复杂度太高,O(nm)级别,对于POJ上来说,不会让你通过的。
    代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<limits.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<set>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
int dp[40000];
int arr[40000];
int main(){
	int n;
	scanf("%d",&n);
	int x;
	while(n--){
		scanf("%d",&x);
		fill_n(dp,40000,1);
		int res=1;
		for(int i=0;i<x;i++){
			scanf("%d",&arr[i]);
		}
		for(int i=1;i<x;i++){
			for(int j=0;j<i;j++){
				if(arr[j]<arr[i]){
					dp[i]=max(dp[i],dp[j]+1);
				}
			}
			res=max(res,dp[i]);
		}
		printf("%d\n",res);
	}
	
	return 0;
}

方法二(超时方法,但这是方法三的基础,必须先掌握这个)

  1. dp[i]记录的不再是长度了,而是子序列长度为i+1中所有子序列的末尾最小值。举个例子解释一下,(1,2,3)是一组,(2,4,5)是另一组,两组都是长度为3的上升子序列,但是第一组的末尾元素3小于第二组,所以dp[2]=3(这个例子不适合这道题,只是用来解释一下意思而已)
  2. 那么我们需要对每个arr里面的元素按顺序从前到后考虑,对于arr[i],需要做以下讨论:
    ①j=0,也就是子序列长度为1时,这个必须要和arr[i]比一下,然后取最小值
    ②j>0&&dp[j-1]<arr[i]&&dp[j]>arr[i],此时arr[i]比dp[j-1]大又比dp[j]小,那么就把dp[j]给他替换掉,符合dp的定义。
  3. 为了提高速度,可以记录res(此时找到的最大长度),每一个arr[i]其实就只更新dp中的每一个元素值,不会更新两次,所以更新完之后咱就continue,节约一点点时间。
    代码
#include<iostream>
#include<cstdio>
#include<string>
#include<limits.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<set>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define inf	INT_MAX
int dp[40000];
int arr[40000];
int main(){
	int n;
	scanf("%d",&n);
	int x;
	while(n--){
		scanf("%d",&x);
		fill_n(dp,40000,inf);
		int res=1;
		for(int i=0;i<x;i++){
			scanf("%d",&arr[i]);
		}
		for(int i=1;i<x;i++){
			for(int j=0;j<=res;j++){
				if(j==0&&arr[i]<dp[j]){
					res++;
					dp[j]=arr[i];
					continue;
				}else if(dp[j-1]<arr[i]&&arr[i]<dp[j]){
					dp[j]=arr[i];
					if(j==res){
						res++;
					}
					continue;
				}
			}
		}
		printf("%d\n",res);
	}
	
	return 0;
}

方法三(AC):

  1. 和方法二思路一摸一样,但是为什么方法二超时了呢,我们知道dp数组他是严格上升的吧!那我还按照线性查找一个一个找这么一个dp[j]满足条件,复杂度自然就上去了。
  2. 我们只能在搜索上做点功夫,采用logn级别的搜索,正好STL中有lower_bound()可以满足我们的要求。
#include<iostream>
#include<cstdio>
#include<string>
#include<limits.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<set>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define inf	INT_MAX
int dp[40000];
int arr[40000];
int main(){
	int n;
	scanf("%d",&n);
	int x;
	while(n--){
		scanf("%d",&x);
		fill_n(dp,40000,inf);
		for(int i=0;i<x;i++){
			scanf("%d",&arr[i]);
		}
		for(int i=0;i<x;i++){
			*lower_bound(dp,dp+x,arr[i])=arr[i];
		}
		printf("%d\n",lower_bound(dp,dp+x,inf)-dp);
	}
	
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值