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 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 Input4642631510234567891018876543219589231746
Sample OUTPUT3 9 1 4 Topic
To find the longest ascending subsequence, the data range is slightly larger.
Analysis
N-party algorithms may time out, only with the NLOGN algorithm.
F[i] indicates what the minimum value of the end of the subsequence to sing for I, updated every time a value is added, can be used to find the optimization with two points.
Code
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 inta[50010],f[50010];7 8 intMain ()9 {Ten intT,n,len; Onescanf"%d",&t); A while(t--) - { -scanf"%d",&n); the for(intI=1; i<=n; ++i) scanf ("%d",&A[i]); -Len =1; -f[1] = a[1]; - for(intI=2; i<=n; ++i) + { - if(A[i]>f[len]) F[++len] =A[i]; + Else A { at intpos = Lower_bound (f+1, f+len+1, A[i])-F; -F[pos] =A[i]; - } - } -printf"%d\n", Len); - } in return 0; -}
Recommended article: http://blog.csdn.net/shuangde800/article/details/7474903
Bridging signals (maximum ascent self-sequence Nlogn algorithm)