Electronic devices
Time limit1sMemory limit512 MB
Assign distinct power supplies to components so every device i gets at least Y_i working components, with a supply's chosen power matching the component's exact requirement, and output the lexicographically smallest connection list.
- Level
Hard8 of 10
- Topics
- Greedy, Sorting, Hash map, Implementation
- Solved
- No attempts yet
Problem
Jongbeom has electronic devices at home. Device is made of components, and component of device works only when it receives exactly units of power. With less than it does not have enough power to run, and with more than it burns out. Device can be used when at least of its components work.
Jongbeom has power supplies. Power supply picks one of distinct power values and feeds it to exactly one component. The -th smallest power that supply can deliver is .
Until the move, Jongbeom used all devices without trouble. He pulled out every cable without writing down which supply had been connected to which component, so nothing works in the new place. Find a set of connections that makes all devices usable again.
Input
The first line contains . ()
Lines 2 through describe the devices.
- Line contains and . ()
- Line contains the integers . ()
Line contains . ()
Lines through describe the power supplies.
- Line contains . ()
- Line contains the integers in increasing order. ()
At least one set of connections that makes all devices usable is guaranteed to exist.
Output
Connect exactly components on device , so the number of connections equals .
Print on the first line. Each of the next lines holds one connection as four integers , , , . It means that power supply is connected to component of device , and that the supply delivers the -th smallest power it can produce.
A component may appear at most once, and a power supply may appear at most once. The power a connected component needs must equal the power its supply delivers.
Print the lines sorted by comparing the four integers in the order , , , . If several answers satisfy the rules, print the one whose sequence of all integers, read from the top line down, is lexicographically smallest.
Hint
The answer to the first example has 5 connections.
Device 1 takes power 4 on component 1 from supply 2, and power 3 on component 3 from supply 1. Two components work, which meets , so device 1 can be used.
Device 2 takes power 4 on component 2 from supply 3, power 1 on component 3 from supply 5, and power 4 on component 4 from supply 4. Three components work, so device 2 can be used as well.
Component 1 of device 2 needs power 2, and supply 2 is the only one that can deliver power 2. In the lexicographically smallest answer, component 1 of device 1 takes supply 2, so device 2 connects its other three components.