Chip Construction
Time limit1sMemory limit128 MB
Given N component priorities and K non-crossing power lines each serving up to two components, find a nesting-valid pairing that maximizes total sum of squares/products.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Intervals
- Solved
- No attempts yet
Problem
There are N components on a board, numbered 1 through N from left to right. Each component has a nonnegative integer priority.
Only K power lines are available. One power line may be connected directly to one component, and it may also be shared with one other component. Thus, one power line can serve at most two components.
Wires placed on the top of the board must not cross each other. A wire may, however, be placed inside the interval enclosed by another wire.
The chip's importance is computed as follows.
- If a power line is connected to exactly one component, add the square of that component's priority.
- If a power line is connected to two components, add the product of the two priorities.
Output a valid connection whose chip importance is as large as possible.
Input
The first line contains two integers N (1 <= N <= 50) and K (1 <= K <= 20).
The second line contains N integers. The i-th integer is the priority of component i, and every priority is between 0 and 1,000 inclusive.
Output
For the first K lines, output the component number directly connected to each power line. Output 0 for an unused power line.
For the next N lines, output which component each component shares power with. If a component uses a power line alone, output its own number. If a component does not use power, output 0. If two components share the same power line, they must output each other's numbers.
You may output any valid configuration that maximizes the chip's importance.