QOJ.ac

QOJ

Time Limit: 4 s - 6 s Memory Limit: 1024 MB Total points: 39

#5812. Marbles

統計

Problem

You have 2n marbles on a square grid. The marbles are colored in n different colors such that there are exactly 2 marbles of each color. The marbles are placed at the coordinates (1,0), (2,0), ..., (2n, 0).

Your task is to draw a path for each color that joins the two marbles of that color. Each path should be composed of vertical or horizontal line segments between grid points. No two paths can intersect or touch each other. No path may cross the y=0 line. Each path can only touch the y=0 line at the position of the two marbles it is connecting, so the first and last line segment of each path must be vertical.

Given an arrangement of marbles, return the minimum height of a solution, or return -1 if no solution exists. The height is defined as the difference between the highest and lowest Y-coordinates of the paths used.

An example:

red red blue yellow blue yellow

One solution would be:

+---+    +-----------+
|   |    |           |
red red blue yellow blue yellow
|           |
+-----------+

The minimum height is 2 in this case.

Input

The first line of input gives the number of cases, T. T test cases follow. The first line of each case contains n, the number of different colors for the marbles. The next line contains a string of 2n words separated by spaces which correspond to the colors of the marbles, in order from left to right. Each color is a string of lower case letters ('a' .. 'z') no longer than 10 characters. There will be exactly n different colors and each color will appear exactly twice.

Output

For each test case, output one line containing "Case #x: ", where x is the case number (starting from 1), followed by the height of any optimal solution, or -1 if no solution exists.

Limits

Memory limit: 1 GB.

1 <= T <= 50.

Small dataset (7 Points)

Time limit: 30 4 seconds.

1 <= n <= 20.

Large dataset (32 Points)

Time limit: 60 6 seconds.

1 <= n <= 500.

Sample

4
3
red red blue yellow blue yellow
3
red blue yellow red blue yellow
3
red blue yellow blue yellow red
3
red red blue blue yellow yellow
Case #1: 2
Case #2: -1
Case #3: 3
Case #4: 1
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.