2013
12-26

# Bridging signals

‘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.

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.

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.

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

3
9
1
4

这题目是经典的DP题目，也可叫作LIS（Longest Increasing Subsequence）最长上升子序列 或者 最长不下降子序列。很基础的题目。但是注意的是此题用传统的n^2算法会超时。

A.
O(n^2)算法分析如下：

（a[1]…a[n] 存的都是输入的数）
1、对于a[n]来说，由于它是最后一个数，所以当从a[n]开始查找时，只存在长度为1的不下降子序列；
2、若从a[n-1]开始查找，则存在下面的两种可能性：
（1）若a[n-1] < a[n] 则存在长度为2的不下降子序列 a[n-1],a[n];
（2）若a[n-1] > a[n] 则存在长度为1的不下降子序列 a[n-1]或者a[n]。
3、一般若从a[t]开始，此时最长不下降子序列应该是按下列方法求出的：

4、为算法上的需要，定义一个数组：
int d[n][3];
d[t][0]表示a[t];
d[t][1]表示从i位置到达n的最长不下降子序列的长度;
d[t][2]表示从i位置开始最长不下降子序列的下一个位置。

B.

(1)x < y < t
(2)A[x] < A[y] < A[t]
(3)F[x] = F[y]

(1) D[k]的值是在整个计算过程中是单调不上升的。
(2) D[]的值是有序的，即D[1] < D[2] < D[3] < … < D[n]。

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int a[50000],i,j,t,n,maxn,minn,dp[50000];
int main()
{
cin>>t;
while(t--)
{
cin>>n;
memset(a,0,sizeof(a));
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
cin>>a[i];
}
int sum=0;
dp[0]=-9999999;
for(i=1;i<=n;i++)
{
if(a[i]>=dp[sum])
{
dp[++sum]=a[i];
}
else if(a[i]<dp[sum])
{
minn=1;maxn=sum;
int mid;
while(minn<maxn-1)
{
mid=(maxn+minn)/2;
if(a[i]>dp[mid])
minn=mid;
else
maxn=mid;
}
if(dp[minn]>a[i])
dp[minn]=a[i];
else
dp[maxn]=a[i];
}
}
cout<<sum<<endl;
}
return 0;
}

1. 样例输出和程序输出不吻合，修改一下样例输出吧。我用的是VC编译器，会提示我的i和j变量重复定义