Bridging signals
Problem Description ' Oh no, they ' ve done it again ', cries the chief designer at the Waferland chip factory. Once more The routing designers has screwed up completely, making the signals on the chip connecting the ports of the Ctional blocks cross each of the other and the place. At this late stage of the process, it is too
Expensive to redo the routing. Instead, the engineers has to bridge the signals, using the third dimension, so, no, and cross signals. However, bridging is a complicated operation, and thus it's desirable to bridge as few signals as possible. The call for a computer program This finds the maximum number of signals which may is connected on the silicon surface wit Hout rossing each and imminent. Bearing in mind that there is housands of signal ports at the boundary of a functional block, the problem asks quite a Lot of the programmer. Is you up to the task?
Figure 1. To the left:the, blocks ' ports and their signal mapping (4,2,6,3,1,5). To the Right:at most three signals is routed on the silicon surface without crossing. The dashed signals must be bridged.
A Typical situation is schematically depicted in Figure 1. The ports of the functional blocks is numbered from 1 to p and 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 RA Nge 1 to P, in which the i:th number pecifies which ports on the right side should is connected to the i:th port on the Lef T side.
The signals cross if and only if the straight lines connecting the both ports of each pair do.
Inputon the first line of the input, there are 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 and the Ctional blocks. Then follow P lines, describing the signal mapping:on the i:th line is the port number of the "The Block on the" right side WHI CH should is connected to the i:th port of the block on the left side.
Outputfor Each test scenario, output one line containing the maximum number of signals which is routed on the silicon Surface without crossing each other.
Sample INPUT4 6 4 2 6 3 1 5 10 2 3 4 5 6 7 8 9 10 18 8 7 6 5 4 3 2 19 5 8 9 2 3 1 7 4 6
Sample OUTPUT3 91 4
1#include <cstdio>2#include <algorithm>3#include <cstring>4 using namespacestd;5 6 intv[40005];7 intd[40005];8 9 intMain ()Ten { One //freopen ("In.txt", "R", stdin); A intT,p,i,len; -scanf"%d",&t); - while(t--) the { - intlen=1; -scanf"%d",&p); - for(i=0; i<p;i++) + { -scanf"%d",&v[i]); +d[i]=1; A } atd[i]=1; -d[len]=v[0]; - for(i=1; i<p;i++) - { - if(v[i]>D[len]) -d[++len]=V[i]; in Else - { to intTemp=upper_bound (d+1, D+len,v[i])-D; +d[temp]=V[i]; - } the } *printf"%d\n", Len); $ }Panax Notoginseng return 0; -}
HDU 1950 Bridging signals (LIS O (NLOGN))