HDU1950-Bridging signals-longest ascending subsequence
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 Rosing each other, is imm Inent. 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 ). 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 shocould 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 shocould 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
464 2 6 3 1 510 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 Output
3914
Question nature: Find the longest ascending subsequence (no repeated numbers here ).
We have two ways to solve this problem. You can refer to the D sequence questions on shuoj. Here is the question link ::
There are two main ideas: (1) lower_bound (2). If you think the code is hard to understand, you can click the link above.
The two methods have the same idea. Store the minimum value of the length of the neutron sequence of array A as I in array S. We use 3, 2, 4, 6, 7, 3 as an example to demonstrate behavior traversal. The column is an array of S, and the changes have been marked out to help you understand. Here, a [I]> s [j] & a [I] <= s [j + 1] should put a [I] in s [j + 1] location. So the key is to find j and know where a [I] is put. The above two methods are used to find j. (Here lower_bound returns j + 1 directly.) We can find that the values in the s array must increase sequentially, which is also a necessary condition for the use of the binary method.
Demo
| 0 |
1 |
2 |
3 |
4 |
| 1 |
3 |
|
|
|
| 2 |
2 |
|
|
|
| 3 |
2 |
4 |
|
|
| 4 |
2 |
4 |
6 |
|
| 5 |
2 |
4 |
5 |
|
| 6 |
2 |
4 |
5 |
7 |
| 7 |
2 |
3 |
5 |
7 |
| |
|
|
|
|
The second method code is provided here ::
# Include
# Include
# Include # define INF 0x3f3fusing namespace std; const int N = 1e5 + 5; int s [N]; int n, p, a [N]; int len; int main () {cin> n; while (n --) {cin> p; memset (s, 0, sizeof (s); for (int I = 0; I
> A [I]; s [1] = a [0]; len = 1; // The length starts from 1 for (int I = 1; I
S [len]) s [++ len] = a [I]; else {/**************/int l = 1, r = len, mid; // here we use the left-closed and right-closed method int ans = 0; while (l <= r) {mid = (l + r) /2; if (s [mid]
If the code is not easy to understand, click the link ::
For the first type of code, you only need to replace the code between two/***************/
int p = lower_bound(s+1,s+len+1,t)-s;s[p] = t;
You can.
3914
3914
The two methods have the same idea. Store the minimum value of the length of the neutron sequence of array A as I in array S. We use 3, 2, 4, 6, 7, 3 as an example to demonstrate behavior traversal. The column is an array of S, and the changes have been marked out to help you understand. Here, a [I]> s [j] & a [I] <= s [j + 1] should put a [I] in s [j + 1] location. So the key is to find j and know where a [I] is put. The above two methods are used to find j. (Here lower_bound returns j + 1 directly.) We can find that the values in the s array must increase sequentially, which is also a necessary condition for the use of the binary method.
Demo
| 0 |
1 |
2 |
3 |
4 |
| 1 |
3 |
|
|
|
| 2 |
2 |
|
|
|
| 3 |
2 |
4 |
|
|
| 4 |
2 |
4 |
6 |
|
| 5 |
2 |
4 |
5 |
|
| 6 |
2 |
4 |
5 |
7 |
| 7 |
2 |
3 |
5 |
7 |
| |
|
|
|
|