HOJ 1288
Bridging Signals
Time limit: 1sec. Submitted: 281Memory limit: 32M Accepted: 131
Source : ACM ICPC Northwestern European Regional 2003
'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?
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 ith number specifies which port on the right side should be connected to the ith 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 ith line is the port number of the block on the right side which should be connected to the ith 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 #include<stdio.h>
2
3 int main()
4 {
5 int test,i,k,a[40000],b[40001],c[40000],maxlen,low,high,mid;
6 //a是输入序列,b[i]是长度为i的序列的最小末元素,c[i]是以a[i]为终点的的最长上升序列长度
7 scanf("%d",&test);
8 while(test --)
9 {
10 scanf("%d",&k);
11 for(i = 0;i < k;i ++)
12 {
13 scanf("%d",&a[i]);
14 b[i] = 50000;//先把数组置一个大数值
15 }
16
17 c[0] = 1;//以a[i]为末的最长长度
18 maxlen = 1;
19 b[1] = a[0];
20 for(i = 1;i < k;i ++)
21 {
22 low = 1;
23 high = k;
24 while(low <= high)
25 {
26 mid = (low + high)/2;
27 if(a[i] > b[mid])
28 low = mid + 1;
29 else if(a[i] < b[mid])
30 high = mid - 1;
31 }
32 if(low > 1)
33 c[i] = low;
34 else c[i] = 1;
35 if(a[i] < b[c[i]])b[c[i] ] = a[i];//记录最小的那个
36
37 if(c[i] > maxlen) maxlen = c[i];
38 }
39 printf("%d"n",maxlen);
40 }
41 return 0;
42 }
43
算法描述:
a[]是输入的序列,用c[i]表示以a[i]为末元素的最长递增子序列的长度,b[i]是长度为i的最长递增子序列的末
元素中最小的元素。那么c[i + 1]就等于所有c[j],0<=j<=i中最大的且a[j]<a[i + 1]的。怎么找出这个a[j]呢?
逐个枚举的话复杂度就是O(n^2);用二分法查找的话就能降到O(nlogn),数组b就是用于二分查找的,很容易看出b[]
是上升的。因此可以用二分法在b中找b[j]小于a[i]的最大长度j,在上面的代码中,从二分法的while循环出来
之后low - 1的值就是那个最大长度。这题我想了好久,真正实现的时候又改了好久才弄好。。。。真辛苦~
不过我感觉这道题确实挺有意义的,能锻炼一些很基本算法方面的能力。因此和大家分享了。