HDU1950-Bridging signals-最长上升子序列

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 rossing each other, is imminent. Bearing in mind that there may be housands 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?

Figure 1. To the left: The two blocks‘ ports and their signal mapping (4,2,6,3,1,5). To the right: At most three signals may be routed on the silicon surface without crossing each other. The dashed signals must be bridged.

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 pecifies 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

题目本质::求最长上升子序列(这里没有重复数字)。

我们有两种思路求可以参考shuoj上的D序列的题目。这里给出题目的题解链接::

主要是两种思路::(1)lower_bound(2)二分法,如果觉得代码不易理解可以点上面的链接

两种方法的思路是一样的。将数组A中子序列长度为 i 的最小值存放在数组S中。我们以3 2 4 6  5 7 3 为例进行演示行为遍历,列为数组S,变化的地方已经标出来,有助于理解。在这里a[ i ] > s[ j ]&&a[i]<=s[ j + 1 ]就应该把a[ i ]放在s[ j+1 ]的位置。所以关键就是找出 j 就知道把a[ i ]放在哪了。上面的两种方法就是用来寻找 j的
。(在这里lower_bound直接返回 j + 1 )

我们可以发现s数组中的值必然是有序递增的,这也是可以利用二分法的一个必要条件。

演示
0 1 2 3 4
1 3      
2 2      
3 2 4    
4 2 4 6  
5 2 4 5  
6 2 4 5 7
7 2 3 5 7
         

这里给出第二种方法代码::

#include <iostream>
#include<cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5 + 5;
int s[N];
int n,p,a[N];
int len;
int main()
{
	cin>>n;
	while(n--){
		cin>>p;
		memset(s,0,sizeof(s));
		for(int i = 0;i<p;i++)cin>>a[i];
		s[1] = a[0];len = 1;//长度从1开始
		for(int i = 1;i<p;i++){

			int t = a[i];
			if(t>s[len])s[++len] = a[i];
			else{
		/*************/int l = 1,r = len,mid;//这里的二分法采用了左闭右闭的思路
               <span style="white-space:pre">			</span>int ans = 0;
				while(l<=r)
				{
					mid = (l+r)/2;
					if(s[mid]<t)
                        {l = mid +1;ans = max(ans,mid);}//ans即为思路中的j,j必然为s数组中小于t的最大的数
					else r = mid-1;
				}
				s[ans+1] = t;/******************/
			}
		}
		//for(int i = 1;i<p;i++){cout<<s[i];}//有必要可以打开看看s中存的是什么值
		cout<<len<<endl;
	}
	return 0;
}

如果代码不易理解请点击链接,链接为::

第一种的代码只要将两个/**************/之间的代码换为

int p = lower_bound(s+1,s+len+1,t)-s;
s[p] = t;

就可以了。

3
9
1
4 
3
9
1
4 

两种方法的思路是一样的。将数组A中子序列长度为 i 的最小值存放在数组S中。我们以3 2 4 6  5 7 3 为例进行演示行为遍历,列为数组S,变化的地方已经标出来,有助于理解。在这里a[ i ] > s[ j ]&&a[i]<=s[ j + 1 ]就应该把a[ i ]放在s[ j+1 ]的位置。所以关键就是找出 j 就知道把a[ i ]放在哪了。上面的两种方法就是用来寻找 j的
。(在这里lower_bound直接返回 j + 1 )

我们可以发现s数组中的值必然是有序递增的,这也是可以利用二分法的一个必要条件。

演示
0 1 2 3 4
1 3      
2 2      
3 2 4    
4 2 4 6  
5 2 4 5  
6 2 4 5 7
7 2 3 5 7
         

版权声明:大家随意浏览。

时间: 2024-10-10 21:04:07

HDU1950-Bridging signals-最长上升子序列的相关文章

hdu----(1950)Bridging signals(最长递增子序列 (LIS) )

Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 667    Accepted Submission(s): 443 Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferland

POJ3903Stock Exchange&amp;&amp;POJ1631Bridging signals最长上升子序列 &amp;&amp;POJ1887Testing the CATCHER(最长下降子序列)(LIS模版题)

题目链接:http://poj.org/problem?id=3903 题目链接:http://poj.org/problem?id=1631 题目链接:http://poj.org/problem?id=1887 题目解析: 这两道题都是直接求最长上升子序列,没什么好说的. POJ 3903这题n为1000000,如果用n^2的算法肯定超时,所以要选择nlogn的算法.都是简单题. #include <iostream> #include <string.h> #include

dp之最长上升子序列

普通做法是O(n^2)下面介绍:最长上升子序列O(nlogn)算法(http://blog.csdn.net/shuangde800/article/details/7474903) /* HDU 1950 Bridging signals -----最长上升子序列nlogn算法 */ #include<cstdio> #include<cstring> #define MAXN 40005 int arr[MAXN],ans[MAXN],len; /* 二分查找. 注意,这个二分

最长上升子序列(LIS)长度的O(nlogn)算法

最长上升子序列(LIS)的典型变形,熟悉的n^2的动归会超时.LIS问题可以优化为nlogn的算法.定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则记录最小的那个最末元素.注意d中元素是单调递增的,下面要用到这个性质.首先len = 1,d[1] = a[1],然后对a[i]:若a[i]>d[len],那么len++,d[len] = a[i];否则,我们要从d[1]到d[len-1]中找到一个j,满足d[j-1]<a[i]<d[j],则根据D的定义,我们需

Bridging signals(NlogN最长上升子序列)

Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2354    Accepted Submission(s): 1536 Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferlan

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). 如果子序列长度相同,那么最末位元素较小的在之后会更加有优势. #p

poj 1631 Bridging signals DP(最长上升子序列)

最近一直在做<挑战程序设计竞赛>的练习题,感觉好多经典的题,都值得记录. 题意:给你t组数据,每组数组有n个数字,求每组的最长上升子序列的长度. 思路:由于n最大为40000,所以n*n的复杂度不够了,会超时. 书上状态方程换成了d[i]——以长度为i+1的上升子序列中末尾元素的最小值. 那么我们在遍历第i个元素时候,以这个元素为末尾元素的最长子序列也就是在d[i]中找到一个小于num[i]的最大值,然后在这个序列末尾加上num[i] 显然,我们在查找时便可以利用二分搜索,从而把复杂度从原来的

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

http://poj.org/problem?id=1631 题意: (题意比较繁琐)本质就是: 给你一个长为n(n<=40000)的整数序列, 要你求出该序列的最长上升子序列LIS. 分析: 如果用O(n^2)的算法的话, 可能会超时. 所以用O(n*logn)的算法. 令g[i]==x表示当前遍历到的长度为i的所有最长上升子序列中的最小序列末尾值为x.(如果到目前为止, 根本不存在长i的上升序列, 那么x==INF无穷大) 假设当前遍历到了第j个值即a[j], 那么先找到g[n]数组的值a[

Bridging signals POJ 1631(最长递增子序列dp)

原题 题目链接 题目分析 由题目知,如果能求出连接点的最长递增子序列,则可以把连接不在该序列中的点的线全部剪掉.而维护最长递增子序列可以用dp来做,考虑到相同长度的递增子序列末尾数字越小越好,可以这样定义dp,dp[i]长度为i的递增子序列的最小末尾值,初始化为INF,由于这个dp具有有序性,因此可以用二分来加快更新,每次遍历到值num[i],只需二分找出大于等于num[i]的更新之即可.最后从扫一遍dp数组即可得到最长长度. 代码 1 #include <iostream> 2 #inclu

Bridging signals(求最长上升自序列nlogn算法)

Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2582    Accepted Submission(s): 1665 Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferlan