"2018.10.3" Halloween Delivery

Source: Internet
Author: User

Input Format

[Lazyjazz or bok choy ...]

Stacks are like a trash can ...--chxer

[Today ...]

Lazyjazz January 1 Big Buy Special buy order Today (November 1) finally arrived, the algorithm (SF) Express Company Lazyjazz Order all the express to two trains, a row in the morning, a column of the afternoon station. Lazyjazz, who received the notice, was thrilled to have spent half a year building two express receiving vehicles-two trucks each carrying a "trash bin", hurried to the railway station.

The process of receiving the shipment is this:

The train that arrives in the morning (for short, No. 1th train, the short train to arrive in the afternoon) all the express need first to use the first express receiving car pulled away. All the shipments in train No. 2nd need to be pulled away with a second express.

Morning, the 1th train has $na $, of which the first $i $ in the car in a total of $ka _i$ Express line in a row, Lazyjazz need to select a car every time, all the express in order to take out in turn, threw him in the barrel of the initial express receiving car, until all the carriage inside the express are taken out.

In the afternoon, train No. 2nd has $NB $ carriage, of which the first $i $ carriage inside a total of $kb _i$ Express line in a row, Lazyjazz also need to select a car every time, the express will be taken out in turn, thrown into his second express receiving car barrels, until all the carriage inside the express are taken out.

Lazyjazz ordered $n $ A shipment, each shipment has a unique, in the $[1,n]$ range of the number, in the end of the reception back home, Lazyjazz asked his housekeeper from $1$ to $n $ scan by number and record each shipment information.

The recording process is this:

We first to the first express receipt of the car bucket named 1th barrels, the second express receiver on the car's bucket named 2nd barrels. Because of the "garbage bin" and the similarity of the stack, there is a robotic arm, each operation can be a bucket top of the express to the top of the other barrel, the butler needs to constantly operate this arm, so that the top of the 1th barrels in turn appears numbered $1$,$2$,$3$ ... $n $ of express, and record express information. That is, the first operation of the robot arm to the top of the 1th barrel is the 1th Express, record information, and then the operation of the robot arm to the top of the 1th barrel is the 2nd Express, record information ... etc...

Unfortunately, when the express delivery, each express will be placed in which train which compartment of which position. The good news is that we know the number of express numbers in each compartment of the two trains. Lazyjazz for humanitarian reasons, the decision to find a choice of the order of the car, so that the butler in the recording of express information when the operation of the robot arm the least number of operations.

Now, give you two trains in each compartment of the express arrangement information, you can answer in the Lazyjazz select the order of the train reasonable circumstances, the Butler Record Express when the operation of the mechanical arm of the minimum number of operations?

Give an example:

If there is a total of $6$ Express

There are $1$ carriages in train No. 1th.

Section $1$ carriage inside the Express number is: 2-4-3

There are $2$ carriages in train No. 2nd.

Section $1$ carriage inside the Express number is: 1-5

Section $2$ carriage inside the Express number is: 6

Well, at the beginning,

1th barrels from the top to the end of the courier number can only be: 3-4-2 (2 first taken out, placed at the bottom, 4 the second to take out, put in the middle, 3 Finally, put on the top)

2nd barrels from top to bottom The courier number can be: 6-5-1 or 5-1-6, and 5-1-6 better (the former will eventually need to $15$ operations, the latter only need $13$ times)

The answer is $13$.

Operation Details:

Initial state: {3-4-2},{5-1-6}

After $2$ operation: becomes {1-5-3-4-2},{6}

After $4$ operation: becomes {2},{4-3-5-1-6}

After $2$ operation: becomes {3-4-2},{5-1-6}

After $1$ operation: becomes {4-2},{3-5-1-6}

After $2$ operation: becomes {5-3-4-2},{1-6}

After $2$ operation: becomes {6-1-5-3-4-2},{}

A total of $13$ operations.

Input Format

The first line of three non-negative integers $n $, $na $, $NB $, express the number of shipments, number 1th train compartments, number 2nd train compartments

Next $na $ lines, the first number of each line is a positive integer $ka $, followed by $ka $ positive integer $A _i$, which indicates that there is a $ka $ express and the arrangement of the courier number in a compartment of train No. 1th

Next $NB $ lines, the first number of each line is a positive integer $kb $, followed by $kb $ positive integer $B _i$, which indicates that there is a $KB $ express and the arrangement of the courier number in a compartment of train No. 2nd

Guaranteed $\sum{ka}+\sum{kb}=n$, all $A _i$ and $B _i$ are not duplicated and guaranteed to \leq a_i,b_i \leq n$

Input in a courier arrangement, the first occurrence of the number is placed in the receiving vehicle, the order is irreversible, see the sample for details

output Format

An integer representing the number of operations in which the final picker has the least number of operations in all compartment selection scenarios

Sample Oneinput
6 1 23 2 4 32 1 51 6
Output
13
explanation

This group sample is the same as the example in the topic description, if you have any questions, please refer to the topic description

Example Twoinput
10 2 33 2 4 91 72 8 12 3 52 10 6
Output
42
example Three

See Sample data download

Restrictions and conventions

For the previous $10\texttt{%}$ data: $na, NB \leq 1, n \leq 1000$

Data for $10\texttt{%}-30\texttt{%}$: $na, NB \leq 1$

Data for $30\texttt{%}-40\texttt{%}$: $na +nb=n$

Data for $40\texttt{%}-60\texttt{%}$: $na, NB \leq 6$

Data for $60\texttt{%}-80\texttt{%}$: $na, NB \leq, n \leq 20$

Data for $90\texttt{%}$: $na, NB \leq 15$

For $100\texttt{%}$ data: $na, NB \leq 20, n \leq 100000, 1 \leq na+nb \leq n$

Guaranteed $\sum{ka}+\sum{kb}=n$, all $A _i$ and $B _i$ are not duplicated and guaranteed to \leq a_i,b_i \leq n$

Time limit: $2\texttt{s}$

Space limitation: $512\texttt{mb}$

* *, can't see how the pressure DP Ah!

Me: "How about this problem D ah?" ”

Small Self-closing * *: "Direct D AH "

Me: "I am not good, how to record status?" ”

Small self-closing * *: "direct 01 record Ah"

Me: "What does 01 mean?" ”

Small self-closing * *: "The car is not taken."

Go back and sweep the questions, listen to the afternoon, barely understand.

The above is nonsense

30pts: All sorts of promiscuity (do you know next_permutation such a function)

70pts: Full arrangement

90pts: Pressure DP

First of all, we are to order in the number of orders to make the shipment on the top of stack 1th, the person who will bubble sort should know that the relative position of the two order of the correct express is unchanged. Like what:

"2018.10.3" Halloween Delivery

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.