HDU ---- (1950) Bridging signals (Maximum ascending subsequence (LIS ))

Source: Internet
Author: User
Tags define local
Bridging Signals

Time Limit: 5000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 667 accepted submission (s): 443


Problem 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.

 

Inputon 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.

 

Outputfor 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 input4642631510234567891018876543219589231746

 

Sample output3914

 

Source nwerc2003 simple question. It is almost the same as a template question...-> _-> but you can understand that there is another algorithm, of course, there are other practices, such as using the line segment tree + dp Practices Code:
1 // # define local 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <iostream> 6 # define INF 0x3f3f3f 7 using namespace STD; 8 const int maxn = 40005; 9 10 int STR [maxn], ANS [maxn], DP [maxn]; 11 int N, DD; 12 13 int Lis (int A [], int N) 14 {15 int I, j, Res = 0; 16 for (I = 1; I <= N; I ++) 17 ans [I] = inf; 18 memset (DP, 0, sizeof (INT) * (n + 1); 19 for (I = 1; I <= N; ++ I) 20 {21 22 DP [I] = lower_bound (ANS + 1, ANS + n + 1, a [I])-ans; 23 // J = bsearch (C, size, a [I]); // replace 24 if (RES <DP [I]) in the existing Sequence res = DP [I]; 25 J = I; 26 if (j> 0 & Ans [DP [J]> A [J]) 27 ans [DP [J] = A [J]; 28} 29 return res; 30} 31 32 33 int main () 34 {35 # ifdef local36 freopen ("test. in "," r ", stdin); 37 # endif38 int CAS; 39 scanf (" % d ", & CAS); 40 while (CAS --) {41 42 scanf ("% d", & N); 43 for (INT I = 1; I <= N; I ++) 44 {45 scanf ("% d", STR + I); 46} 47 printf ("% d \ n", Lis (STR, n )); 48} 49 return 0; 50}
View code

 

 

HDU ---- (1950) Bridging signals (Maximum ascending subsequence (LIS ))

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.