(Binary + dp) poj 1631 bridging Signals

Source: Internet
Author: User
Bridging Signals
Time limit:1000 ms   Memory limit:10000 K
Total submissions:5919   Accepted:3233

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 th IRD 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 func Tional block, the problem asks quite a lot of the programmer. Are you up to the task?

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 specifies 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

4642631510234567891018876543219589231746

Sample output

3914
/* Question: Find the longest ascending subsequence. question: Generally, 0 (N ^ 2) times out. Only 0 (nlogn) is allowed ). let's first review how poj 1887 testing the catcher works.: First, we confirm that the question has the optimal sub-structure. Then we start to recursively construct the optimal sub-structure and define an array OPT [I] to indicate the longest descent sequence of the first I characters containing the I character,: Then the optimal sub-structure recursively generates the optimal formula: OPT [I] = max (OPT [J] + 1 ), num [I] <num [J] & amp; 0 <= j <I <m. However, due to the sub-structure's no-order nature, you can only traverse it to find the largest feasible solution, as a result, the complexity suddenly rises to N ^ 2. Then we want to ask what sub-structures we build recursively do not have increment or disorder ,: the reason is that when we define it, we determine the result generation (the longest descent sequence of the first I character including the I character before OPT [I) to include the I character, it is not possible to make sure OPT [I]> OPT [I-1]. For example: 2 5 4 1 OPT [3] = 2; OPT [4] = 1; how do we create an ordered optimal sub-structure ?? To ensure that OPT [I]> = OPT [I-1] Set OPT [I]: is the oldest sequence of the first I character, this obviously does not work, we lack the subdecoding information, if the update fails, recursive solutions cannot be implemented. so what do we need to solve a n-character string: information ?? 1: The I character (this is the source data ). 2: The oldest sequence of the first I character 3: What is the oldest sequence of the first I character ?, What if there are multiple? Let's look at the third condition: what if there are multiple conditions? For example, 1 5 3 ....; the sequence of the first three characters is known as length 2 at first glance, but there are many solutions: {} and {1, 3} find that one? Of course, the smallest one can be selected to maximize the space for target growth by 3 ~ Infinity> 5 ~ Infinity. Now we have all three conditions. The key is: the steps are coming, how do we store the two conditions 'opt [condition 2] = [Condition 3] is the oldest sequence of the first I characters ..... source codeproblem: 1631 User: wawadimu memory: 608 K time: 110 ms language: C ++ result: accepted source code */# include <iostream> using namespace STD; # define maxn 40010int num [maxn]; int B [maxn]; // B [k] = m; mint OPT [maxn]; // OPT [k] = L; the length of the first K numbers in the longest ascending subsequence is lint P; // array size int main () {// freopen ("1631.txt"," r ", stdin); int CAS; int I, J, K; int L, R; int blen; // Save the size of the current B array (that is, the longest ascending subsequence that has been obtained) scanf ("% d ", & CAS); While (CAS --) {scanf ("% d", & P); for (I = 1; I <= P; I ++) scanf ("% d", & num [I]); B [1] = num [1]; // only one number is, of course, its own blen = 1; // length for (I = 1; I <= P; I ++) // Insert the I-th digit {L = 1; r = blen; while (L <= r) // find a B [k] {int M = (L + r)> 1 that is just greater than num [I; if (B [m] <num [I]) L = m + 1; elser = m-1;} B [l] = num [I]; // Replace the large number with the small number OPT [I] = L; // The maximum size of the first I number blen = blen <L? L: blen; // update the longest part length} printf ("% d/N", blen); // output answer} return 1 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.