HDU-1950(LCS)(Bridging signals)

来源:互联网 发布:免费域名注册永久 编辑:程序博客网 时间:2024/06/05 02:17

HDU-1950(LCS)(Bridging signals)

Bridging signals

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1476    Accepted Submission(s): 965


Problem 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
4642631510234567891018876543219589231746
 

Sample Output
3914
注:这题题目很复杂,但解法很简单,是lcs模板题。

题意:有两列数,第一列数根据相应关系,和第二列数进行连线。若一条一条连线,最多画m条线时,这些线互不相交,在画m+1条线时就有相关线条交叉了。

样例分析:

如:

6

4

2

6

3

1

5

6表示有6行数,如样列第一行为4,表示第一列的第一行数和第二列的第四行数有一条连线。

样列第二行的2表示:第一列的第二行数和第二列的第二行数之间有一条连线。样列第三行的6表示:第一列的第三行的数和第二列的第六行的数之间有一条连线。。。。。。。

第一列和第二列的数,都是从1到p的连续的数。

求连线不交叉的最大线条数目,由于第一列的数是顺序递增的,也就是求第二列被连接的数的最大上升子序列的长度。

第一组样列的结果为3,  即:    2----2;4------3;6-------5,其中2、3、5是第二列被连接的数的最大上升子序列。

My  solution:

/*2016.4.22*/

#include<stdio.h>#include<cstring>#include<algorithm>using namespace std;int a[44000],dp[40010];int n,m,h;int search(int x)//折半查找 {int i,j,k,left=1,right=h,path;while(left<=right){path=(left+right)/2; if(dp[path]<a[x]&&a[x]<=dp[path+1])return path+1;else{if(dp[path]>a[x])right=path;elseleft=path;  } } } int  main(){int i,j,k,t,temp;scanf("%d",&t);while(t--){temp=0;scanf("%d",&n);for(i=1;i<=n;i++)   scanf("%d",&a[i]);temp=dp[1]=a[1];h=1;for(i=2;i<=n;i++){if(a[i]>dp[h]){h++;dp[h]=a[i];}else if(a[i]<=dp[1]){dp[1]=a[i];}else{k=search(i);dp[k]=a[i];}}printf("%d\n",h);}return 0;}


0 0
原创粉丝点击